Skip to content

B024 false negative when there's a class var #471

@aureliojargas

Description

@aureliojargas
from abc import ABC


class Foo(ABC):
    BAR = 1

    def method(self):
        bar()

Because of the BAR = 1 declaration, B024 is considering Foo an ABC, but it isn't.

Expected:

$ flake8 --isolated --select B024 abc.py
abc.py:4:1: B024 Foo is an abstract base class, but none of the methods it defines are abstract. This is not necessarily an error, but you might have forgotten to add the @abstractmethod decorator, potentially in conjunction with @classmethod, @property and/or @staticmethod.
$

But got:

$ flake8 --isolated --select B024 abc.py
$

Info:

$ flake8 --version
7.0.0 (flake8-bugbear: 24.4.26, mccabe: 0.7.0, pycodestyle: 2.11.1, pyflakes: 3.2.0) CPython 3.12.2 on Darwin

This looks related to the fix for #293.

If I comment out this code, it works as expected:

flake8-bugbear/bugbear.py

Lines 925 to 929 in d1aec4c

# 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)):
has_abstract_method = True
continue

By the way, this same issue was reported to ruff in astral-sh/ruff#11208 and they got a fix. Maybe here a similar patch would work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions