Debian Codegraph @

test-data/unit/check-basic.test

raw
1 [case testEmptyFile]
2 [out]
3
4 [case testAssignmentAndVarDef]
5 a: A
6 b: B
7 if int():
8 a = a
9 if int():
10 a = b # E: Incompatible types in assignment (expression has type "B", variable has type "A")
11 class A: pass
12 class B: pass
13
14 [case testConstructionAndAssignment]
15 class A:
16 def __init__(self): pass
17 class B:
18 def __init__(self): pass
19
20 x: A
21 x = A()
22 if int():
23 x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
24 [case testInheritInitFromObject]
25 class A(object): pass
26 class B(object): pass
27 x: A
28 if int():
29 x = A()
30 if int():
31 x = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
32 [case testImplicitInheritInitFromObject]
33 class A: pass
34 class B: pass
35 x: A
36 o: object
37 if int():
38 x = o # E: Incompatible types in assignment (expression has type "object", variable has type "A")
39 if int():
40 x = A()
41 if int():
42 o = x
43 [case testTooManyConstructorArgs]
44 import typing
45 object(object())
46 [out]
47 main:2: error: Too many arguments for "object"
48
49 [case testVarDefWithInit]
50 import typing
51 class A: pass
52 a = A() # type: A
53 b = object() # type: A # E: Incompatible types in assignment (expression has type "object", variable has type "A")
54 [case testInheritanceBasedSubtyping]
55 import typing
56 class A: pass
57 class B(A): pass
58 x = B() # type: A
59 y = A() # type: B # E: Incompatible types in assignment (expression has type "A", variable has type "B")
60 [case testDeclaredVariableInParentheses]
61
62 (x) = 2 # type: int
63 if int():
64 x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
65 if int():
66 x = 1
67
68
69 [case testIncompatibleAssignmentAmbiguousShortnames]
70
71 class Any: pass
72 class List: pass
73 class Dict: pass
74 class Iterator: pass
75
76 x = Any()
77 x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "__main__.Any")
78
79 y = List()
80 y = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "__main__.List")
81
82 z = Dict()
83 z = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "__main__.Dict")
84
85 w = Iterator()
86 w = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "__main__.Iterator")
87
88
89 -- Simple functions and calling
90 -- ----------------------------
91
92
93 [case testFunction]
94 import typing
95 class A: pass
96 class B: pass
97 def f(x: 'A') -> None: pass
98 f(A())
99 f(B()) # E: Argument 1 to "f" has incompatible type "B"; expected "A"
100 [case testNotCallable]
101 import typing
102 class A: pass
103 A()() # E: "A" not callable
104 [case testSubtypeArgument]
105 import typing
106 class A: pass
107 class B(A): pass
108 def f(x: 'A', y: 'B') -> None: pass
109 f(B(), A()) # E: Argument 2 to "f" has incompatible type "A"; expected "B"
110 f(B(), B())
111 [case testInvalidArgumentCount]
112 import typing
113 def f(x, y) -> None: pass
114 f(object())
115 f(object(), object(), object())
116 [out]
117 main:3: error: Missing positional argument "y" in call to "f"
118 main:4: error: Too many arguments for "f"
119
120 [case testMissingPositionalArguments]
121 class Foo:
122 def __init__(self, bar: int):
123 pass
124 c = Foo()
125 def foo(baz: int, bas: int):pass
126 foo()
127 [out]
128 main:4: error: Missing positional argument "bar" in call to "Foo"
129 main:6: error: Missing positional arguments "baz", "bas" in call to "foo"
130
131
132 -- Locals
133 -- ------
134
135
136 [case testLocalVariables]
137 def f() -> None:
138 x: A
139 y: B
140 if int():
141 x = x
142 x = y # E: Incompatible types in assignment (expression has type "B", variable has type "A")
143 class A: pass
144 class B: pass
145
146 [case testLocalVariableScope]
147 def f() -> None:
148 x: A
149 x = A()
150 def g() -> None:
151 x: B
152 x = A() # E: Incompatible types in assignment (expression has type "A", variable has type "B")
153 class A: pass
154 class B: pass
155
156 [case testFunctionArguments]
157 import typing
158 def f(x: 'A', y: 'B') -> None:
159 if int():
160 x = y # E: Incompatible types in assignment (expression has type "B", variable has type "A")
161 x = x
162 y = B()
163 class A: pass
164 class B: pass
165
166 [case testLocalVariableInitialization]
167 import typing
168 def f() -> None:
169 a = A() # type: A
170 b = B() # type: A # Fail
171 class A: pass
172 class B: pass
173 [out]
174 main:4: error: Incompatible types in assignment (expression has type "B", variable has type "A")
175
176 [case testVariableInitializationWithSubtype]
177 import typing
178 class A: pass
179 class B(A): pass
180 x = B() # type: A
181 y = A() # type: B # E: Incompatible types in assignment (expression has type "A", variable has type "B")
182
183
184 -- Misc
185 -- ----
186
187
188 [case testInvalidReturn]
189 import typing
190 def f() -> 'A':
191 return B()
192 class A: pass
193 class B: pass
194 [out]
195 main:3: error: Incompatible return value type (got "B", expected "A")
196
197 [case testTopLevelContextAndInvalidReturn]
198 import typing
199 class A: pass
200 class B: pass
201 def f() -> 'A':
202 return B() # E: Incompatible return value type (got "B", expected "A")
203 a = B() # type: A # E: Incompatible types in assignment (expression has type "B", variable has type "A")
204 [case testEmptyReturnInAnyTypedFunction]
205 from typing import Any
206 def f() -> Any:
207 return
208
209 [case testEmptyYieldInAnyTypedFunction]
210 from typing import Any
211 def f() -> Any:
212 yield
213
214 [case testModuleImplicitAttributes]
215 import typing
216 class A: pass
217 reveal_type(__name__) # N: Revealed type is "builtins.str"
218 reveal_type(__doc__) # N: Revealed type is "builtins.str"
219 reveal_type(__file__) # N: Revealed type is "builtins.str"
220 reveal_type(__package__) # N: Revealed type is "builtins.str"
221 reveal_type(__annotations__) # N: Revealed type is "builtins.dict[builtins.str, Any]"
222 # This will actually reveal Union[importlib.machinery.ModuleSpec, None]
223 reveal_type(__spec__) # N: Revealed type is "builtins.object | None"
224
225 import module
226 reveal_type(module.__name__) # N: Revealed type is "builtins.str"
227 # This will actually reveal importlib.machinery.ModuleSpec
228 reveal_type(module.__spec__) # N: Revealed type is "builtins.object"
229 [file module.py]
230 [builtins fixtures/primitives.pyi]
231
232
233 -- Scoping and shadowing
234 -- ---------------------
235
236
237 [case testLocalVariableShadowing]
238 class A: pass
239 class B: pass
240 a: A
241 if int():
242 a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
243 a = A()
244 def f() -> None:
245 a: B
246 if int():
247 a = A() # E: Incompatible types in assignment (expression has type "A", variable has type "B")
248 a = B()
249 a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
250 a = A()
251 [case testGlobalDefinedInBlockWithType]
252 class A: pass
253 while 1:
254 a: A
255 if int():
256 a = A()
257 a = object() # E: Incompatible types in assignment (expression has type "object", variable has type "A")
258
259
260 -- # type: signatures
261 -- ------------------
262
263
264 [case testFunctionSignatureAsComment]
265 def f(x): # type: (int) -> str
266 return 1
267 f('')
268 [out]
269 main:2: error: Incompatible return value type (got "int", expected "str")
270 main:3: error: Argument 1 to "f" has incompatible type "str"; expected "int"
271
272 [case testMethodSignatureAsComment]
273 class A:
274 def f(self, x):
275 # type: (int) -> str
276 self.f('') # Fail
277 return 1
278 A().f('') # Fail
279 [out]
280 main:4: error: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
281 main:5: error: Incompatible return value type (got "int", expected "str")
282 main:6: error: Argument 1 to "f" of "A" has incompatible type "str"; expected "int"
283
284 [case testTrailingCommaParsing]
285 x = 1
286 x in 1, # E: Unsupported right operand type for in ("int")
287 [builtins fixtures/tuple.pyi]
288
289 [case testTrailingCommaInIfParsing_no_native_parse]
290 if x in 1, : pass
291 [out]
292 main:1: error: Invalid syntax
293
294 [case testInitReturnTypeError]
295 class C:
296 def __init__(self):
297 # type: () -> int
298 pass
299 [out]
300 main:2: error: The return type of "__init__" must be None
301
302 -- WritesCache signals to testcheck to do the cache validation
303 [case testWritesCache-writescache]
304 import a
305 import d
306 [file a.py]
307 import b
308 import c
309 [file b.py]
310 [file c.py]
311 [file d.py]
312
313 [case testWritesCacheErrors-writescache]
314 import a
315 import d
316 [file a.py]
317 import b
318 import c
319 [file b.py]
320 [file c.py]
321 [file d.py]
322 import e
323 [file e.py]
324 1+'no' # E: Unsupported operand types for + ("int" and "str")
325
326 [case testModuleAsTypeNoCrash]
327
328 import mock
329 from typing import Union
330
331 class A: ...
332 class B: ...
333
334 x: Union[mock, A] # E: Module "mock" is not valid as a type \
335 # N: Perhaps you meant to use a protocol matching the module structure?
336
337 if isinstance(x, B):
338 pass
339 [file mock.py]
340 [builtins fixtures/isinstance.pyi]
341 [out]
342
343 [case testModuleAsTypeNoCrash2]
344
345 import mock
346 from typing import overload, Any, Union
347
348 @overload
349 def f(x: int) -> int: ...
350 @overload
351 def f(x: str) -> Union[mock, str]: ... # E: Module "mock" is not valid as a type \
352 # N: Perhaps you meant to use a protocol matching the module structure?
353 def f(x):
354 pass
355
356 x: Any
357 f(x)
358 [file mock.py]
359 [builtins fixtures/isinstance.pyi]
360 [out]
361
362 [case testPartialTypeComments]
363 def foo(
364 a, # type: str
365 b,
366 args=None,
367 ):
368 # type: (...) -> None
369 pass
370
371 [case testNoneHasBool]
372 def main() -> None:
373 none = None
374 b = none.__bool__()
375 reveal_type(b) # N: Revealed type is "Literal[False]"
376 [builtins fixtures/bool.pyi]
377
378 [case testAssignmentInvariantNoteForList]
379 from typing import List
380 x: List[int]
381 y: List[float]
382 y = x # E: Incompatible types in assignment (expression has type "list[int]", variable has type "list[float]") \
383 # N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
384 # N: Consider using "Sequence" instead, which is covariant
385 [builtins fixtures/list.pyi]
386
387 [case testAssignmentInvariantNoteForDict]
388 from typing import Dict
389 x: Dict[str, int]
390 y: Dict[str, float]
391 y = x # E: Incompatible types in assignment (expression has type "dict[str, int]", variable has type "dict[str, float]") \
392 # N: "dict" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \
393 # N: Consider using "Mapping" instead, which is covariant in the value type
394 [builtins fixtures/dict.pyi]
395
396 [case testDistinctTypes]
397 import b
398
399 [file a.py]
400 from typing import NamedTuple, TypedDict
401 from enum import Enum
402 class A: pass
403 N = NamedTuple('N', [('x', int)])
404 D = TypedDict('D', {'x': int})
405 class B(Enum):
406 b = 10
407
408 [file b.py]
409 from typing import Final, List, Literal, Optional, Union, Sequence, NamedTuple, Tuple, Type, TypedDict
410 from enum import Enum
411 import a
412 class A: pass
413 N = NamedTuple('N', [('x', int)])
414 class B(Enum):
415 b = 10
416 D = TypedDict('D', {'y': int})
417
418 def foo() -> Optional[A]:
419 b = True
420 return a.A() if b else None # E: Incompatible return value type (got "a.A | None", expected "b.A | None")
421
422 def bar() -> List[A]:
423 l = [a.A()]
424 return l # E: Incompatible return value type (got "list[a.A]", expected "list[b.A]")
425
426 def baz() -> Union[A, int]:
427 b = True
428 return a.A() if b else 10 # E: Incompatible return value type (got "a.A | int", expected "b.A | int")
429
430 def spam() -> Optional[A]:
431 return a.A() # E: Incompatible return value type (got "a.A", expected "b.A | None")
432
433 def eggs() -> Sequence[A]:
434 x = [a.A()]
435 return x # E: Incompatible return value type (got "list[a.A]", expected "Sequence[b.A]")
436
437 def eggs2() -> Sequence[N]:
438 x = [a.N(0)]
439 return x # E: Incompatible return value type (got "list[a.N]", expected "Sequence[b.N]")
440
441 def asdf1() -> Sequence[Tuple[a.A, A]]:
442 x = [(a.A(), a.A())]
443 return x # E: Incompatible return value type (got "list[tuple[a.A, a.A]]", expected "Sequence[tuple[a.A, b.A]]")
444
445 def asdf2() -> Sequence[Tuple[A, a.A]]:
446 x = [(a.A(), a.A())]
447 return x # E: Incompatible return value type (got "list[tuple[a.A, a.A]]", expected "Sequence[tuple[b.A, a.A]]")
448
449 def arg() -> Tuple[A, A]:
450 return A() # E: Incompatible return value type (got "A", expected "tuple[A, A]")
451
452 def types() -> Sequence[Type[A]]:
453 x = [a.A]
454 return x # E: Incompatible return value type (got "list[type[a.A]]", expected "Sequence[type[b.A]]")
455
456 def literal() -> Sequence[Literal[B.b]]:
457 x = [a.B.b] # type: List[Literal[a.B.b]]
458 return x # E: Incompatible return value type (got "list[Literal[a.B.b]]", expected "Sequence[Literal[b.B.b]]")
459
460 def typeddict() -> Sequence[D]:
461 x = [{'x': 0}] # type: List[a.D]
462 return x # E: Incompatible return value type (got "list[a.D]", expected "Sequence[b.D]")
463
464 a = (a.A(), A())
465 a.x # E: "tuple[a.A, b.A]" has no attribute "x"
466 [builtins fixtures/dict.pyi]
467 [typing fixtures/typing-full.pyi]
468
469 [case testReturnAnyFromFunctionDeclaredToReturnObject]
470 # flags: --warn-return-any
471 from typing import Any
472
473 def f() -> object:
474 x: Any = 1
475 return x
476
477 [case testImportModuleAsClassMember]
478 import test
479
480 class A:
481 def __init__(self) -> None:
482 self.test = test
483
484 def __call__(self) -> None:
485 self.test.foo("Message")
486
487 [file test.py]
488 def foo(s: str) -> None: ...
489
490 [case testLocalImportModuleAsClassMember]
491 class A:
492 def __init__(self) -> None:
493 import test
494
495 self.test = test
496
497 def __call__(self) -> None:
498 self.test.foo("Message")
499
500 [file test.py]
501 def foo(s: str) -> None: ...
502
503 [case testInlineAssertions]
504 import a, b
505 s1: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
506 [file a.py]
507 s2: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
508 [file b.py]
509 s3: str = 42 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
510 [file c.py]
511 s3: str = 'foo'
512
513 [case testMultilineQuotedAnnotation]
514 x: """
515
516 int |
517 str
518
519 """
520 reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
521
522 y: """(
523 int |
524 str
525 )
526 """
527 reveal_type(y) # N: Revealed type is "builtins.int | builtins.str"