-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add exception for PYI034 for the usage of Self in metaclasses #8353
Comments
Interesting, flake8-pyi reports the same thing here. (\cc @AlexWaygood, you may be interested in this.) |
Yup. Personally I think it's somewhat unfortunate that PEP 673 mandates this behaviour for metaclasses — but it does, and it's pretty unlikely that that's going to change any time soon. So mypy is definitely correct here; this is indeed a false positive from flake8-pyi. The somewhat-hacky workaround we use in typeshed that keeps both flake8-pyi and mypy happy here is to just use a normal TypeVar that happens to be called Self(!): https://github.com/python/typeshed/blob/f7aa7b709a826ed34f52b1de3f7095f90f349a9c/stdlib/abc.pyi#L14-L19 Possibly flake8-pyi could add some heuristics here to try to detect if a class is a metaclass, and avoid emitting this error if so. I'm not sure if it would be worth it for us, though, since:
Ruff isn't necessarily bound by the same constraints, though, so if you can see a way of avoiding this false positive, then I'd say go ahead! |
Thanks @AlexWaygood for taking the time to look at the issue!
I think one solution would be to update the semantic model to include a new |
Here's my best attempt at fixing this for flake8-pyi: PyCQA/flake8-pyi#436. The check's already pretty complicated, so it didn't end up significantly complicating the implementation after all :) But it would be great if ruff could fix this without the hacky heuristics that I'm using there ;) |
Does Ruff run this check in |
I actually think this rule could still be useful for from collections.abc import Iterator
class Foo(Iterator[int]):
def __new__(cls) -> Foo:
return cls()
def __iter__(self) -> Foo:
return self
def __next__(self) -> int:
return 42
def __enter__(self) -> Foo:
return self |
(We do run this over |
This is included in the latest flake8-pyi release FYI! Thanks for the ping @charliermarsh :D |
Awesome, thanks @AlexWaygood! You rock. |
PEP 673 forbids the use of `typing(_extensions).Self` in metaclasses, so we want to avoid flagging `PYI034` on metaclasses. This is based on an analogous change in `flake8-pyi`: PyCQA/flake8-pyi#436. Closes #8353.
minimal example:
in this dummy metaclass - if I run
ruff
enabling the rulePYI034
I gotUnfortunately I think this is one of the exceptions in the usual cases the error talks about :)
If I annotate
__new__
withSelf
as return type, in fact,mypy
tells me(as you can see in https://github.com/python/mypy/blob/master/mypy/typeanal.py#L688-L689 )
Could you refine
PYI034
to ignore theSelf
requirements for metaclasses?The text was updated successfully, but these errors were encountered: