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.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

False-positive B902 on custom metaclass extending ABCMeta

alexey-pelykh opened this issue · comments

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

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.

@cooperlees mcs is already supported since 19.3.0, the issue is about determining what's a meta class here

if "type" in bases:

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.

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

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

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.

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.

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

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