Skip to content

Commit fbaa7e0

Browse files
authored
[PEP 695] Add tests for type aliases with bounds and value restrictions (#17330)
The functionality already works, but there was missing test coverage. Work on #15238.
1 parent ad0e180 commit fbaa7e0

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

test-data/unit/check-python312.test

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,3 +1360,42 @@ C: TypeAlias = int
13601360
[typing fixtures/typing-full.pyi]
13611361
[out2]
13621362
tmp/a.py:2: error: "TypeAliasType" not callable
1363+
1364+
[case testPEP695TypeAliasBoundAndValueChecking]
1365+
# flags: --enable-incomplete-feature=NewGenericSyntax
1366+
from typing import Any, cast
1367+
1368+
class C: pass
1369+
class D(C): pass
1370+
1371+
type A[T: C] = list[T]
1372+
a1: A
1373+
reveal_type(a1) # N: Revealed type is "builtins.list[Any]"
1374+
a2: A[Any]
1375+
a3: A[C]
1376+
a4: A[D]
1377+
a5: A[object] # E: Type argument "object" of "A" must be a subtype of "C"
1378+
a6: A[int] # E: Type argument "int" of "A" must be a subtype of "C"
1379+
1380+
x1 = cast(A[C], a1)
1381+
x2 = cast(A[None], a1) # E: Type argument "None" of "A" must be a subtype of "C"
1382+
1383+
type A2[T: (int, C)] = list[T]
1384+
b1: A2
1385+
reveal_type(b1) # N: Revealed type is "builtins.list[Any]"
1386+
b2: A2[Any]
1387+
b3: A2[int]
1388+
b4: A2[C]
1389+
b5: A2[D] # E: Value of type variable "T" of "A2" cannot be "D"
1390+
b6: A2[object] # E: Value of type variable "T" of "A2" cannot be "object"
1391+
1392+
list[A2[int]]()
1393+
list[A2[None]]() # E: Invalid type argument value for "A2"
1394+
1395+
class N(int): pass
1396+
1397+
type A3[T: C, S: (int, str)] = T | S
1398+
c1: A3[C, int]
1399+
c2: A3[D, str]
1400+
c3: A3[C, N] # E: Value of type variable "S" of "A3" cannot be "N"
1401+
c4: A3[int, str] # E: Type argument "int" of "A3" must be a subtype of "C"

0 commit comments

Comments
 (0)