-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathgenerics_syntax_declarations.py
83 lines (44 loc) · 1.38 KB
/
generics_syntax_declarations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Validates the type parameter syntax introduced in PEP 695.
"""
# Specification: https://peps.python.org/pep-0695/#type-parameter-declarations
# This generic class is parameterized by a TypeVar T, a
# TypeVarTuple Ts, and a ParamSpec P.
from typing import Generic, Protocol
class ChildClass[T, *Ts, **P]:
pass
class ClassA[T](Generic[T]): # E: Runtime error
...
class ClassB[S, T](Protocol): # OK
...
class ClassC[S, T](Protocol[S, T]): # E
...
class ClassD[T: str]:
def method1(self, x: T):
x.capitalize() # OK
x.is_integer() # E
class ClassE[T: dict[str, int]]: # OK
pass
class ClassF[S: ForwardReference[int], T: "ForwardReference[str]"]: # OK
...
class ClassG[V]:
class ClassD[T: dict[str, V]]: # E: generic type not allowed
...
class ClassH[T: [str, int]]: # E: illegal expression form
...
class ClassI[AnyStr: (str, bytes)]: # OK
...
class ClassJ[T: (ForwardReference[int], "ForwardReference[str]", bytes)]: # OK
...
class ClassK[T: ()]: # E: two or more types required
...
class ClassL[T: (str,)]: # E: two or more types required
...
t1 = (bytes, str)
class ClassM[T: t1]: # E: literal tuple expression required
...
class ClassN[T: (3, bytes)]: # E: invalid expression form
...
class ClassO[T: (list[S], str)]: # E: generic type
...
class ForwardReference[T]: ...