From cfc24290a2d0092f5714f1e893f55ac73cf6bc25 Mon Sep 17 00:00:00 2001 From: Fabian Henze <32638720+henzef@users.noreply.github.com> Date: Sun, 26 Nov 2023 01:12:51 +0100 Subject: [PATCH] B902: Add exceptions for standard library metaclasses (#415) I faced a false positive when deriving my metaclass from ABCMeta, which should be fixed with this commit. I also added EnumMeta to this list, because it was the only other public metaclass in the standard library. --- bugbear.py | 2 +- tests/b024.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bugbear.py b/bugbear.py index ee2afd4..88fbd2b 100644 --- a/bugbear.py +++ b/bugbear.py @@ -1046,7 +1046,7 @@ def is_classmethod(decorators: Set[str]) -> bool: return bases = {b.id for b in cls.bases if isinstance(b, ast.Name)} - if "type" in bases: + if any(basetype in bases for basetype in ("type", "ABCMeta", "EnumMeta")): if is_classmethod(decorators): expected_first_args = B902.metacls kind = "metaclass class" diff --git a/tests/b024.py b/tests/b024.py index 238dcd3..f975887 100644 --- a/tests/b024.py +++ b/tests/b024.py @@ -92,7 +92,7 @@ def method(self): class non_keyword_abcmeta_1(ABCMeta): # safe - def method(self): + def method(cls): foo()