Skip to content

Commit

Permalink
Fix PyCQA#293, B024 now skips classes with class attribute declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkdl committed Oct 4, 2022
1 parent 0fec7e5 commit 4ecf9bc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bugbear.py
Expand Up @@ -640,6 +640,10 @@ def is_abstract_decorator(expr):
map(is_abstract_decorator, stmt.decorator_list)
):
return
# https://github.com/PyCQA/flake8-bugbear/issues/293
# Ignore abc's that declares a class attribute that must be set
if isinstance(stmt, (ast.AnnAssign, ast.Assign)):
return

self.errors.append(B024(node.lineno, node.col_offset, vars=(node.name,)))

Expand Down
17 changes: 17 additions & 0 deletions tests/b024.py
Expand Up @@ -89,3 +89,20 @@ def method(self):
class multi_super_2(notabc.ABC, metaclass=abc.ABCMeta): # error
def method(self):
...


class abc_set_class_variable_1(ABC): # safe
foo: int


class abc_set_class_variable_2(ABC): # safe
foo = 2


class abc_set_class_variable_3(ABC): # safe
foo: int = 2


# this doesn't actually declare a class variable, it's just an expression
class abc_set_class_variable_4(ABC): # error
foo
1 change: 1 addition & 0 deletions tests/test_bugbear.py
Expand Up @@ -365,6 +365,7 @@ def test_b024(self):
B024(74, 0, vars=("abc_Base_2",)),
B024(84, 0, vars=("multi_super_1",)),
B024(89, 0, vars=("multi_super_2",)),
B024(107, 0, vars=("abc_set_class_variable_4",)),
)
self.assertEqual(errors, expected)

Expand Down

0 comments on commit 4ecf9bc

Please sign in to comment.