8000 [PEP 695] Add tests for type alias in class body or function (#17334) · python/mypy@ddcf90d · GitHub
[go: up one dir, main page]

Skip to content

Commit ddcf90d

Browse files
authored
[PEP 695] Add tests for type alias in class body or function (#17334)
Work on #15238.
1 parent fbaa7e0 commit ddcf90d

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

test-data/unit/check-python312.test

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,3 +1399,57 @@ c1: A3[C, int]
13991399
c2: A3[D, str]
14001400
c3: A3[C, N] # E: Value of type variable "S" of "A3" cannot be "N"
14011401
c4: A3[int, str] # E: Type argument "int" of "A3" must be a subtype of "C"
1402+
1403+
[case testPEP695TypeAliasInClassBodyOrFunction]
1404+
# flags: --enable-incomplete-feature=NewGenericSyntax
1405+
1406+
class C:
1407+
type A = int
1408+
type B[T] = list[T] | None
1409+
a: A
1410+
b: B[str]
1411+
1412+
def method(self) -> None:
1413+
v: C.A
1414+
reveal_type(v) # N: Revealed type is "builtins.int"
1415+
1416+
reveal_type(C.a) # N: Revealed type is "builtins.int"
1417+
reveal_type(C.b) # N: Revealed type is "Union[builtins.list[builtins.str], None]"
1418+
1419+
C.A = str # E: Incompatible types in assignment (expression has type "Type[str]", variable has type "TypeAliasType")
1420+
1421+
x: C.A
1422+
y: C.B[int]
1423+
reveal_type(x) # N: Revealed type is "builtins.int"
1424+
reveal_type(y) # N: Revealed type is "Union[builtins.list[builtins.int], None]"
1425+
1426+
def f() -> None:
1427+
type A = int
1428+
type B[T] = list[T] | None
1429+
a: A
1430+
reveal_type(a) # N: Revealed type is "builtins.int"
1431+
1432+
def g() -> None:
1433+
b: B[int]
1434+
reveal_type(b) # N: Revealed type is "Union[builtins.list[builtins.int], None]"
1435+
1436+
class D:
1437+
def __init__(self) -> None:
1438+
type A = int
1439+
self.a: A = 0
1440+
type B[T] = list[T]
1441+
self.b: B[int] = [1]
1442+
1443+
reveal_type(D().a) # N: Revealed type is "builtins.int"
1444+
reveal_type(D().b) # N: Revealed type is "builtins.list[builtins.int]"
1445+
1446+
class E[T]:
1447+
type X = list[T] # E: All type parameters should be declared ("T" not declared)
1448+
1449+
def __init__(self) -> None:
1450+
type A = list[T] # E: All type parameters should be declared ("T" not declared)
1451+
self.a: A
1452+
1453+
reveal_type(E[str]().a) # N: Revealed type is "builtins.list[Any]"
1454+
[builtins fixtures/tuple.pyi]
1455+
[typing fixtures/typing-full.pyi]

0 commit comments

Comments
 (0)
0