PEP 484: Clarify scoping of type variables (#231)

This commit is contained in:
Ivan Levkivskyi 2017-03-24 16:35:10 +01:00 committed by Guido van Rossum
parent 07c157552d
commit 257e909cae
1 changed files with 18 additions and 4 deletions

View File

@ -472,9 +472,9 @@ However, there are some special cases in the static typechecking context:
def fun_2(x: T) -> T: ... # and here could be different
fun_1(1) # This is OK, T is inferred to be int
fun_2('a') # This is aslo OK, now T is str
fun_2('a') # This is also OK, now T is str
* A type variable used in a method of a generic class that coincisides
* A type variable used in a method of a generic class that coincides
with one of the variables that parameterize this class is always bound
to that variable. Example::
@ -537,8 +537,22 @@ However, there are some special cases in the static typechecking context:
class MyGeneric(Generic[T]):
...
* A generic class nested in another generic class cannot use the same
type variables, unless the inner class definition is inside a function.
* A generic class nested in another generic class cannot use same type
variables. The scope of the type variables of the outer class
doesn't cover the inner one::
T = TypeVar('T')
S = TypeVar('S')
class Outer(Generic[T]):
class Bad(Iterable[T]): # Error
...
class AlsoBad:
x = None # type: List[T] # Also an error
class Inner(Iterable[S]): # OK
...
attr = None # type: Inner[T] # Also OK
Instantiating generic classes and type erasure