-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Type Alias inside class unsupported #10788
Comments
I found that import typing
T = typing.NewType('T', int)
class C:
V1 = T
V2: typing.TypeAlias = T
x: T = T(5)
y1: C.V1 = C.V1(5)
y2: C.V2 = C.V2(5) I found that y2 works but y1 doesn't. This is consistent with this comment - https://mail.python.org/archives/list/[email protected]/message/QNJEVUIHGCO6V4RYKBQK7FSRYHZZGWNH/ - on PEP613 |
Yeah, I think this is by design and PEP 613 is the appropriate way forward, see Line 2591 in 3d82f6f
|
PEP613 appears to explicitly ban type aliases in nested scope (https://www.python.org/dev/peps/pep-0613/#scope-restrictions) Though the wording around it makes it seem to me that it meant to declare that implicit type aliases were banned in nested scope - and explicit type aliases are to be preferred (AKA |
Oh interesting. I think mypy's current behaviour is good and don't see a reason explicit type aliases in nested scope should be banned by PEP. Going through mypy issues today it seems like this is not an uncommon use case. I'd be happy with "only use explicit type aliases in nested scope" as PEP guidance. I guess pyre or whatever type checker can choose not to support type aliases in nested scope, but I don't think PEP should prohibit mypy from recognising this. Maybe worth emailing typing-sig about? |
FWIW, I interpreted the PEP to mean that an explicit type alias wasn't allowed in a nested scope, so pyright generates an error for this condition. Conversely, I allow implicit type aliases in nested scopes, since PEP 484 doesn't appear to disallow this. So if we want to clarify in the PEP that explicit type aliases are allowed in nested scopes, I'd be happy to make that change in pyright's logic. |
IMO, explicit type alias / explicit variable should be allowed at any scope
For the implicit ones - some ideas
IMO, 2b makes the most sense to me (pyright's current behavior). It seems really rare to me that a type gets assigned to a variable other than an alias. 2a is closer to mypy's current behavior. (1) allows for flexibility of either, which might be a good starting point. I'll try to email typing-sig! Thanks for commenting. |
Bug Report
When there's a type alias inside a class, it's not considered to be a type by mypy.
pyright appears to support this.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.10&gist=509f8043f236ccc7e178dd56f526eaf1
Expected Behavior
No errors
Actual Behavior
The text was updated successfully, but these errors were encountered: