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

Rule PYI019 should not flag metaclass methods #16127

Closed
Geo5 opened this issue Feb 12, 2025 · 7 comments · Fixed by #16141
Closed

Rule PYI019 should not flag metaclass methods #16127

Geo5 opened this issue Feb 12, 2025 · 7 comments · Fixed by #16141
Assignees
Labels
bug Something isn't working help wanted Contributions especially welcome rule Implementing or modifying a lint rule

Comments

@Geo5
Copy link

Geo5 commented Feb 12, 2025

Description

This code which includes a metaclass and passes mypy without errors:

from typing import TypeVar

F = TypeVar("F")


class FooMeta(type):
    def bar(cls: F) -> F:
        return cls

is fixed to:

ruff check --isolated --select PYI --preview --fix --diff
--- test.py
+++ test.py
@@ -1,8 +1,9 @@
 from typing import TypeVar
+from typing_extensions import Self
 
 F = TypeVar("F")
 
 
 class FooMeta(type):
-    def bar(cls: F) -> F:
+    def bar(cls) -> Self:
         return cls

Would fix 1 error.

Which mypy does not like:

$ mypy --strict test.py 
test.py:8: error: Self type cannot be used in a metaclass  [misc]
Found 1 error in 1 file (checked 1 source file)

$ python --version
Python 3.11.10
$ ruff --version
ruff 0.9.6
$ mypy --version
mypy 1.15.0 (compiled: yes)
@ntBre ntBre added bug Something isn't working fixes Related to suggested fixes for violations labels Feb 12, 2025
@Geo5

This comment has been minimized.

@Geo5
Copy link
Author

Geo5 commented Feb 12, 2025

See also #8353 for a similar case where the relevant PEP is linked: https://peps.python.org/pep-0673/#valid-locations-for-self
Seems like the fix is indeed incorrect and the mypy docs example seems to be incorrect aswell

@ntBre
Copy link
Contributor

ntBre commented Feb 12, 2025

Thanks for the report and the for the additional links! I agree that it looks like a bug in ruff based on the PEP.

@Geo5
Copy link
Author

Geo5 commented Feb 12, 2025

(I also created an issue for the mypy documentation linked above python/mypy#18668)

@AlexWaygood
Copy link
Member

AlexWaygood commented Feb 12, 2025

Yup, this is a bug here -- thanks for the report @Geo5! For some reason we check whether the class is a metaclass for PYI034 before issuing a diagnostic:

// PEP 673 forbids the use of `typing(_extensions).Self` in metaclasses.
if analyze::class::is_metaclass(class_def, semantic).is_yes() {
return;
}

but not for PYI019.

Should be easy to fix!

@AlexWaygood AlexWaygood added rule Implementing or modifying a lint rule help wanted Contributions especially welcome and removed fixes Related to suggested fixes for violations labels Feb 12, 2025
@AlexWaygood AlexWaygood changed the title Rule PYI019 incorrect autofix for metaclasses Rule PYI019 should not flag metaclass methods Feb 12, 2025
@vladNed
Copy link
Contributor

vladNed commented Feb 13, 2025

is this issue still unassigned ? I would like to give it a go

@AlexWaygood
Copy link
Member

@vladNed go for it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Contributions especially welcome rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants