B902: don't false-positive on metaclasses with a dotted base#559
Merged
cooperlees merged 1 commit intoJun 18, 2026
Merged
Conversation
The B902 metaclass check only collected bare `ast.Name` bases, so a metaclass written with a dotted base (`abc.ABCMeta`, `enum.EnumMeta`) was treated as a plain class and its `cls`/`metacls`/`mcs` first arguments were wrongly flagged. Collect the attribute name from dotted `ast.Attribute` bases too, matching the existing bare-name handling. The b024 fixtures `multi_super_1` and `non_keyword_abcmeta_2` used `self` on what are actually metaclasses (only valid while the bug hid them); they now use `cls`, consistent with the bare `non_keyword_abcmeta_1` sibling in the same file. Fixes PyCQA#411.
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a B902 false-positive where custom metaclasses inheriting from dotted standard-library metaclass names (e.g. abc.ABCMeta, enum.EnumMeta) were not recognized as metaclasses, causing valid first-argument names like mcs/metacls/cls to be incorrectly flagged.
Changes:
- Extend B902 metaclass-base detection to include
ast.Attributebases (dotted bases), not just bareast.Name. - Add/adjust eval fixtures to ensure dotted metaclass bases don’t trigger B902, while still reporting genuinely-wrong first arguments.
- Update README B902 documentation and changelog entry to reflect the dotted-base behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| bugbear.py | Updates B902 base-name extraction to include dotted bases (ast.Attribute) so metaclasses are correctly detected. |
| tests/eval_files/b902.py | Adds fixtures covering dotted abc.ABCMeta / enum.EnumMeta metaclasses (no warnings) and a precision case (still warns). |
| tests/eval_files/b024.py | Adjusts two fixtures to use cls so they remain clean under the corrected B902 behavior. |
| README.rst | Updates B902 description and UNRELEASED changelog to document the dotted-base fix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
what
B902 raises a false positive on a metaclass whose base is written with a dotted name, e.g.
This is issue #411.
why
The B902 metaclass check builds the set of base names from bare
ast.Namenodes only:A dotted base like
abc.ABCMetais anast.Attribute, so it gets dropped. The class is then treated as a plain class and itscls/metacls/mcsfirst arguments are flagged. #415 added detection for the baretype/ABCMeta/EnumMetanames back in 2023; the dotted form was the remaining gap, and #411 is still open.fix
Also take the attribute name from dotted
ast.Attributebases:Scoped to direct bases (transitive base chains stay out of scope). This is the same name-based heuristic the check already used, so dotted bases now behave like the bare ones.
tests
tests/eval_files/b902.pygains dottedabc.ABCMetaandenum.EnumMetametaclasses (clean), plus a precision case where a wrong first arg on a dotted metaclass is still reported as a metaclass instance method. Before the fix those clean fixtures emit spurious B902s and the precision error is lost; after, they are correct.Two classes in
tests/eval_files/b024.py(multi_super_1,non_keyword_abcmeta_2) directly subclassabc.ABCMetaand usedself, which was only valid while the bug hid their metaclass-ness. They now usecls, consistent with the barenon_keyword_abcmeta_1sibling in the same file.Also updated the B902 README entry, which only mentioned
type, to coverABCMeta/EnumMetaand dotted bases. Full suite green.Fixes #411.