Debian Codegraph @

test-data/unit/check-python311.test

raw
1 [case testTryStarSimple]
2 try:
3 pass
4 except* Exception as e:
5 reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[builtins.Exception]"
6 [builtins fixtures/exception.pyi]
7
8 [case testTryStarMultiple]
9 try:
10 pass
11 except* Exception as e:
12 reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[builtins.Exception]"
13 except* RuntimeError as e:
14 reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[builtins.RuntimeError]"
15 [builtins fixtures/exception.pyi]
16
17 [case testTryStarBase]
18 try:
19 pass
20 except* BaseException as e:
21 reveal_type(e) # N: Revealed type is "builtins.BaseExceptionGroup[builtins.BaseException]"
22 [builtins fixtures/exception.pyi]
23
24 [case testTryStarTuple]
25 class Custom(Exception): ...
26
27 try:
28 pass
29 except* (RuntimeError, Custom) as e:
30 reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[builtins.RuntimeError | __main__.Custom]"
31 [builtins fixtures/exception.pyi]
32
33 [case testTryStarInvalidType]
34 class Bad: ...
35 try:
36 pass
37 except* (RuntimeError, Bad) as e: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
38 reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[Any]"
39 [builtins fixtures/exception.pyi]
40
41 [case testTryStarGroupInvalid]
42 try:
43 pass
44 except* ExceptionGroup as e: # E: Exception type in except* cannot derive from BaseExceptionGroup
45 reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[Any]"
46 [builtins fixtures/exception.pyi]
47
48 [case testTryStarGroupInvalidTuple]
49 try:
50 pass
51 except* (RuntimeError, ExceptionGroup) as e: # E: Exception type in except* cannot derive from BaseExceptionGroup
52 reveal_type(e) # N: Revealed type is "builtins.ExceptionGroup[builtins.RuntimeError | Any]"
53 [builtins fixtures/exception.pyi]
54
55 [case testBasicTypeVarTupleGeneric]
56 from typing import Generic, TypeVarTuple, Unpack
57
58 Ts = TypeVarTuple("Ts")
59
60 class Variadic(Generic[Unpack[Ts]]):
61 ...
62
63 variadic: Variadic[int, str]
64 reveal_type(variadic) # N: Revealed type is "__main__.Variadic[builtins.int, builtins.str]"
65 [builtins fixtures/tuple.pyi]
66
67 [case testAsyncGeneratorWithinComprehension]
68 # flags: --python-version 3.11
69 from typing import Any, Generator, List
70
71 async def asynciter(iterable):
72 for x in iterable:
73 yield x
74
75 async def coro() -> Generator[List[Any], None, None]:
76 return ([i async for i in asynciter([0,j])] for j in [3, 5])
77 reveal_type(coro) # N: Revealed type is "def () -> typing.Coroutine[Any, Any, typing.Generator[builtins.list[Any], None, None]]"
78 [builtins fixtures/async_await.pyi]
79 [typing fixtures/typing-async.pyi]
80
81 [case testTypeVarTupleNewSyntaxAnnotations]
82 Ints = tuple[int, int, int]
83 x: tuple[str, *Ints]
84 reveal_type(x) # N: Revealed type is "tuple[builtins.str, builtins.int, builtins.int, builtins.int]"
85 y: tuple[int, *tuple[int, ...]]
86 reveal_type(y) # N: Revealed type is "tuple[builtins.int, Unpack[builtins.tuple[builtins.int, ...]]]"
87 [builtins fixtures/tuple.pyi]
88
89 [case testTypeVarTupleNewSyntaxGenerics]
90 from typing import Generic, TypeVar, TypeVarTuple
91
92 T = TypeVar("T")
93 Ts = TypeVarTuple("Ts")
94 class C(Generic[T, *Ts]):
95 attr: tuple[int, *Ts, str]
96
97 def test(self) -> None:
98 reveal_type(self.attr) # N: Revealed type is "tuple[builtins.int, Unpack[Ts`2], builtins.str]"
99 self.attr = ci # E: Incompatible types in assignment (expression has type "C[*tuple[int, ...]]", variable has type "tuple[int, *Ts, str]")
100 def meth(self, *args: *Ts) -> T: ...
101
102 ci: C[*tuple[int, ...]]
103 reveal_type(ci) # N: Revealed type is "__main__.C[Unpack[builtins.tuple[builtins.int, ...]]]"
104 reveal_type(ci.meth) # N: Revealed type is "def (*args: builtins.int) -> builtins.int"
105 c3: C[str, str, str]
106 reveal_type(c3) # N: Revealed type is "__main__.C[builtins.str, builtins.str, builtins.str]"
107
108 A = C[int, *Ts]
109 B = tuple[str, *tuple[str, str], str]
110 z: A[*B]
111 reveal_type(z) # N: Revealed type is "__main__.C[builtins.int, builtins.str, builtins.str, builtins.str, builtins.str]"
112 [builtins fixtures/tuple.pyi]
113
114 [case testTypeVarTupleNewSyntaxCallables]
115 from typing import Generic, overload, TypeVar
116
117 T1 = TypeVar("T1")
118 T2 = TypeVar("T2")
119 class MyClass(Generic[T1, T2]):
120 @overload
121 def __init__(self: MyClass[None, None]) -> None: ...
122
123 @overload
124 def __init__(self: MyClass[T1, None], *types: *tuple[type[T1]]) -> None: ...
125
126 @overload
127 def __init__(self: MyClass[T1, T2], *types: *tuple[type[T1], type[T2]]) -> None: ...
128
129 def __init__(self: MyClass[T1, T2], *types: *tuple[type, ...]) -> None:
130 pass
131
132 myclass = MyClass()
133 reveal_type(myclass) # N: Revealed type is "__main__.MyClass[None, None]"
134 myclass1 = MyClass(float)
135 reveal_type(myclass1) # N: Revealed type is "__main__.MyClass[builtins.float, None]"
136 myclass2 = MyClass(float, float)
137 reveal_type(myclass2) # N: Revealed type is "__main__.MyClass[builtins.float, builtins.float]"
138 myclass3 = MyClass(float, float, float) # E: No overload variant of "MyClass" matches argument types "type[float]", "type[float]", "type[float]" \
139 # N: Possible overload variants: \
140 # N: def [T1, T2] MyClass() -> MyClass[None, None] \
141 # N: def [T1, T2] MyClass(type[T1], /) -> MyClass[T1, None] \
142 # N: def [T1, T2] MyClass(type[T1], type[T2], /) -> MyClass[T1, T2]
143 reveal_type(myclass3) # N: Revealed type is "Any"
144 [builtins fixtures/tuple.pyi]
145
146 [case testUnpackNewSyntaxInvalidCallableAlias]
147 from typing import Any, Callable, List, Tuple, TypeVar, Unpack
148
149 T = TypeVar("T")
150 Ts = TypeVarTuple("Ts") # E: Name "TypeVarTuple" is not defined
151
152 def good(*x: int) -> int: ...
153 def bad(*x: int, y: int) -> int: ...
154
155 Alias1 = Callable[[*Ts], int] # E: Variable "__main__.Ts" is not valid as a type \
156 # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
157 x1: Alias1[int] # E: Bad number of arguments for type alias, expected 0, given 1
158 reveal_type(x1) # N: Revealed type is "def (*Any) -> builtins.int"
159 x1 = good
160 x1 = bad # E: Incompatible types in assignment (expression has type "def bad(*x: int, y: int) -> int", variable has type "def (*Any) -> int")
161
162 Alias2 = Callable[[*T], int] # E: "T" cannot be unpacked (must be tuple or TypeVarTuple)
163 x2: Alias2[int]
164 reveal_type(x2) # N: Revealed type is "def (*Any) -> builtins.int"
165
166 Unknown = Any
167 Alias3 = Callable[[*Unknown], int]
168 x3: Alias3[int] # E: Bad number of arguments for type alias, expected 0, given 1
169 reveal_type(x3) # N: Revealed type is "def (*Any) -> builtins.int"
170
171 IntList = List[int]
172 Alias4 = Callable[[*IntList], int] # E: "list[int]" cannot be unpacked (must be tuple or TypeVarTuple)
173 x4: Alias4[int] # E: Bad number of arguments for type alias, expected 0, given 1
174 reveal_type(x4) # N: Revealed type is "def (*Any) -> builtins.int"
175 [builtins fixtures/tuple.pyi]
176
177 [case testReturnInExceptStarBlock1]
178 # flags: --python-version 3.11
179 def foo() -> None:
180 try:
181 pass
182 except* Exception:
183 return # E: "return" not allowed in except* block
184 finally:
185 return
186 [builtins fixtures/exception.pyi]
187
188 [case testReturnInExceptStarBlock2]
189 # flags: --python-version 3.11
190 def foo():
191 while True:
192 try:
193 pass
194 except* Exception:
195 while True:
196 return # E: "return" not allowed in except* block
197 [builtins fixtures/exception.pyi]
198
199 [case testContinueInExceptBlockNestedInExceptStarBlock]
200 # flags: --python-version 3.11
201 while True:
202 try:
203 ...
204 except* Exception:
205 try:
206 ...
207 except Exception:
208 continue # E: "continue" not allowed in except* block
209 continue # E: "continue" not allowed in except* block
210 [builtins fixtures/exception.pyi]
211
212 [case testReturnInExceptBlockNestedInExceptStarBlock]
213 # flags: --python-version 3.11
214 def foo():
215 try:
216 ...
217 except* Exception:
218 try:
219 ...
220 except Exception:
221 return # E: "return" not allowed in except* block
222 return # E: "return" not allowed in except* block
223 [builtins fixtures/exception.pyi]
224
225 [case testBreakContinueReturnInExceptStarBlock1]
226 # flags: --python-version 3.11
227 from typing import Iterable
228 def foo(x: Iterable[int]) -> None:
229 for _ in x:
230 try:
231 pass
232 except* Exception:
233 continue # E: "continue" not allowed in except* block
234 except* Exception:
235 for _ in x:
236 continue
237 break # E: "break" not allowed in except* block
238 except* Exception:
239 return # E: "return" not allowed in except* block
240 [builtins fixtures/exception.pyi]
241
242 [case testBreakContinueReturnInExceptStarBlock2]
243 # flags: --python-version 3.11
244 def foo():
245 while True:
246 try:
247 pass
248 except* Exception:
249 def inner():
250 while True:
251 if 1 < 1:
252 continue
253 else:
254 break
255 return
256 if 1 < 2:
257 break # E: "break" not allowed in except* block
258 if 1 < 2:
259 continue # E: "continue" not allowed in except* block
260 return # E: "return" not allowed in except* block
261 [builtins fixtures/exception.pyi]
262
263 [case testLambdaInExceptStarBlock]
264 # flags: --python-version 3.11
265 def foo():
266 try:
267 pass
268 except* Exception:
269 x = lambda: 0
270 return lambda: 0 # E: "return" not allowed in except* block
271
272 def loop():
273 while True:
274 try:
275 pass
276 except* Exception:
277 x = lambda: 0
278 return lambda: 0 # E: "return" not allowed in except* block
279 [builtins fixtures/exception.pyi]
280
281 [case testRedefineLocalWithinExceptStarTryClauses]
282 # flags: --allow-redefinition-old
283 def fn_str(_: str) -> int: ...
284 def fn_int(_: int) -> None: ...
285 def fn_exc(_: Exception) -> str: ...
286
287 def in_block() -> None:
288 try:
289 a = ""
290 a = fn_str(a) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
291 fn_int(a) # E: Argument 1 to "fn_int" has incompatible type "str"; expected "int"
292 except* Exception:
293 b = ""
294 b = fn_str(b)
295 fn_int(b)
296 else:
297 c = ""
298 c = fn_str(c)
299 fn_int(c)
300 finally:
301 d = ""
302 d = fn_str(d)
303 fn_int(d)
304 reveal_type(a) # N: Revealed type is "builtins.str"
305 reveal_type(b) # N: Revealed type is "builtins.int"
306 reveal_type(c) # N: Revealed type is "builtins.int"
307 reveal_type(d) # N: Revealed type is "builtins.int"
308
309 def across_blocks() -> None:
310 try:
311 a = ""
312 except* Exception:
313 a = fn_str(a) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
314 else:
315 a = fn_str(a) # E: Incompatible types in assignment (expression has type "int", variable has type "str")
316 reveal_type(a) # N: Revealed type is "builtins.str"
317
318 def exc_name() -> None:
319 try:
320 pass
321 except* RuntimeError as e:
322 e = fn_exc(e)
323 [builtins fixtures/exception.pyi]
324
325 [case testGenericTypeAliasArgOfTypeAliasIsUnpackedTuple]
326 from typing_extensions import TypeAlias
327 from typing import TypeVarTuple, Unpack
328
329 Ts = TypeVarTuple('Ts')
330 TA: TypeAlias = tuple[Unpack[Ts]]
331
332 v1: TA[*list[int]] # E: "list[int]" cannot be unpacked (must be tuple or TypeVarTuple)
333
334 v2: TA[Unpack[list[int]]] # E: "list[int]" cannot be unpacked (must be tuple or TypeVarTuple)
335 [builtins fixtures/tuple.pyi]