-
Notifications
You must be signed in to change notification settings - Fork 281
Open
Labels
Milestone
Description
Describe the Bug
See https://typing.python.org/en/latest/spec/generics.html#scoping-rules-for-type-variables for the scoping rules to implement
See test case test_typevar_scoping_restrictions
pyrefly/pyrefly/lib/test/generic_restrictions.rs
Line 1046 in 1a28df5
| test_typevar_scoping_restrictions, |
from typing import TypeVar, Generic, TypeAlias
from collections.abc import Iterable
T = TypeVar("T")
S = TypeVar("S")
# Unbound TypeVar S used in generic function body
def fun_3(x: T) -> list[T]:
y: list[T] = [] # OK
z: list[S] = [] # should error: S not in scope
return y
# Unbound TypeVar S in class body (not in method)
class Bar(Generic[T]):
an_attr: list[S] = [] # should error: S not in scope
# Nested class using outer class's TypeVar
class Outer(Generic[T]):
class Bad(Iterable[T]): # should error: T from outer not in scope
...
class AlsoBad:
x: list[T] # should error: T from outer not in scope
alias: TypeAlias = list[T] # should error: T not allowed in TypeAlias here
# Unbound TypeVars at global scope
global_var1: T # should error
global_var2: list[T] = [] # should error
list[T]() # should errorSandbox Link
No response
(Only applicable for extension issues) IDE Information
No response
Reactions are currently unavailable