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

Should we discuss "scope" of type variables in PEP 484? #249

Closed
ilevkivskyi opened this issue Jul 19, 2016 · 3 comments
Closed

Should we discuss "scope" of type variables in PEP 484? #249

ilevkivskyi opened this issue Jul 19, 2016 · 3 comments

Comments

@ilevkivskyi
Copy link
Member

I did not find any discussion about the "visibility" of type variables. For example, here

from typing import TypeVar, Generic

T = TypeVar('T')

def fun_1(x: T) -> T:
    ...
def fun_2(x: T) -> T:
    ...

class MyClass(Generic[T]):
    def meth_1(self, x: T) -> T:
        ...
    def meth_2(self, x: T) -> T:
        ...

I think the type variable T could take different values in fun_1 and fun_2, but it is always the same for meth_1 and meth_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?

@gvanrossum
Copy link
Member

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 meth_1 and meth_2 the argument is constrained by the "value" for T that applies to the instance. E.g. in

a = MyClass()  # type: MyClass[int]
a.meth_1(x)  # For some x

the type of x must be compatible with int.

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.

@ilevkivskyi
Copy link
Member Author

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 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?

@gvanrossum
Copy link
Member

gvanrossum commented Jul 27, 2016 via email

gvanrossum pushed a commit to python/peps that referenced this issue Jul 29, 2016
Also clarify the description of NewType() a bit.

Fixes python/typing#249
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants