Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False-positive B902 on custom metaclass extending ABCMeta #411

Open
alexey-pelykh opened this issue Sep 14, 2023 · 8 comments
Open

False-positive B902 on custom metaclass extending ABCMeta #411

alexey-pelykh opened this issue Sep 14, 2023 · 8 comments
Labels

Comments

@alexey-pelykh
Copy link

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 cooperlees added the bug label Sep 15, 2023
@cooperlees
Copy link
Collaborator

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
Copy link
Author

@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.

@jakkdl
Copy link
Contributor

jakkdl commented Sep 16, 2023

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

@alexey-pelykh
Copy link
Author

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
Copy link
Contributor

jakkdl commented Sep 18, 2023

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
Copy link
Contributor

henzef commented Nov 27, 2023

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
Copy link
Author

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

@jakkdl
Copy link
Contributor

jakkdl commented Nov 27, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants