-
Notifications
You must be signed in to change notification settings - Fork 258
Should we discuss "scope" of type variables in PEP 484? #249
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
Comments
I don't think we need to explain that type variables follow Python's normal scope rules, so I presume you're just talking about whether inside a class that's generic in T, all uses of T refer to the T in the class definition. I think that's the only thing that makes sense, as your example shows -- for a = MyClass() # type: MyClass[int]
a.meth_1(x) # For some x the type of If you think the PEP doesn't clarify this sufficiently please suggest some text. I guess there are some edge cases worth ruling about, e.g. a generic class nested in another generic class can't use the same type vars, unless the inner class definition is inside a function -- I learned this by experimenting with mypy but it all seems reasonable. |
I wanted to discuss such edge cases, and maybe something simpler like "non-generic class attributes should not contain free type variables (probably unless it is inside another class if it is generic)" mentioned in python/mypy#1903 I am going to make a PR to python/peps beginning of next week. @gvanrossum , do you have additional suggestions on the rules/corner cases that should be mentioned? |
I will be happy to review what you write, I don't believe I have anything
specific to add at this point.
|
Also clarify the description of NewType() a bit. Fixes python/typing#249
I did not find any discussion about the "visibility" of type variables. For example, here
I think the type variable
T
could take different values infun_1
andfun_2
, but it is always the same formeth_1
andmeth_2
. I could imagine more complex examples with deeply nested classes and functions. Although it might be obvious for some people, maybe we should discuss "scoping rules" for type variables in the PEP?The text was updated successfully, but these errors were encountered: