Skip to content

[refurb] add list of base class exceptions for FURB180 #18148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

robsdedude
Copy link
Contributor

@robsdedude robsdedude commented May 17, 2025

Summary

Add a config option tool.ruff.lint.refurb.allow-abc-meta-bases (["typing.Protocol", "typing_extensions.Protocol"] by default) that allows to specify a list such that if class has at least one base-class and all base-classes are in this list it is exempt from FURB180. This is useful for classes that validate their base-classes at runtime such as the two that are the default value.

Test Plan

Besides the adjusted test ruff_linter::rules::refurb::tests::rules, I created a minimal Python project to test the configuration option.

References

Fixes #13307 (together with #18149)

@robsdedude robsdedude force-pushed the feat/13307-furb180-configure-exceptions branch from 36d80ed to 5b17773 Compare May 17, 2025 11:06
@robsdedude robsdedude force-pushed the feat/13307-furb180-configure-exceptions branch from 5b17773 to 03f6b77 Compare May 17, 2025 11:07
@MichaReiser
Copy link
Member

@ntBre could you take a look at this PR when you're back?

@ntBre ntBre added rule Implementing or modifying a lint rule configuration Related to settings and configuration preview Related to preview mode features labels May 29, 2025
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this all makes sense to me! I just had a couple of minor suggestions.

Copy link
Contributor

github-actions bot commented May 29, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@robsdedude robsdedude marked this pull request as draft June 3, 2025 05:44
@robsdedude robsdedude marked this pull request as ready for review June 3, 2025 08:54
@robsdedude robsdedude requested a review from ntBre June 3, 2025 08:54
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is looking good. Just a couple more suggestions, and one question about naming

@robsdedude robsdedude changed the title FURB180: add list of base class exceptions [refurb] add list of base class exceptions for FURB180 Jun 3, 2025
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks again! This is good to go from my perspective, modulo any further bike-shedding on the name.

@robsdedude robsdedude requested a review from ntBre June 4, 2025 07:53
@robsdedude
Copy link
Contributor Author

@ntBre Just want to make sure this is still on your radar 👀

@ntBre
Copy link
Contributor

ntBre commented Jun 11, 2025

Oh yes, thank you! Let me actually cc someone who might have an opinion on the name (@MichaReiser any thoughts on the names and maybe the two separate options?)

I believe the current state is that we added two new config options:

  • lint.refurb.allowed_abc_meta_bases
  • lint.refurb.extend_allowed_abc_meta_bases

/// Expects to receive a list of fully-qualified names (e.g., `typing.Protocol`, rather than
/// `Protocol`).
#[option(
default = r#"["typing.Protocol", "typing_extensions.Protocol"]"#,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to gate this behind preview, given that we now emit fewer lints (which can lead to unused noqa comments) or do we consider this a bug fix?

From our versioning policy

A new configuration option is added in a backwards compatible way (no formatting changes or new lint errors)

So I think we need to gate this behind preview

@MichaReiser
Copy link
Member

MichaReiser commented Jun 12, 2025

Oh yes, thank you! Let me actually cc someone who might have an opinion on the name (@MichaReiser any thoughts on the names and maybe the two separate options?)

I'm not entirely sure I understnad what you want feedback on. I see that this PR introduces two new options. Are you asking for my opinion on the option names? Is there mroe feedback that you're looking for?

Looking at the issue #13307 (comment) it's unclear if we want these options. More specifically, options have a non-zero cost because users need to understand them. This PR also changes the default for everyone using the rule, which may not what they want.

@robsdedude
Copy link
Contributor Author

This PR also changes the default for everyone using the rule, which may not what they want.

That's a fair point. I figured if users are in the situation where this becomes relevant, they have code along the lines of

class Foo(Protocol, metaclass=ABCMeta): ...

I imagine if a user wants this to be linted, the lint shouldn't be "use ABC as a base instead of ABCMeta", but rather "don't bother specifying ABC/ABCMeta if the base(s) already includes ABC in the class hierarchy".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
configuration Related to settings and configuration preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FURB180 should consider allowlisting some classes
3 participants