Debian Codegraph @

test-data/unit/semanal-classvar.test

raw
1 [case testClassVarDef]
2 from typing import ClassVar
3 class A:
4 x = 1 # type: ClassVar[int]
5 [out]
6 MypyFile:1(
7 ImportFrom:1(typing, [ClassVar])
8 ClassDef:2(
9 A
10 AssignmentStmt:3(
11 NameExpr(x [m])
12 IntExpr(1)
13 builtins.int)))
14
15 [case testClassVarDefInModuleScope]
16 from typing import ClassVar
17 x = None # type: ClassVar[int]
18 [out]
19 main:2: error: ClassVar can only be used for assignments in class body
20
21 [case testClassVarDefInFuncScope]
22 from typing import ClassVar
23 def f() -> None:
24 x = None # type: ClassVar[int]
25 [out]
26 main:3: error: ClassVar can only be used for assignments in class body
27
28 [case testClassVarDefInMethod]
29 from typing import ClassVar
30 class A:
31 def f(self) -> None:
32 x = None # type: ClassVar
33 [out]
34 main:4: error: ClassVar can only be used for assignments in class body
35
36 [case testClassVarTooManyArguments]
37 from typing import ClassVar
38 class A:
39 x = 1 # type: ClassVar[int, str]
40 [out]
41 main:3: error: ClassVar[...] must have at most one type argument
42
43 [case testClassVarWithoutArguments]
44 from typing import ClassVar
45 class A:
46 x = 1 # type: ClassVar
47 [out]
48 MypyFile:1(
49 ImportFrom:1(typing, [ClassVar])
50 ClassDef:2(
51 A
52 AssignmentStmt:3(
53 NameExpr(x [m])
54 IntExpr(1)
55 builtins.int)))
56
57 [case testClassVarWithTypeVar]
58
59 from typing import ClassVar, TypeVar
60 T = TypeVar('T')
61 class A:
62 x = None # type: ClassVar[T]
63 [out]
64 main:5: error: Type variable "__main__.T" is unbound
65 main:5: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
66 main:5: note: (Hint: Use "T" in function signature to bind "T" inside a function)
67
68 [case testClassVarInFunctionArgs]
69 from typing import ClassVar
70 def f(x: str, y: ClassVar) -> None: pass
71 [out]
72 main:2: error: ClassVar can only be used for assignments in class body
73
74 [case testClassVarInMethodArgs]
75 from typing import ClassVar
76 class A:
77 def f(x: str, y: ClassVar) -> None: pass
78 [out]
79 main:3: error: ClassVar can only be used for assignments in class body
80
81 [case testClassVarFunctionRetType]
82 from typing import ClassVar
83 def f() -> ClassVar: pass
84 [out]
85 main:2: error: ClassVar can only be used for assignments in class body
86
87 [case testClassVarMethodRetType]
88 from typing import ClassVar
89 class A:
90 def f(self) -> ClassVar: pass
91 [out]
92 main:3: error: ClassVar can only be used for assignments in class body
93
94 [case testMultipleClassVarInFunctionSig]
95 from typing import ClassVar
96 def f(x: ClassVar, y: ClassVar) -> ClassVar: pass
97 [out]
98 main:2: error: ClassVar can only be used for assignments in class body
99
100 [case testClassVarInCallableArgs]
101 from typing import Callable, ClassVar, Any
102 f = None # type: Callable[[int, ClassVar], Any]
103 [out]
104 main:2: error: Invalid type: ClassVar nested inside other type
105
106 [case testClassVarInCallableRet]
107 from typing import Callable, ClassVar
108 f = None # type: Callable[..., ClassVar]
109 [out]
110 main:2: error: Invalid type: ClassVar nested inside other type
111
112 [case testClassVarInUnion]
113 from typing import ClassVar, Union
114 x = None # type: Union[ClassVar, str]
115 [out]
116 main:2: error: Invalid type: ClassVar nested inside other type
117
118 [case testClassVarInUnionAsAttribute]
119 from typing import ClassVar, Union
120 class A:
121 x = None # type: Union[ClassVar, str]
122 [out]
123 main:3: error: Invalid type: ClassVar nested inside other type
124
125 [case testListWithClassVars]
126 from typing import ClassVar, List
127 x = [] # type: List[ClassVar]
128 [builtins fixtures/list.pyi]
129 [out]
130 main:2: error: Invalid type: ClassVar nested inside other type
131
132 [case testTupleClassVar]
133 from typing import ClassVar, Tuple
134 x = None # type: Tuple[ClassVar, int]
135 [builtins fixtures/tuple.pyi]
136 [out]
137 main:2: error: Invalid type: ClassVar nested inside other type
138
139 [case testMultipleLvaluesWithList]
140 from typing import ClassVar, Tuple
141 class A:
142 [x, y] = None, None # type: Tuple[ClassVar, ClassVar]
143 [builtins fixtures/tuple.pyi]
144 [out]
145 main:3: error: Invalid type: ClassVar nested inside other type
146
147 [case testDeeplyNested]
148 from typing import Callable, ClassVar, Union
149 class A: pass
150 class B:
151 x = None # type: Union[str, Callable[[A, ClassVar], int]]
152 [out]
153 main:4: error: Invalid type: ClassVar nested inside other type
154
155 [case testClassVarInClassVar]
156 from typing import ClassVar
157 class A:
158 x = None # type: ClassVar[ClassVar[int]]
159 [out]
160 main:3: error: Invalid type: ClassVar nested inside other type
161
162 [case testInsideGeneric]
163 from typing import ClassVar, Generic, TypeVar
164 T = TypeVar('T')
165 class A(Generic[T]): pass
166 class B:
167 x = None # type: A[ClassVar]
168 [out]
169 main:5: error: Invalid type: ClassVar nested inside other type
170
171 [case testDefineOnSelf]
172 from typing import ClassVar
173 class A:
174 def __init__(self) -> None:
175 self.x = None # type: ClassVar
176 [out]
177 main:4: error: ClassVar can only be used for assignments in class body
178
179 [case testForIndex]
180 from typing import ClassVar
181 for i in []: # type: ClassVar
182 pass
183 [out]
184 main:2: error: ClassVar can only be used for assignments in class body
185
186 [case testForIndexInClassBody]
187 from typing import ClassVar
188 class A:
189 for i in []: # type: ClassVar
190 pass
191 [out]
192 main:3: error: ClassVar can only be used for assignments in class body
193
194 [case testWithStmt]
195 from typing import ClassVar
196 class A: pass
197 with A() as x: # type: ClassVar
198 pass
199 [out]
200 main:3: error: ClassVar can only be used for assignments in class body
201
202 [case testWithStmtInClassBody]
203 from typing import ClassVar
204 class A: pass
205 class B:
206 with A() as x: # type: ClassVar
207 pass
208 [out]
209 main:4: error: ClassVar can only be used for assignments in class body