PyCQA / flake8-bugbear

A plugin for Flake8 finding likely bugs and design problems in your program. Contains warnings that don't belong in pyflakes and pycodestyle.
MIT License
1.06k stars 104 forks source link

False-positive B902 on custom metaclass extending ABCMeta #411

Open alexey-pelykh opened 1 year ago

alexey-pelykh commented 1 year ago
from abc import ABCMeta

class CustomMeta(ABCMeta):
    def __new__(mcs, name: str, bases: tuple[type, ...], namespace: dict[str, object], /, **kwargs) -> type:
        cls = super().__new__(mcs, name, bases, namespace, **kwargs)

        return cls

causes B902 false-positive

cooperlees commented 1 year ago

Is mcs a well known for a meta class? If so, I'd take a PR allowing this if the class is Abstract of some description.

alexey-pelykh commented 1 year ago

@cooperlees mcs is already supported since 19.3.0, the issue is about determining what's a meta class here https://github.com/PyCQA/flake8-bugbear/blob/d1a8f2bcc09dd84e8dc743b05876cbd058ee3f20/bugbear.py#L990

abc.ABCMeta is also a meta class yet it won't be detected as one if there's another class like in the example extending it.

jakkdl commented 1 year ago

seems pretty straightforward to extend the check to also check for inheritance from [abc.]ABCMeta

alexey-pelykh commented 1 year ago

It would seem that hardcoding all well-known meta classes list is going to be a never-ending story. Yet it's not my call to suggest how to tackle this issue

jakkdl commented 1 year ago

that's unfortunately the way that AST-based static analysis checkers kind of have to do it. The "proper" way of doing it would be to hook into a type checker, but that's currently only possible if switching to LibCST and users run Pyre as their typechecker.

henzef commented 10 months ago

Note that I added a list of well-known metaclasses in #415. It currently only contains the ones from the standard library, but that could of course be extended.

alexey-pelykh commented 10 months ago

@henzef I wonder if that could be extended to a configurable custom list from settings or something 🤔

jakkdl commented 10 months ago

It could definitely be done, adding something similar to --classmethod-decorators, if there's sufficient interest.