Debian Codegraph @

test-data/unit/check-modules.test

raw ◀ ▶
1 Debian patch touch this file
1 -- Type checker test cases dealing with modules and imports.
2 -- Towards the end there are tests for PEP 420 (namespace packages, i.e. __init__.py-less packages).
3
4 [case testAccessImportedDefinitions0]
5 import m
6 import typing
7 m.f() # E: Missing positional argument "a" in call to "f"
8 m.f(object()) # E: Argument 1 to "f" has incompatible type "object"; expected "A"
9 m.x = object() # E: Incompatible types in assignment (expression has type "object", variable has type "A")
10 m.f(m.A())
11 m.x = m.A()
12 [file m.py]
13 class A: pass
14 def f(a: A) -> None: pass
15 x = A()
16
17 [case testAccessImportedDefinitions1]
18 import m
19 import typing
20 m.f(object()) # E: Argument 1 to "f" has incompatible type "object"; expected "A"
21 m.f(m.A())
22 [file m.py]
23 class A: pass
24 def f(a: A) -> None: pass
25
26 [case testAccessImportedDefinitions2]
27 from m import f, A
28 import typing
29 f(object()) # E: Argument 1 to "f" has incompatible type "object"; expected "A"
30 f(A())
31 [file m.py]
32 class A: pass
33 def f(a: A) -> None: pass
34
35 [case testImportedExceptionType]
36 import m
37 import typing
38 try:
39 pass
40 except m.Err:
41 pass
42 except m.Bad: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
43 pass
44 [file m.py]
45 class Err(BaseException): pass
46 class Bad: pass
47 [builtins fixtures/exception.pyi]
48
49 [case testImportedExceptionType2]
50 from m import Err, Bad
51 import typing
52 try:
53 pass
54 except Err:
55 pass
56 except Bad: # E: Exception type must be derived from BaseException (or be a tuple of exception classes)
57 pass
58 [file m.py]
59 class Err(BaseException): pass
60 class Bad: pass
61 [builtins fixtures/exception.pyi]
62
63 [case testImportWithinBlock]
64 import typing
65 if 1:
66 import m
67 m.a = m.b # E: Incompatible types in assignment (expression has type "B", variable has type "A")
68 m.a = m.a
69 m.f()
70 m.f(m.a) # E: Too many arguments for "f"
71 m.a = m.A()
72 m.a = m.B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
73 [file m.py]
74 class A: pass
75 class B: pass
76 a = A()
77 b = B()
78 def f() -> None: pass
79
80 [case testImportWithinFunction]
81 import typing
82 def f() -> None:
83 from m import a, b, f, A, B
84 if int():
85 a = b \
86 # E: Incompatible types in assignment (expression has type "B", variable has type "A")
87 a = a
88 f()
89 f(a) # E: Too many arguments for "f"
90 a = A()
91 a = B() \
92 # E: Incompatible types in assignment (expression has type "B", variable has type "A")
93 [file m.py]
94 class A: pass
95 class B: pass
96 a = A()
97 b = B()
98 def f() -> None: pass
99 [out]
100
101 [case testImportWithinMethod]
102 import typing
103 class C:
104 def f(self) -> None:
105 from m import *
106 if int():
107 a = b # E: Incompatible types in assignment (expression has type "B", variable has type "A")
108 a = a
109 f()
110 f(a) # E: Too many arguments for "f"
111 a = A()
112 a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
113 [file m.py]
114 class A: pass
115 class B: pass
116 a = A()
117 b = B()
118 def f() -> None: pass
119 [out]
120
121 [case testImportWithinClassBody]
122 import typing
123 class C:
124 import m
125 m.f()
126 m.f(C) # E: Too many arguments for "f"
127 [file m.py]
128 def f() -> None: pass
129 [out]
130
131 [case testImportWithinClassBody2]
132 import typing
133 class C:
134 from m import f # E: Unsupported class scoped import
135 f()
136 # ideally, the following should error:
137 f(C)
138 [file m.py]
139 def f() -> None: pass
140 [out]
141
142 [case testImportWithStub]
143 import _m
144 _m.f("hola")
145 [file _m.pyi]
146 def f(c:str) -> None: pass
147 [out]
148
149 [case testImportWithStubIncompatibleType]
150 import _m
151 _m.f("hola")
152 _m.f(12) # E: Argument 1 to "f" has incompatible type "int"; expected "str"
153 [file _m.py]
154 def f(c):
155 print(c)
156 [file _m.pyi]
157 def f(c:str) -> None: pass
158
159 [case testInvalidOperationsOnModules]
160 import m
161 import typing
162
163 class A: pass
164 m() # E: Module not callable
165 a = m # type: A # E: Incompatible types in assignment (expression has type Module, variable has type "A")
166 m + None # E: Unsupported left operand type for + (Module)
167 [file m.py]
168 [builtins fixtures/module.pyi]
169
170 [case testNameDefinedInDifferentModule]
171 import m, n
172 import typing
173 m.x # E: Module has no attribute "x"
174 [file m.py]
175 y = object()
176 [file n.py]
177 x = object()
178 [builtins fixtures/module.pyi]
179
180 [case testChainedAssignmentAndImports]
181 import m
182
183 i: int
184 s: str
185 if int():
186 i = m.x
187 if int():
188 i = m.y
189 if int():
190 s = m.x # E: Incompatible types in assignment (expression has type "int", variable has type "str")
191 if int():
192 s = m.y # E: Incompatible types in assignment (expression has type "int", variable has type "str")
193 [file m.py]
194 x = y = 1
195 [builtins fixtures/primitives.pyi]
196
197 [case testConditionalFunctionDefinitionAndImports]
198 import m
199 import typing
200 m.f(1)
201 m.f('x') # E: Argument 1 to "f" has incompatible type "str"; expected "int"
202 [file m.py]
203 x = object()
204 if x:
205 def f(x: int) -> None: pass
206 else:
207 def f(x: int) -> None: pass
208
209 [case testTypeCheckWithUnknownModule]
210 import nonexistent
211 None + ''
212 [out]
213 main:1: error: Cannot find implementation or library stub for module named "nonexistent"
214 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
215 main:2: error: Unsupported left operand type for + ("None")
216
217 [case testTypeCheckWithUnknownModule2]
218 import m, nonexistent
219 None + ''
220 m.x = 1
221 m.x = ''
222 [file m.py]
223 x = 1
224 [out]
225 main:1: error: Cannot find implementation or library stub for module named "nonexistent"
226 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
227 main:2: error: Unsupported left operand type for + ("None")
228 main:4: error: Incompatible types in assignment (expression has type "str", variable has type "int")
229
230 [case testTypeCheckWithUnknownModule3]
231 import nonexistent, m
232 None + ''
233 m.x = 1
234 m.x = ''
235 [file m.py]
236 x = 1
237 [out]
238 main:1: error: Cannot find implementation or library stub for module named "nonexistent"
239 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
240 main:2: error: Unsupported left operand type for + ("None")
241 main:4: error: Incompatible types in assignment (expression has type "str", variable has type "int")
242
243 [case testTypeCheckWithUnknownModule4]
244 import nonexistent, another
245 None + ''
246 [out]
247 main:1: error: Cannot find implementation or library stub for module named "nonexistent"
248 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
249 main:1: error: Cannot find implementation or library stub for module named "another"
250 main:2: error: Unsupported left operand type for + ("None")
251
252 [case testTypeCheckWithUnknownModule5]
253 import nonexistent as x
254 None + ''
255 [out]
256 main:1: error: Cannot find implementation or library stub for module named "nonexistent"
257 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
258 main:2: error: Unsupported left operand type for + ("None")
259
260 [case testTypeCheckWithUnknownModuleUsingFromImport]
261 from nonexistent import x
262 None + ''
263 [out]
264 main:1: error: Cannot find implementation or library stub for module named "nonexistent"
265 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
266 main:2: error: Unsupported left operand type for + ("None")
267
268 [case testTypeCheckWithUnknownModuleUsingImportStar]
269 from nonexistent import *
270 None + ''
271 [out]
272 main:1: error: Cannot find implementation or library stub for module named "nonexistent"
273 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
274 main:2: error: Unsupported left operand type for + ("None")
275
276 [case testAccessingUnknownModule]
277 import xyz
278 xyz.foo()
279 xyz()
280 [out]
281 main:1: error: Cannot find implementation or library stub for module named "xyz"
282 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
283
284 [case testAccessingUnknownModule2]
285 import xyz, bar
286 xyz.foo()
287 bar()
288 [out]
289 main:1: error: Cannot find implementation or library stub for module named "xyz"
290 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
291 main:1: error: Cannot find implementation or library stub for module named "bar"
292
293 [case testAccessingUnknownModule3]
294 import xyz as z
295 xyz.foo()
296 z()
297 [out]
298 main:1: error: Cannot find implementation or library stub for module named "xyz"
299 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
300 main:2: error: Name "xyz" is not defined
301
302 [case testAccessingNameImportedFromUnknownModule]
303 from xyz import y, z
304 y.foo()
305 z()
306 [out]
307 main:1: error: Cannot find implementation or library stub for module named "xyz"
308 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
309
310 [case testAccessingNameImportedFromUnknownModule2]
311 from xyz import *
312 y
313 [out]
314 main:1: error: Cannot find implementation or library stub for module named "xyz"
315 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
316 main:2: error: Name "y" is not defined
317
318 [case testAccessingNameImportedFromUnknownModule3]
319 from xyz import y as z
320 y
321 z
322 [out]
323 main:1: error: Cannot find implementation or library stub for module named "xyz"
324 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
325 main:2: error: Name "y" is not defined
326
327 [case testUnknownModuleRedefinition]
328 # Error messages differ with the new analyzer
329
330 import xab # E: Cannot find implementation or library stub for module named "xab" # N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
331 def xab(): pass # E: Name "xab" already defined (possibly by an import)
332
333 [case testAccessingUnknownModuleFromOtherModule]
334 import x
335 x.nonexistent.foo
336 x.z # E: Module has no attribute "z"
337 [file x.py]
338 import nonexistent # E: Cannot find implementation or library stub for module named "nonexistent" \
339 # N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
340 [builtins fixtures/module.pyi]
341
342 [case testUnknownModuleImportedWithinFunction]
343 def f():
344 import foobar
345 def foobar(): pass
346 foobar('')
347 [out]
348 main:2: error: Cannot find implementation or library stub for module named "foobar"
349 main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
350 main:4: error: Too many arguments for "foobar"
351
352 [case testUnknownModuleImportedWithinFunction2]
353 def f():
354 from foobar import x
355 def x(): pass
356 x('')
357 [out]
358 main:2: error: Cannot find implementation or library stub for module named "foobar"
359 main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
360 main:4: error: Too many arguments for "x"
361
362 [case testRelativeImports]
363 import typing
364 import m.a
365 m.a.x = m.a.y # E: Incompatible types in assignment (expression has type "B", variable has type "A")
366 [file m/__init__.py]
367 [file m/a.py]
368 import typing
369 from .b import A, B, x, y
370 z = x
371 if int():
372 z = y # E: Incompatible types in assignment (expression has type "B", variable has type "A")
373 [file m/b.py]
374 import typing
375 class A: pass
376 class B: pass
377 x = A()
378 y = B()
379
380 [case testRelativeImports2]
381 import typing
382 import m.a
383 m.a.x = m.a.y # E: Incompatible types in assignment (expression has type "B", variable has type "A")
384 [file m/__init__.py]
385 [file m/a.py]
386 import typing
387 from .b import A, B, x, y
388 [file m/b.py]
389 import typing
390 class A: pass
391 class B: pass
392 x = A()
393 y = B()
394
395 [case testExportedValuesInImportAll]
396 import typing
397 from m import *
398 _ = a
399 _ = b
400 _ = c
401 _ = d
402 _ = e # E: Name "e" is not defined
403 _ = f
404 _ = _g # E: Name "_g" is not defined
405 [file m.py]
406 __all__ = ['a']
407 __all__ += ('b',)
408 __all__.append('c')
409 __all__.extend(('d', 'e', 'f'))
410 __all__.remove('e')
411
412 a = b = c = d = e = f = _g = 1
413 [builtins fixtures/module_all.pyi]
414
415 [case testAllMustBeSequenceStr]
416 import typing
417 __all__ = [1, 2, 3]
418 [builtins fixtures/module_all.pyi]
419 [out]
420 main:2: error: List item 0 has incompatible type "int"; expected "str"
421 main:2: error: List item 1 has incompatible type "int"; expected "str"
422 main:2: error: List item 2 has incompatible type "int"; expected "str"
423
424 [case testAllMustBeSequenceStr2]
425 import typing
426 __all__ = 1 # E: Type of __all__ must be "Sequence[str]", not "int"
427 reveal_type(__all__) # N: Revealed type is "builtins.int"
428 [builtins fixtures/module_all.pyi]
429
430 [case testAllMustBeSequenceStr3]
431 import typing
432 __all__ = set() # E: Need type annotation for "__all__" (hint: "__all__: set[<type>] = ...") \
433 # E: Type of __all__ must be "Sequence[str]", not "set[Any]"
434 reveal_type(__all__) # N: Revealed type is "builtins.set[Any]"
435 [builtins fixtures/set.pyi]
436
437 [case testModuleAllEmptyList]
438 __all__ = []
439 reveal_type(__all__) # N: Revealed type is "builtins.list[builtins.str]"
440 [builtins fixtures/module_all.pyi]
441
442 [case testDunderAllNotGlobal]
443 class A:
444 __all__ = 1
445
446 def foo() -> None:
447 __all__ = 1
448 [builtins fixtures/module_all.pyi]
449
450 [case testUnderscoreExportedValuesInImportAll]
451 import typing
452 from m import *
453 _ = a
454 _ = _b
455 _ = __c__
456 _ = ___d
457 _ = e
458 _ = f # E: Name "f" is not defined
459 _ = _g # E: Name "_g" is not defined
460 [file m.py]
461 __all__ = ['a']
462 __all__ += ('_b',)
463 __all__.append('__c__')
464 __all__.extend(('___d', 'e'))
465
466 a = _b = __c__ = ___d = e = f = _g = 1
467 [builtins fixtures/module_all.pyi]
468
469 [case testEllipsisInitializerInStubFileWithType]
470 import m
471 m.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
472 [file m.pyi]
473 x = ... # type: int
474
475 [case testEllipsisInitializerInStubFileWithoutType]
476 import m
477 m.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "ellipsis")
478 [file m.pyi]
479 # Ellipsis is only special with a # type: comment (not sure though if this is great)
480 x = ...
481
482 [case testEllipsisInitializerInModule]
483 x = ... # type: int # E: Incompatible types in assignment (expression has type "ellipsis", variable has type "int")
484
485 [case testEllipsisDefaultArgValueInStub]
486 import m
487 m.f(1)
488 m.f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int"
489 [file m.pyi]
490 def f(x: int = ...) -> None: pass
491
492 [case testEllipsisDefaultParamValueInStub2]
493 import m
494 def f1(x: int = ...) -> int: return 1 # E: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "int")
495 def f2(x: int = '') -> int: return 1 # E: Incompatible default for parameter "x" (default has type "str", parameter has type "int")
496 [file m.pyi]
497 def g1(x: int = ...) -> int: pass
498 def g2(x: int = '') -> int: pass # E: Incompatible default for parameter "x" (default has type "str", parameter has type "int")
499
500 [case testEllipsisDefaultParamValueInNonStub]
501 def ok_1(x: int = ...) -> None: pass
502 def ok_2(x: int = ...) -> None: ...
503 def ok_3(x: int = ...) -> None: raise NotImplementedError
504 def ok_4(x: int = ...) -> None: raise NotImplementedError()
505 def ok_5(x: int = ...) -> None:
506 """Docstring here"""
507 pass
508
509 def bad_1(x: int = ...) -> None: 1 # E: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "int")
510 def bad_2(x: int = ...) -> None: # E: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "int")
511 """Docstring here"""
512 ok_1()
513 def bad_3(x: int = ...) -> None: # E: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "int")
514 raise Exception("Some other exception")
515 [builtins fixtures/exception.pyi]
516
517 [case testEllipsisDefaultParamValueInNonStubsOverload]
518 from typing import overload, Union
519
520 Both = Union[int, str]
521
522 @overload
523 def foo(x: int, y: int = ...) -> int: ...
524 @overload
525 def foo(x: str, y: str = ...) -> str: ...
526 def foo(x: Both, y: Both = ...) -> Both: # E: Incompatible default for parameter "y" (default has type "ellipsis", parameter has type "int | str")
527 return x
528
529 @overload
530 def bar(x: int, y: int = ...) -> int: ...
531 @overload
532 def bar(x: str, y: str = ...) -> str: ...
533 def bar(x: Both, y: Both = ...) -> Both:
534 raise NotImplementedError
535 [builtins fixtures/exception.pyi]
536
537 [case testEllipsisDefaultParamValueInNonStubsMethods]
538 from typing import Generic, Protocol, TypeVar
539 from abc import abstractmethod
540
541 T = TypeVar('T')
542 class Wrap(Generic[T]): ...
543
544 class MyProtocol(Protocol):
545 def no_impl(self, x: Wrap[int] = ...) -> int: ...
546 def default_impl(self, x: Wrap[int] = ...) -> int: return 3 # E: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "Wrap[int]")
547
548 class MyAbstractClass:
549 @abstractmethod
550 def no_impl(self, x: Wrap[int] = ...) -> int: raise NotImplementedError
551
552 @abstractmethod
553 def default_impl(self, x: Wrap[int] = ...) -> int: return 3 # E: Incompatible default for parameter "x" (default has type "ellipsis", parameter has type "Wrap[int]")
554 [builtins fixtures/exception.pyi]
555
556 [case testStarImportOverlapping]
557 from m1 import *
558 from m2 import *
559 j = ''
560 [file m1.py]
561 x = 1
562 [file m2.py]
563 x = 1
564
565 [case testStarImportOverlappingMismatch]
566 from m1 import *
567 from m2 import * # E: Incompatible import of "x" (imported name has type "int", local name has type "str")
568 j = ''
569 [file m1.py]
570 x = ''
571 [file m2.py]
572 x = 1
573
574 [case testStarImportOverridingLocalImports]
575 from m1 import *
576 from m2 import *
577 x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
578 [file m1.py]
579 x = 1
580 [file m2.py]
581 x = 1
582
583 [case testStarImportDoesNotImportGeneratedIntersections]
584 from m1 import *
585 from m2 import *
586 [file m1.py]
587 from m3 import A, B
588
589 a: A
590 if isinstance(a, B): ...
591 [file m2.py]
592 from m3 import A, B
593
594 a: A
595 if isinstance(a, B): ...
596 [file m3.py]
597 class A: ...
598 class B: ...
599 class AB(A, B): ...
600 [builtins fixtures/isinstance.pyi]
601
602 [case testAssignToFuncDefViaImport]
603
604 # Errors differ with the new analyzer. (Old analyzer gave error on the
605 # input, which is maybe better, but no error about f, which seems
606 # wrong)
607 from m import *
608 f = None # E: Incompatible types in assignment (expression has type "None", variable has type "Callable[[], Any]")
609 x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
610 [file m.py]
611 def f(): pass
612 x = 1+0
613 [out]
614
615
616 -- Conditional definitions and function redefinitions via module object
617 -- --------------------------------------------------------------------
618
619
620 [case testConditionalImportAndAssign]
621 # flags: --no-strict-optional
622 try:
623 from m import x
624 except:
625 x = None
626 try:
627 from m import x as y
628 except:
629 y = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
630 [file m.py]
631 x = ''
632
633 [case testAssignAndConditionalImport]
634 x = ''
635 try:
636 from m import x
637 except:
638 pass
639 y = 1
640 try:
641 from m import x as y # E: Incompatible import of "y" (imported name has type "str", local name has type "int")
642 except:
643 pass
644 [file m.py]
645 x = ''
646
647 [case testAssignAndConditionalStarImport]
648 x = ''
649 y = 1
650 try:
651 from m import * # E: Incompatible import of "y" (imported name has type "str", local name has type "int")
652 except:
653 pass
654 [file m.py]
655 x = ''
656 y = ''
657
658 [case testRedefineImportedFunctionViaImport]
659 try:
660 from m import f, g
661 except:
662 def f(x): pass
663 def g(x): pass # E: All conditional function variants must have identical signatures \
664 # N: Original: \
665 # N: def g(x: Any, y: Any) -> Any \
666 # N: Redefinition: \
667 # N: def g(x: Any) -> Any
668 [file m.py]
669 def f(x): pass
670 def g(x, y): pass
671
672 [case testImportedVariableViaImport]
673 try:
674 from m import x
675 except:
676 from n import x # E: Incompatible import of "x" (imported name has type "str", local name has type "int")
677 [file m.py]
678 x = 1
679 [file n.py]
680 x = ''
681
682 [case testRedefineFunctionViaImport]
683 def f(x): pass
684 def g(x): pass
685 try:
686 from m import f, g # E: Incompatible import of "g" (imported name has type "Callable[[Any, Any], Any]", local name has type "Callable[[Any], Any]")
687 except:
688 pass
689
690 import m as f # E: Incompatible import of "f" (imported name has type "object", local name has type "Callable[[Any], Any]")
691
692 [file m.py]
693 def f(x): pass
694 def g(x, y): pass
695
696 [case testRedefineTypeViaImport]
697 from typing import Type
698 import mod
699
700 X: Type[mod.A]
701 Y: Type[mod.B]
702 from mod import B as X
703 from mod import A as Y # E: Incompatible import of "Y" (imported name has type "type[A]", local name has type "type[B]")
704
705 import mod as X # E: Incompatible import of "X" (imported name has type "object", local name has type "type[A]")
706
707 [file mod.py]
708 class A: ...
709 class B(A): ...
710
711
712 [case testImportVariableAndAssignNone]
713 # flags: --no-strict-optional
714 try:
715 from m import x
716 except:
717 x = None
718 [file m.py]
719 x = 1
720
721 [case testImportFunctionAndAssignNone]
722 # flags: --no-strict-optional
723 try:
724 from m import f
725 except:
726 f = None
727 [file m.py]
728 def f(): pass
729
730 [case testImportFunctionAndAssignFunction]
731 def g(x): pass
732 try:
733 from m import f
734 except:
735 f = g
736 [file m.py]
737 def f(x): pass
738
739 [case testImportFunctionAndAssignIncompatible]
740 try:
741 from m import f
742 except:
743 f = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Callable[[], Any]")
744 [file m.py]
745 def f(): pass
746
747 [case testAssignToFuncDefViaGlobalDecl2]
748 # flags: --no-strict-optional
749 import typing
750 from m import f
751 def g() -> None:
752 global f
753 f = None
754 if int():
755 f = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Callable[[], Any]")
756 [file m.py]
757 def f(): pass
758 [out]
759
760 [case testAssignToFuncDefViaNestedModules]
761 # flags: --no-strict-optional
762 import m.n
763 m.n.f = None
764 m.n.f = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Callable[[], Any]")
765 [file m/__init__.py]
766 [file m/n.py]
767 def f(): pass
768 [out]
769
770 [case testAssignToFuncDefViaModule]
771 # flags: --no-strict-optional
772 import m
773 m.f = None
774 m.f = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Callable[[], Any]")
775 [file m.py]
776 def f(): pass
777 [out]
778
779 [case testConditionalImportAndAssignNoneToModule]
780 # flags: --no-strict-optional
781 if object():
782 import m
783 else:
784 m = None
785 m.f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "str"
786 [file m.py]
787 def f(x: str) -> None: pass
788 [builtins fixtures/module.pyi]
789 [out]
790
791 [case testConditionalImportAndAssignInvalidToModule]
792 if object():
793 import m
794 else:
795 m = 1 # E: Incompatible types in assignment (expression has type "int", variable has type Module)
796 [file m.py]
797 [builtins fixtures/module.pyi]
798 [out]
799
800 [case testImportAndAssignToModule]
801 # flags: --no-strict-optional
802 import m
803 m = None
804 m.f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "str"
805 [file m.py]
806 def f(x: str) -> None: pass
807 [builtins fixtures/module.pyi]
808 [out]
809
810
811 -- Test cases that simulate 'mypy -m modname'
812 --
813 -- The module name to import is encoded in a comment.
814
815 [case testTypeCheckNamedModule]
816 # cmd: mypy -m m.a
817 [file m/__init__.py]
818 None + 1
819 [file m/a.py]
820 [out]
821 tmp/m/__init__.py:1: error: Unsupported left operand type for + ("None")
822
823 [case testTypeCheckNamedModule2]
824 # cmd: mypy -m m.a
825 [file m/__init__.py]
826 [file m/a.py]
827 None + 1
828 [out]
829 tmp/m/a.py:1: error: Unsupported left operand type for + ("None")
830
831 [case testTypeCheckNamedModule3]
832 # cmd: mypy -m m
833 [file m/__init__.py]
834 None + 1
835 [file m/a.py]
836 [out]
837 tmp/m/__init__.py:1: error: Unsupported left operand type for + ("None")
838
839 [case testTypeCheckNamedModule4]
840 # cmd: mypy -m m
841 [file m/__init__.py]
842 [file m/a.py]
843 None + 1 # Not analyzed.
844 [out]
845
846 [case testTypeCheckNamedModule5]
847 # cmd: mypy -m m
848 None + '' # Not analyzed.
849 [file m.py]
850 None + 1
851 [out]
852 tmp/m.py:1: error: Unsupported left operand type for + ("None")
853
854 [case testTypeCheckNamedModuleWithImportCycle]
855 # cmd: mypy -m m.a
856 None + 1 # Does not generate error, as this file won't be analyzed.
857 [file m/__init__.py]
858 import m.a
859 [file m/a.py]
860 [out]
861
862 [case testCheckDecoratedFuncAsAnnotWithImportCycle]
863
864 import a
865 [file a.py]
866 from typing import TypeVar
867 import b
868
869 T = TypeVar('T')
870 def idf(x: T) -> T: return x
871
872 @idf
873 def Session() -> None: pass
874
875 [file b.py]
876 MYPY = False
877 if MYPY:
878 from a import Session
879
880 def f(self, session: Session) -> None: # E: Function "a.Session" is not valid as a type \
881 # N: Perhaps you need "Callable[...]" or a callback protocol?
882 pass
883 [builtins fixtures/bool.pyi]
884
885
886 -- Checks dealing with submodules and different kinds of imports
887 -- -------------------------------------------------------------
888
889 [case testSubmoduleRegularImportAddsAllParents]
890 import a.b.c
891 reveal_type(a.value) # N: Revealed type is "builtins.int"
892 reveal_type(a.b.value) # N: Revealed type is "builtins.str"
893 reveal_type(a.b.c.value) # N: Revealed type is "builtins.float"
894 b.value # E: Name "b" is not defined
895 c.value # E: Name "c" is not defined
896
897 [file a/__init__.py]
898 value = 3
899 [file a/b/__init__.py]
900 value = "a"
901 [file a/b/c.py]
902 value = 3.2
903 [out]
904
905 [case testSubmoduleImportAsDoesNotAddParents]
906 import a.b.c as foo
907 reveal_type(foo.value) # N: Revealed type is "builtins.float"
908 a.value # E: Name "a" is not defined
909 b.value # E: Name "b" is not defined
910 c.value # E: Name "c" is not defined
911
912 [file a/__init__.py]
913 value = 3
914 [file a/b/__init__.py]
915 value = "a"
916 [file a/b/c.py]
917 value = 3.2
918 [out]
919
920 [case testSubmoduleImportFromDoesNotAddParents]
921 from a import b
922 reveal_type(b.value) # N: Revealed type is "builtins.str"
923 b.c.value # E: Module has no attribute "c"
924 a.value # E: Name "a" is not defined
925
926 [file a/__init__.py]
927 value = 3
928 [file a/b/__init__.py]
929 value = "a"
930 [file a/b/c.py]
931 value = 3.2
932 [builtins fixtures/module.pyi]
933 [out]
934
935 [case testSubmoduleImportFromDoesNotAddParents2]
936 from a.b import c
937 reveal_type(c.value) # N: Revealed type is "builtins.float"
938 a.value # E: Name "a" is not defined
939 b.value # E: Name "b" is not defined
940
941 [file a/__init__.py]
942 value = 3
943 [file a/b/__init__.py]
944 value = "a"
945 [file a/b/c.py]
946 value = 3.2
947 [out]
948
949 [case testSubmoduleRegularImportNotDirectlyAddedToParent]
950 import a.b.c
951 def accept_float(x: float) -> None: pass
952 accept_float(a.b.c.value)
953
954 [file a/__init__.py]
955 value = 3
956 b.value # E: Name "b" is not defined
957 a.b.value # E: Name "a" is not defined
958
959 [file a/b/__init__.py]
960 value = "a"
961 c.value # E: Name "c" is not defined
962 a.b.c.value # E: Name "a" is not defined
963
964 [file a/b/c.py]
965 value = 3.2
966
967 [case testSubmoduleMixingLocalAndQualifiedNames]
968 from a.b import MyClass
969 val1: a.b.MyClass # E: Name "a" is not defined
970 val2: MyClass
971
972 [file a/__init__.py]
973 [file a/b.py]
974 class MyClass: pass
975 [out]
976
977 [case testSubmoduleMixingImportFrom]
978 import parent.child
979
980 [file parent/__init__.py]
981
982 [file parent/common.py]
983 class SomeClass: pass
984
985 [file parent/child.py]
986 from parent.common import SomeClass
987 from parent import common
988 foo = parent.common.SomeClass()
989
990 [builtins fixtures/module.pyi]
991 [out]
992 tmp/parent/child.py:3: error: Name "parent" is not defined
993
994 [case testSubmoduleMixingImportFromAndImport]
995 import parent.child
996
997 [file parent/__init__.py]
998
999 [file parent/common.py]
1000 class SomeClass: pass
1001
1002 [file parent/unrelated.py]
1003 class ShouldNotLoad: pass
1004
1005 [file parent/child.py]
1006 from parent.common import SomeClass
1007 import parent
1008
1009 # Note, since this might be unintuitive -- when `parent.common` is loaded in any way,
1010 # shape, or form, it's added to `parent`'s namespace, which is why the below line
1011 # succeeds.
1012 foo = parent.common.SomeClass()
1013 reveal_type(foo)
1014 bar = parent.unrelated.ShouldNotLoad()
1015
1016 [builtins fixtures/module.pyi]
1017 [out]
1018 tmp/parent/child.py:8: note: Revealed type is "parent.common.SomeClass"
1019 tmp/parent/child.py:9: error: Module has no attribute "unrelated"
1020
1021 [case testSubmoduleMixingImportFromAndImport2]
1022 import parent.child
1023
1024 [file parent/__init__.py]
1025
1026 [file parent/common.py]
1027 class SomeClass: pass
1028
1029 [file parent/child.py]
1030 from parent import common
1031 import parent
1032 foo = parent.common.SomeClass()
1033 reveal_type(foo)
1034
1035 [builtins fixtures/module.pyi]
1036 [out]
1037 tmp/parent/child.py:4: note: Revealed type is "parent.common.SomeClass"
1038
1039 -- Tests repeated imports
1040
1041 [case testIdenticalImportFromTwice]
1042 from a import x, y, z
1043 from b import x, y, z
1044 [file a.py]
1045 from common import x, y, z
1046 [file b.py]
1047 from common import x, y, z
1048 [file common.py]
1049 x = 3
1050 def y() -> int: return 3
1051 class z: pass
1052 [out]
1053
1054 [case testIdenticalImportStarTwice]
1055 from a import *
1056 from b import *
1057 [file a.py]
1058 from common import x, y, z
1059 [file b.py]
1060 from common import x, y, z
1061 [file common.py]
1062 x = 3
1063 def y() -> int: return 3
1064 class z: pass
1065 [out]
1066
1067 [case testDifferentImportSameNameTwice]
1068 from a import x, y, z
1069 from b import x, y, z
1070 [file a.py]
1071 x = 3
1072 def y() -> int: return 1
1073 class z: pass
1074 [file b.py]
1075 x = "foo"
1076 def y() -> str: return "foo"
1077 class z: pass
1078 [out]
1079 main:2: error: Incompatible import of "x" (imported name has type "str", local name has type "int")
1080 main:2: error: Incompatible import of "y" (imported name has type "Callable[[], str]", local name has type "Callable[[], int]")
1081 main:2: error: Incompatible import of "z" (imported name has type "type[b.z]", local name has type "type[a.z]")
1082
1083 -- Misc
1084
1085 [case testInheritFromBadImport]
1086 # cmd: mypy -m bar
1087 [file foo.py]
1088 pass
1089 [file bar.py]
1090 from foo import B
1091 class C(B):
1092 pass
1093 [out]
1094 tmp/bar.py:1: error: Module "foo" has no attribute "B"
1095
1096 [case testImportSuppressedWhileAlmostSilent]
1097 # cmd: mypy -m main
1098 # flags: --follow-imports=error
1099 [file main.py]
1100 import mod
1101 [file mod.py]
1102 [builtins fixtures/module.pyi]
1103 [out]
1104 tmp/main.py:1: error: Import of "mod" ignored
1105 tmp/main.py:1: note: (Using --follow-imports=error, module not passed on command line)
1106
1107 [case testAncestorSuppressedWhileAlmostSilent]
1108 # cmd: mypy -m foo.bar
1109 # flags: --follow-imports=error
1110 [file foo/bar.py]
1111 [file foo/__init__.py]
1112 [builtins fixtures/module.pyi]
1113 [out]
1114 tmp/foo/bar.py: error: Ancestor package "foo" ignored
1115 tmp/foo/bar.py: note: (Using --follow-imports=error, submodule passed on command line)
1116
1117 [case testStubImportNonStubWhileSilent]
1118 # cmd: mypy -m main
1119 # flags: --follow-imports=skip
1120 [file main.py]
1121 from stub import x, z # Followed
1122 from other import y # Not followed
1123 x + '' # No error here
1124 y + '' # No error here
1125 z + '' # Error here
1126 [file stub.pyi]
1127 from non_stub import x as x # this import is not followed
1128
1129 z = 42
1130 [file non_stub.py]
1131 x = 42
1132
1133 x + '' # no error because file is not analyzed
1134 [file other.py]
1135 y = 42
1136 [builtins fixtures/module.pyi]
1137 [out]
1138 tmp/main.py:5: error: Unsupported left operand type for + ("int")
1139
1140 [case testSilentSubmoduleImport]
1141 # cmd: mypy -m foo
1142 # flags: --follow-imports=skip
1143 [file foo/__init__.py]
1144 from foo import bar
1145 [file foo/bar.py]
1146 pass
1147
1148 [case testImportReExportFromChildrenInCycle1]
1149 # cmd: mypy -m project.root project.study.a project.neighbor
1150 [file project/__init__.py]
1151 from project.study import CustomType
1152 x = 10
1153 [file project/root.py]
1154 [file project/study/__init__.py]
1155 from project.study.a import CustomType
1156 [file project/study/a.py]
1157 from project import root
1158 # TODO (#4498): This test is basically testing the `all_are_submodules` logic
1159 # in build, which skips generating a dependency to a module if
1160 # everything in it is a submodule. But that is still all just a
1161 # workaround for bugs in cycle handling. If we uncomment the next
1162 # line, we'll still break:
1163 # from project import x
1164 CustomType = str
1165 [file project/neighbor/__init__.py]
1166 from project.study import CustomType
1167 def m(arg: CustomType) -> str:
1168 return 'test'
1169
1170 [case testImportReExportFromChildrenInCycle2]
1171 # cmd: mypy -m project project.b project.ba project.c
1172 # See comments in above test about this being a workaround.
1173 [file foo.py]
1174 def get_foo() -> int: return 12
1175
1176 [file project/ba.py]
1177 from . import b
1178 b.FOO
1179
1180 [file project/b.py]
1181 import foo
1182 from . import c
1183 FOO = foo.get_foo()
1184
1185 [file project/c.py]
1186
1187 [file project/__init__.py]
1188 from . import ba
1189
1190 [case testSuperclassInImportCycle]
1191 import a
1192 import d
1193 a.A().f(d.D())
1194 [file a.py]
1195 if 0:
1196 import d
1197 class B: pass
1198 class C(B): pass
1199 class A:
1200 def f(self, x: B) -> None: pass
1201 [file d.py]
1202 import a
1203 class D(a.C): pass
1204
1205 [case testSuperclassInImportCycleReversedImports]
1206 import d
1207 import a
1208 a.A().f(d.D())
1209 [file a.py]
1210 if 0:
1211 import d
1212 class B: pass
1213 class C(B): pass
1214 class A:
1215 def f(self, x: B) -> None: pass
1216 [file d.py]
1217 import a
1218 class D(a.C): pass
1219
1220 [case testPreferPackageOverFile]
1221 import a
1222 [file a.py]
1223 / # intentional syntax error -- this file shouldn't be parsed
1224 [file a/__init__.py]
1225 pass
1226 [out]
1227
1228 [case testPreferPackageOverFile2]
1229 from a import x
1230 [file a.py]
1231 / # intentional syntax error -- this file shouldn't be parsed
1232 [file a/__init__.py]
1233 x = 0
1234 [out]
1235
1236 [case testImportInClass]
1237 class C:
1238 import foo
1239 reveal_type(C.foo.bar) # N: Revealed type is "builtins.int"
1240 [file foo.py]
1241 bar = 0
1242 [builtins fixtures/module.pyi]
1243 [out]
1244
1245 [case testIfFalseImport]
1246 if False:
1247 import a
1248 def f(x: 'a.A') -> int:
1249 return x.f()
1250 [file a.py]
1251 class A:
1252 def f(self) -> int:
1253 return 0
1254 [builtins fixtures/bool.pyi]
1255
1256
1257 -- Test stability under import cycles
1258 -- ----------------------------------
1259
1260 -- The first two tests are identical except one main has 'import x'
1261 -- and the other 'import y'. Previously (before build.order_ascc()
1262 -- was added) one of these would fail because the imports were
1263 -- processed in the (reverse) order in which the files were
1264 -- encountered.
1265
1266 [case testImportCycleStability1]
1267 import x
1268 [file x.py]
1269 def f() -> str: return ''
1270 class Base:
1271 attr = f()
1272 def foo():
1273 import y
1274 [file y.py]
1275 import x
1276 class Sub(x.Base):
1277 attr = x.Base.attr
1278 [out]
1279
1280 [case testImportCycleStability2]
1281 import y
1282 [file x.py]
1283 def f() -> str: return ''
1284 class Base:
1285 attr = f()
1286 def foo():
1287 import y
1288 [file y.py]
1289 import x
1290 class Sub(x.Base):
1291 attr = x.Base.attr
1292 [out]
1293
1294 -- This case isn't fixed by order_ascc(), but is fixed by the
1295 -- lightweight type inference added to semanal.py
1296 -- (analyze_simple_literal_type()).
1297
1298 [case testImportCycleStability3]
1299 import y
1300 [file x.py]
1301 class Base:
1302 pass
1303 def foo() -> int:
1304 import y
1305 reveal_type(y.Sub.attr)
1306 return y.Sub.attr
1307 [file y.py]
1308 import x
1309 class Sub(x.Base):
1310 attr = 0
1311 [out]
1312 tmp/x.py:5: note: Revealed type is "builtins.int"
1313
1314 -- This case has a symmetrical cycle, so it doesn't matter in what
1315 -- order the files are processed. It depends on the lightweight type
1316 -- interference.
1317
1318 [case testImportCycleStability4]
1319 import x
1320 [file x.py]
1321 import y
1322 class C:
1323 attr = ''
1324 def foo() -> int:
1325 return y.D.attr
1326 [file y.py]
1327 import x
1328 class D:
1329 attr = 0
1330 def bar() -> str:
1331 return x.C.attr
1332
1333 -- These cases test all supported literal types.
1334
1335 [case testImportCycleStability5]
1336 import y
1337 [file x.py]
1338 class Base:
1339 pass
1340 def foo() -> None:
1341 import y
1342 i = y.Sub.iattr # type: int
1343 f = y.Sub.fattr # type: float
1344 s = y.Sub.sattr # type: str
1345 b = y.Sub.battr # type: bytes
1346 [file y.py]
1347 import x
1348 class Sub(x.Base):
1349 iattr = 0
1350 fattr = 0.0
1351 sattr = ''
1352 battr = b''
1353 [out]
1354
1355 -- This case tests module-level variables.
1356
1357 [case testImportCycleStability7]
1358 import x
1359 [file x.py]
1360 def foo() -> int:
1361 import y
1362 reveal_type(y.value)
1363 return y.value
1364 [file y.py]
1365 import x
1366 value = 12
1367 [out]
1368 tmp/x.py:3: note: Revealed type is "builtins.int"
1369
1370 -- This is not really cycle-related but still about the lightweight
1371 -- type checker.
1372
1373 [case testImportCycleStability8]
1374 x = 1 # type: str
1375 reveal_type(x)
1376 [out]
1377 main:1: error: Incompatible types in assignment (expression has type "int", variable has type "str")
1378 main:2: note: Revealed type is "builtins.str"
1379
1380 -- Tests for cross-module second_pass checking.
1381
1382 [case testSymmetricImportCycle1]
1383 import a
1384 [file a.py]
1385 import b
1386 def f() -> int:
1387 return b.x
1388 y = 0 + int()
1389 [file b.py]
1390 import a
1391 def g() -> int:
1392 reveal_type(a.y)
1393 return a.y
1394 x = 1 + int()
1395 [out]
1396 tmp/b.py:3: note: Revealed type is "builtins.int"
1397
1398 [case testSymmetricImportCycle2]
1399 import b
1400 [file a.py]
1401 import b
1402 def f() -> int:
1403 reveal_type(b.x)
1404 return b.x
1405 y = 0 + int()
1406 [file b.py]
1407 import a
1408 def g() -> int:
1409 return a.y
1410 x = 1 + int()
1411 [out]
1412 tmp/a.py:3: note: Revealed type is "builtins.int"
1413
1414 [case testThreePassesRequired]
1415 import b
1416 [file a.py]
1417 import b
1418 class C:
1419 def f1(self) -> None:
1420 reveal_type(self.x2)
1421 def f2(self) -> None:
1422 self.x2 = b.b
1423 [file b.py]
1424 import a
1425 b = 1 + int()
1426 [out]
1427 tmp/a.py:4: note: Revealed type is "builtins.int"
1428
1429 [case testErrorInPassTwo1]
1430 import b
1431 [file a.py]
1432 import b
1433 def f() -> None:
1434 a = b.x + 1
1435 a + ''
1436 [file b.py]
1437 import a
1438 x = 1 + int()
1439 [out]
1440 tmp/a.py:4: error: Unsupported operand types for + ("int" and "str")
1441
1442 [case testErrorInPassTwo2]
1443 import a
1444 [file a.py]
1445 import b
1446 def f() -> None:
1447 a = b.x + 1
1448 a + ''
1449 [file b.py]
1450 import a
1451 x = 1 + int()
1452 [out]
1453 tmp/a.py:4: error: Unsupported operand types for + ("int" and "str")
1454
1455 [case testDeferredDecorator]
1456 import a
1457 [file a.py]
1458 import b
1459 def g() -> None:
1460 f('')
1461 @b.deco
1462 def f(a: str) -> int: pass
1463 reveal_type(f)
1464 x = 1 + int()
1465 [file b.py]
1466 from typing import Callable, TypeVar
1467 import a
1468 T = TypeVar('T')
1469 def deco(f: Callable[[T], int]) -> Callable[[T], int]:
1470 a.x
1471 return f
1472 [out]
1473 tmp/a.py:6: note: Revealed type is "def (builtins.str) -> builtins.int"
1474
1475 [case testDeferredClassContext]
1476 class A:
1477 def f(self) -> str: return 'foo'
1478 class B(A):
1479 def f(self) -> str: return self.x
1480 def initialize(self) -> None: self.x = 'bar'
1481
1482 [case testDeferredClassContextUnannotated]
1483 class A:
1484 def f(self) -> str: return 'foo'
1485 class B(A):
1486 def f(self) -> str: return self.x
1487 def initialize(self): self.x = 'bar'
1488
1489 -- Scripts and __main__
1490
1491 [case testScriptsAreModules]
1492 # flags: --scripts-are-modules
1493 [file a]
1494 pass
1495 [file b]
1496 pass
1497
1498 -- Misc
1499
1500 [case testScriptsAreNotModules]
1501 # cmd: mypy a b
1502 [file a]
1503 pass
1504 [file b]
1505 pass
1506 [out]
1507
1508 [case testTypeCheckPrio]
1509 # cmd: mypy -m part1 part2 part3 part4
1510
1511 [file part1.py]
1512 from part3 import Thing
1513 class FirstThing: pass
1514
1515 [file part2.py]
1516 from part4 import part4_thing as Thing
1517
1518 [file part3.py]
1519 from part2 import Thing
1520 reveal_type(Thing)
1521
1522 [file part4.py]
1523 from typing import TYPE_CHECKING
1524 if TYPE_CHECKING:
1525 from part1 import FirstThing
1526 def part4_thing(a: int) -> str: pass
1527
1528 [builtins fixtures/bool.pyi]
1529 [typing fixtures/typing-medium.pyi]
1530 [out]
1531 tmp/part3.py:2: note: Revealed type is "def (a: builtins.int) -> builtins.str"
1532
1533 [case testImportStarAliasAnyList]
1534 import bar
1535
1536 [file bar.py]
1537 from foo import *
1538 def bar(y: AnyAlias) -> None: pass
1539
1540 l: ListAlias[int]
1541 reveal_type(l)
1542
1543 [file foo.py]
1544 from typing import Any, List
1545 AnyAlias = Any
1546 ListAlias = List
1547 [builtins fixtures/list.pyi]
1548 [out]
1549 tmp/bar.py:5: note: Revealed type is "builtins.list[builtins.int]"
1550
1551 [case testImportStarAliasSimpleGeneric]
1552 from ex2a import *
1553
1554 def do_something(dic: Row) -> None:
1555 pass
1556
1557 def do_another() -> Row:
1558 return {}
1559
1560 do_something({'good': 'bad'}) # E: Dict entry 0 has incompatible type "str": "str"; expected "str": "int"
1561 reveal_type(do_another()) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
1562
1563 [file ex2a.py]
1564 from typing import Dict
1565 Row = Dict[str, int]
1566 [builtins fixtures/dict.pyi]
1567 [out]
1568
1569 [case testImportStarAliasGeneric]
1570 from y import *
1571 notes: G[X]
1572 another = G[X]()
1573 second = XT[str]()
1574 last = XT[G]()
1575
1576 reveal_type(notes) # N: Revealed type is "y.G[y.G[builtins.int]]"
1577 reveal_type(another) # N: Revealed type is "y.G[y.G[builtins.int]]"
1578 reveal_type(second) # N: Revealed type is "y.G[builtins.str]"
1579 reveal_type(last) # N: Revealed type is "y.G[y.G[Any]]"
1580
1581 [file y.py]
1582 from typing import Generic, TypeVar
1583
1584 T = TypeVar('T')
1585
1586 class G(Generic[T]):
1587 pass
1588
1589 X = G[int]
1590 XT = G[T]
1591 [out]
1592
1593 [case testImportStarAliasCallable]
1594 from foo import *
1595 from typing import Any
1596
1597 def bar(x: Any, y: AnyCallable) -> Any:
1598 return 'foo'
1599
1600 cb: AnyCallable
1601 reveal_type(cb) # N: Revealed type is "def (*Any, **Any) -> Any"
1602
1603 [file foo.py]
1604 from typing import Callable, Any
1605 AnyCallable = Callable[..., Any]
1606 [out]
1607
1608 [case testRevealType]
1609 import types
1610 def f() -> types.ModuleType:
1611 return types
1612 reveal_type(f()) # N: Revealed type is "types.ModuleType"
1613 reveal_type(types) # N: Revealed type is "types.ModuleType"
1614 [builtins fixtures/module.pyi]
1615 [typing fixtures/typing-full.pyi]
1616
1617 [case testClassImportAccessedInMethod]
1618 class C:
1619 import m
1620 def foo(self) -> None:
1621 x = self.m.a
1622 reveal_type(x) # N: Revealed type is "builtins.str"
1623 # ensure we distinguish self from other variables
1624 y = 'hello'
1625 z = y.m.a # E: "str" has no attribute "m"
1626 @classmethod
1627 def cmethod(cls) -> None:
1628 y = cls.m.a
1629 reveal_type(y) # N: Revealed type is "builtins.str"
1630 @staticmethod
1631 def smethod(foo: int) -> None:
1632 # we aren't confused by first arg of a staticmethod
1633 y = foo.m.a # E: "int" has no attribute "m"
1634
1635 [file m.py]
1636 a = 'foo'
1637
1638 [builtins fixtures/module.pyi]
1639
1640 [case testModuleAlias]
1641 import m
1642 m2 = m
1643 reveal_type(m2.a) # N: Revealed type is "builtins.str"
1644 m2.b # E: Module has no attribute "b"
1645 m2.c = 'bar' # E: Module has no attribute "c"
1646
1647 [file m.py]
1648 a = 'foo'
1649
1650 [builtins fixtures/module.pyi]
1651
1652 [case testClassModuleAlias]
1653 import m
1654
1655 class C:
1656 x = m
1657 def foo(self) -> None:
1658 reveal_type(self.x.a) # N: Revealed type is "builtins.str"
1659
1660 [file m.py]
1661 a = 'foo'
1662
1663 [builtins fixtures/module.pyi]
1664
1665 [case testLocalModuleAlias]
1666 import m
1667
1668 def foo() -> None:
1669 x = m
1670 reveal_type(x.a) # N: Revealed type is "builtins.str"
1671
1672 class C:
1673 def foo(self) -> None:
1674 x = m
1675 reveal_type(x.a) # N: Revealed type is "builtins.str"
1676
1677 [file m.py]
1678 a = 'foo'
1679
1680 [builtins fixtures/module.pyi]
1681
1682 [case testChainedModuleAlias]
1683 import m
1684 m3 = m2 = m
1685 m4 = m3
1686 m5 = m4
1687 reveal_type(m2.a) # N: Revealed type is "builtins.str"
1688 reveal_type(m3.a) # N: Revealed type is "builtins.str"
1689 reveal_type(m4.a) # N: Revealed type is "builtins.str"
1690 reveal_type(m5.a) # N: Revealed type is "builtins.str"
1691
1692 [file m.py]
1693 a = 'foo'
1694
1695 [builtins fixtures/module.pyi]
1696
1697 [case testMultiModuleAlias]
1698 import m, n
1699 m2, n2, (m3, n3) = m, n, [m, n]
1700 reveal_type(m2.a) # N: Revealed type is "builtins.str"
1701 reveal_type(n2.b) # N: Revealed type is "builtins.str"
1702 reveal_type(m3.a) # N: Revealed type is "builtins.str"
1703 reveal_type(n3.b) # N: Revealed type is "builtins.str"
1704
1705 x, y = m # E: Module object is not iterable
1706 x, y, z = m, n # E: Need more than 2 values to unpack (3 expected)
1707 x, y = m, m, m # E: Too many values to unpack (2 expected, 3 provided)
1708 x, (y, z) = m, n # E: Module object is not iterable
1709 x, (y, z) = m, (n, n, n) # E: Too many values to unpack (2 expected, 3 provided)
1710
1711 [file m.py]
1712 a = 'foo'
1713
1714 [file n.py]
1715 b = 'bar'
1716
1717 [builtins fixtures/module.pyi]
1718
1719 [case testModuleAliasWithExplicitAnnotation]
1720 from typing import Any
1721 import types
1722 import m
1723 mod_mod: types.ModuleType = m
1724 mod_mod2: types.ModuleType
1725 mod_mod2 = m
1726 mod_mod3 = m # type: types.ModuleType
1727 mod_any: Any = m
1728 mod_int: int = m # E: Incompatible types in assignment (expression has type Module, variable has type "int")
1729
1730 reveal_type(mod_mod) # N: Revealed type is "types.ModuleType"
1731 reveal_type(mod_mod.a) # N: Revealed type is "Any"
1732 reveal_type(mod_mod2) # N: Revealed type is "types.ModuleType"
1733 reveal_type(mod_mod2.a) # N: Revealed type is "Any"
1734 reveal_type(mod_mod3) # N: Revealed type is "types.ModuleType"
1735 reveal_type(mod_mod3.a) # N: Revealed type is "Any"
1736 reveal_type(mod_any) # N: Revealed type is "Any"
1737
1738 [file m.py]
1739 a = 'foo'
1740
1741 [builtins fixtures/module.pyi]
1742
1743 [case testModuleAliasPassedToFunction]
1744 import types
1745 import m
1746
1747 def takes_module(x: types.ModuleType):
1748 reveal_type(x.__file__) # N: Revealed type is "builtins.str"
1749
1750 n = m
1751 takes_module(m)
1752 takes_module(n)
1753
1754 [file m.py]
1755 a = 'foo'
1756
1757 [builtins fixtures/module.pyi]
1758
1759 [case testModuleAliasRepeated]
1760 import m, n
1761
1762 if bool():
1763 x = m
1764 else:
1765 x = 3 # E: Incompatible types in assignment (expression has type "int", variable has type Module)
1766
1767 if bool():
1768 y = 3
1769 else:
1770 y = m # E: Incompatible types in assignment (expression has type Module, variable has type "int")
1771
1772 if bool():
1773 z = m
1774 else:
1775 z = n # E: Cannot assign multiple modules to name "z" without explicit "types.ModuleType" annotation
1776
1777 [file m.py]
1778 a = 'foo'
1779
1780 [file n.py]
1781 a = 3
1782
1783 [builtins fixtures/module.pyi]
1784
1785 [case testModuleAliasRepeatedWithAnnotation]
1786 import types
1787 import m, n
1788
1789 x: types.ModuleType
1790 if bool():
1791 x = m
1792 else:
1793 x = n
1794
1795 reveal_type(x.nope) # N: Revealed type is "Any"
1796 reveal_type(x.__file__) # N: Revealed type is "builtins.str"
1797
1798 [file m.py]
1799 a = 'foo'
1800
1801 [file n.py]
1802 a = 3
1803
1804 [builtins fixtures/module.pyi]
1805
1806 [case testModuleAliasRepeatedComplex]
1807 import m, n, o
1808
1809 x = m
1810 if int():
1811 x = n # E: Cannot assign multiple modules to name "x" without explicit "types.ModuleType" annotation
1812 if int():
1813 x = o # E: Cannot assign multiple modules to name "x" without explicit "types.ModuleType" annotation
1814
1815 y = o
1816 if int():
1817 y, z = m, n # E: Cannot assign multiple modules to name "y" without explicit "types.ModuleType" annotation
1818
1819 xx = m
1820 if int():
1821 xx = m
1822 reveal_type(xx.a) # N: Revealed type is "builtins.str"
1823
1824 [file m.py]
1825 a = 'foo'
1826
1827 [file n.py]
1828 a = 3
1829
1830 [file o.py]
1831 a = 'bar'
1832
1833 [builtins fixtures/module.pyi]
1834
1835 [case testModuleAliasToOtherModule]
1836 import m, n
1837 m = n # E: Cannot assign multiple modules to name "m" without explicit "types.ModuleType" annotation
1838
1839 [file m.py]
1840
1841 [file n.py]
1842
1843 [builtins fixtures/module.pyi]
1844
1845 [case testNoReExportFromStubs]
1846 from stub import Iterable # E: Module "stub" does not explicitly export attribute "Iterable"
1847 from stub import D # E: Module "stub" does not explicitly export attribute "D"
1848 from stub import C
1849 from stub import foo
1850 from stub import bar # E: Module "stub" does not explicitly export attribute "bar"
1851
1852 c = C()
1853 reveal_type(c.x) # N: Revealed type is "builtins.int"
1854 it: Iterable[int]
1855 reveal_type(it) # N: Revealed type is "typing.Iterable[builtins.int]"
1856
1857 [file stub.pyi]
1858 from typing import Iterable
1859 from substub import C as C
1860 from substub import C as D
1861 from package import foo as foo
1862 import package.bar as bar
1863
1864 def fun(x: Iterable[str]) -> Iterable[int]: pass
1865
1866 [file substub.pyi]
1867 class C:
1868 x: int
1869
1870 [file package/foo.pyi]
1871
1872 [file package/bar.pyi]
1873
1874 [builtins fixtures/module.pyi]
1875
1876 [case testNoReExportFromStubsMemberType]
1877 import stub
1878
1879 c = stub.C()
1880 reveal_type(c.x) # N: Revealed type is "builtins.int"
1881 it: stub.Iterable[int] # E: Name "stub.Iterable" is not defined
1882 reveal_type(it) # N: Revealed type is "Any"
1883
1884 [file stub.pyi]
1885 from typing import Iterable
1886 from substub import C as C
1887
1888 def fun(x: Iterable[str]) -> Iterable[int]: pass
1889
1890 [file substub.pyi]
1891 class C:
1892 x: int
1893
1894 [builtins fixtures/module.pyi]
1895
1896 [case testNoReExportFromStubsMemberVar]
1897 import stub
1898
1899 reveal_type(stub.y) # N: Revealed type is "builtins.int"
1900 reveal_type(stub.z) # E: Module "stub" does not explicitly export attribute "z" \
1901 # N: Revealed type is "builtins.int"
1902
1903
1904 [file stub.pyi]
1905 from substub import y as y
1906 from substub import z
1907
1908 [file substub.pyi]
1909 y = 42
1910 z: int
1911
1912 [builtins fixtures/module.pyi]
1913
1914 [case testReExportChildStubs]
1915 import mod
1916 from mod import submod
1917
1918 reveal_type(mod.x) # N: Revealed type is "mod.submod.C"
1919 y = submod.C()
1920 reveal_type(y.a) # N: Revealed type is "builtins.str"
1921
1922 [file mod/__init__.pyi]
1923 from . import submod
1924 x: submod.C
1925
1926 [file mod/submod.pyi]
1927 class C:
1928 a: str
1929
1930 [builtins fixtures/module.pyi]
1931
1932 [case testReExportChildStubs2]
1933 import mod.submod
1934
1935 y = mod.submod.C()
1936 reveal_type(y.a) # N: Revealed type is "builtins.str"
1937
1938 [file mod/__init__.pyi]
1939 from . import submod
1940 x: submod.C
1941
1942 [file mod/submod.pyi]
1943 class C:
1944 a: str
1945
1946 [builtins fixtures/module.pyi]
1947
1948 [case testReExportChildStubs3]
1949 from util import mod
1950 reveal_type(mod) # N: Revealed type is "def () -> package.mod.mod"
1951
1952 from util import internal_detail # E: Module "util" does not explicitly export attribute "internal_detail"
1953
1954 [file package/__init__.pyi]
1955 from .mod import mod as mod
1956
1957 [file package/mod.pyi]
1958 class mod: ...
1959
1960 [file util.pyi]
1961 from package import mod as mod
1962 # stubs require explicit re-export
1963 from package import mod as internal_detail
1964 [builtins fixtures/module.pyi]
1965
1966 [case testNoReExportUnrelatedModule]
1967 from mod2 import unrelated # E: Module "mod2" does not explicitly export attribute "unrelated"
1968
1969 [file mod1/__init__.pyi]
1970 [file mod1/unrelated.pyi]
1971 x: int
1972
1973 [file mod2.pyi]
1974 from mod1 import unrelated
1975 [builtins fixtures/module.pyi]
1976
1977 [case testNoReExportUnrelatedSiblingPrefix]
1978 from pkg.unrel import unrelated # E: Module "pkg.unrel" does not explicitly export attribute "unrelated"
1979
1980 [file pkg/__init__.pyi]
1981 [file pkg/unrelated.pyi]
1982 x: int
1983
1984 [file pkg/unrel.pyi]
1985 from pkg import unrelated
1986 [builtins fixtures/module.pyi]
1987
1988 [case testNoReExportChildStubs]
1989 import mod
1990 from mod import C, D # E: Module "mod" does not explicitly export attribute "C"
1991
1992 reveal_type(mod.x) # N: Revealed type is "mod.submod.C"
1993 mod.C # E: Module "mod" does not explicitly export attribute "C"
1994 y = mod.D()
1995 reveal_type(y.a) # N: Revealed type is "builtins.str"
1996
1997 [file mod/__init__.pyi]
1998 from .submod import C, D as D
1999 x: C
2000
2001 [file mod/submod.pyi]
2002 class C: pass
2003 class D:
2004 a: str
2005 [builtins fixtures/module.pyi]
2006
2007 [case testNoReExportNestedStub]
2008 from stub import substub # E: Module "stub" does not explicitly export attribute "substub"
2009
2010 [file stub.pyi]
2011 import substub
2012
2013 [file substub.pyi]
2014 x = 42
2015
2016 [file mod/submod.pyi]
2017
2018 [case testModuleAliasToQualifiedImport]
2019 import package.module
2020 alias = package.module
2021 reveal_type(alias.whatever('/')) # N: Revealed type is "builtins.str"
2022 [file package/__init__.py]
2023
2024 [file package/module.py]
2025 from typing import TypeVar
2026 T = TypeVar('T')
2027 def whatever(x: T) -> T: pass
2028 [builtins fixtures/module.pyi]
2029 [typing fixtures/typing-full.pyi]
2030
2031 [case testModuleAliasToQualifiedImport2]
2032 import mod
2033 import othermod
2034 alias = mod.submod
2035 reveal_type(alias.whatever('/')) # N: Revealed type is "builtins.str"
2036 if int():
2037 alias = othermod # E: Cannot assign multiple modules to name "alias" without explicit "types.ModuleType" annotation
2038 [file mod.py]
2039 import submod
2040 [file submod.py]
2041 from typing import TypeVar
2042 T = TypeVar('T')
2043 def whatever(x: T) -> T: pass
2044 [file othermod.py]
2045 [builtins fixtures/module.pyi]
2046 [typing fixtures/typing-full.pyi]
2047
2048 [case testModuleLevelGetattr]
2049 import has_getattr
2050
2051 reveal_type(has_getattr.any_attribute) # N: Revealed type is "Any"
2052
2053 [file has_getattr.pyi]
2054 from typing import Any
2055
2056 def __getattr__(name: str) -> Any: ...
2057
2058 [builtins fixtures/module.pyi]
2059
2060 [case testModuleLevelGetattrReturnType]
2061 import has_getattr
2062
2063 reveal_type(has_getattr.any_attribute) # N: Revealed type is "builtins.str"
2064
2065 [file has_getattr.pyi]
2066 def __getattr__(name: str) -> str: ...
2067
2068 [builtins fixtures/module.pyi]
2069
2070 [case testModuleLevelGetattrInvalidSignature]
2071 import has_getattr
2072
2073 reveal_type(has_getattr.any_attribute) # N: Revealed type is "builtins.str"
2074
2075 [file has_getattr.pyi]
2076 def __getattr__(x: int, y: str) -> str: ... # E: Invalid signature "Callable[[int, str], str]" for "__getattr__"
2077 [builtins fixtures/module.pyi]
2078
2079 [case testModuleLevelGetattrNotCallable]
2080 import has_getattr
2081
2082 reveal_type(has_getattr.any_attribute) # N: Revealed type is "Any"
2083
2084 [file has_getattr.pyi]
2085 __getattr__ = 3 # E: Invalid signature "int" for "__getattr__"
2086 [builtins fixtures/module.pyi]
2087
2088 [case testModuleLevelGetattrUntyped]
2089 import has_getattr
2090 reveal_type(has_getattr.any_attribute) # N: Revealed type is "Any"
2091
2092 [file has_getattr.pyi]
2093 def __getattr__(name): ...
2094
2095 [builtins fixtures/module.pyi]
2096
2097 [case testModuleLevelGetattrNotStub]
2098 import has_getattr
2099 reveal_type(has_getattr.any_attribute) # N: Revealed type is "builtins.str"
2100
2101 [file has_getattr.py]
2102 def __getattr__(name) -> str: ...
2103
2104 [builtins fixtures/module.pyi]
2105
2106 [case testModuleLevelGetattribute]
2107
2108 def __getattribute__(): ... # E: __getattribute__ is not valid at the module level
2109
2110 [case testModuleLevelGetattrImportFrom]
2111 from has_attr import name
2112 reveal_type(name) # N: Revealed type is "Any"
2113
2114 [file has_attr.pyi]
2115 from typing import Any
2116 def __getattr__(name: str) -> Any: ...
2117
2118 [builtins fixtures/module.pyi]
2119
2120 [case testModuleLevelGetattrImportFromRetType]
2121 from has_attr import int_attr
2122 reveal_type(int_attr) # N: Revealed type is "builtins.int"
2123
2124 [file has_attr.pyi]
2125 def __getattr__(name: str) -> int: ...
2126
2127 [builtins fixtures/module.pyi]
2128
2129 [case testModuleLevelGetattrImportFromNotStub]
2130 from non_stub import name
2131 reveal_type(name) # N: Revealed type is "Any"
2132
2133 [file non_stub.py]
2134 from typing import Any
2135 def __getattr__(name: str) -> Any: ...
2136
2137 [builtins fixtures/module.pyi]
2138
2139 [case testModuleLevelGetattrImportFromAs]
2140 from has_attr import name as n
2141 reveal_type(name) # E: Name "name" is not defined # N: Revealed type is "Any"
2142 reveal_type(n) # N: Revealed type is "Any"
2143
2144 [file has_attr.pyi]
2145 from typing import Any
2146 def __getattr__(name: str) -> Any: ...
2147
2148 [builtins fixtures/module.pyi]
2149
2150 [case testModuleLevelGetattrImportFromAsTwice]
2151
2152 from has_attr import name
2153 from has_attr import name
2154 from has_attr import x
2155 from has_attr import y as x # E: Name "x" already defined (possibly by an import)
2156 reveal_type(name) # N: Revealed type is "builtins.int"
2157
2158 [file has_attr.pyi]
2159 from typing import Any
2160 def __getattr__(name: str) -> int: ...
2161
2162
2163 [case testModuleLevelGetattrAssignedGood]
2164 import non_stub
2165 reveal_type(non_stub.name) # N: Revealed type is "builtins.int"
2166
2167 [file non_stub.py]
2168 from typing import Callable
2169
2170 def make_getattr_good() -> Callable[[str], int]: ...
2171 __getattr__ = make_getattr_good() # OK
2172
2173 [case testModuleLevelGetattrAssignedBad]
2174 import non_stub
2175 reveal_type(non_stub.name) # N: Revealed type is "builtins.int"
2176
2177 [file non_stub.py]
2178 from typing import Callable
2179
2180 def make_getattr_bad() -> Callable[[], int]: ...
2181 __getattr__ = make_getattr_bad() # E: Invalid signature "Callable[[], int]" for "__getattr__"
2182
2183 [case testModuleLevelGetattrImportedGood]
2184 import non_stub
2185 reveal_type(non_stub.name) # N: Revealed type is "builtins.int"
2186
2187 [file non_stub.py]
2188 from has_getattr import __getattr__
2189
2190 [file has_getattr.py]
2191 def __getattr__(name: str) -> int: ...
2192
2193 [case testModuleLevelGetattrImportedBad]
2194 import non_stub
2195 reveal_type(non_stub.name) # N: Revealed type is "builtins.int"
2196
2197 [file non_stub.py]
2198 from has_getattr import __getattr__
2199
2200 [file has_getattr.py]
2201 def __getattr__() -> int: ... # E: Invalid signature "Callable[[], int]" for "__getattr__"
2202 [builtins fixtures/module.pyi]
2203
2204 -- Parallel mode gives only_once notes once *per worker*
2205 [case testFailedImportFromTwoModules_no_parallel]
2206 import c
2207 import b
2208 [file b.py]
2209 import c
2210 [out]
2211 -- TODO: it would be better for this to be in the other order
2212 tmp/b.py:1: error: Cannot find implementation or library stub for module named "c"
2213 main:1: error: Cannot find implementation or library stub for module named "c"
2214 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
2215
2216 [case testIndirectFromImportWithinCycle1]
2217 import a
2218 [file a.py]
2219 from b import f
2220 from c import x
2221 [file b.py]
2222 from c import y
2223 from a import x
2224 def f() -> None: pass
2225 reveal_type(x) # N: Revealed type is "builtins.str"
2226 [file c.py]
2227 x = str()
2228 y = int()
2229
2230 [case testIndirectFromImportWithinCycle2]
2231 import a
2232 [file a.py]
2233 from c import y
2234 from b import x
2235 def f() -> None: pass
2236 reveal_type(x) # N: Revealed type is "builtins.str"
2237 [file b.py]
2238 from a import f
2239 from c import x
2240 [file c.py]
2241 x = str()
2242 y = int()
2243
2244 [case testIndirectFromImportWithinCycleInPackage]
2245 import p.a
2246 [file p/__init__.py]
2247 [file p/a.py]
2248 from p.b import f
2249 from p.c import x
2250 [file p/b.py]
2251 from p.c import y
2252 from p.a import x
2253 def f() -> None: pass
2254 reveal_type(x) # N: Revealed type is "builtins.str"
2255 [file p/c.py]
2256 x = str()
2257 y = int()
2258
2259 [case testIndirectFromImportWithinCycleInPackageIgnoredInit]
2260 # cmd: mypy -m p.a p.b p.c
2261 # flags: --follow-imports=skip --ignore-missing-imports
2262 [file p/__init__.py]
2263 [file p/a.py]
2264 from p.b import f
2265 from p.c import x
2266 [file p/b.py]
2267 from p.c import y
2268 from p.a import x
2269 def f() -> None: pass
2270 reveal_type(x) # N: Revealed type is "builtins.str"
2271 [file p/c.py]
2272 x = str()
2273 y = int()
2274
2275 [case testForwardReferenceToListAlias]
2276 x: List[int]
2277 reveal_type(x) # N: Revealed type is "builtins.list[builtins.int]"
2278 def f() -> 'List[int]': pass
2279 reveal_type(f) # N: Revealed type is "def () -> builtins.list[builtins.int]"
2280 class A:
2281 y: 'List[str]'
2282 def g(self, x: 'List[int]') -> None: pass
2283 reveal_type(A().y) # N: Revealed type is "builtins.list[builtins.str]"
2284 reveal_type(A().g) # N: Revealed type is "def (x: builtins.list[builtins.int])"
2285 from typing import List
2286 [builtins fixtures/list.pyi]
2287
2288 [case testIndirectStarImportWithinCycle1]
2289 import a
2290 [file a.py]
2291 from b import f
2292 from c import x
2293 [file b.py]
2294 from c import y
2295 from a import *
2296 def f() -> None: pass
2297 reveal_type(x) # N: Revealed type is "builtins.str"
2298 [file c.py]
2299 x = str()
2300 y = int()
2301
2302 [case testIndirectStarImportWithinCycle2]
2303 import a
2304 [file a.py]
2305 from c import y
2306 from b import *
2307 def f() -> None: pass
2308 reveal_type(x) # N: Revealed type is "builtins.str"
2309 [file b.py]
2310 from a import f
2311 from c import x
2312 [file c.py]
2313 x = str()
2314 y = int()
2315
2316 [case testModuleGetattrInit1]
2317 from a import b
2318
2319 x = b.f()
2320 [file a/__init__.pyi]
2321 from typing import Any
2322 def __getattr__(attr: str) -> Any: ...
2323 [builtins fixtures/module.pyi]
2324 [out]
2325
2326 [case testModuleGetattrInit2]
2327 import a.b
2328
2329 x = a.b.f()
2330 [file a/__init__.pyi]
2331 from typing import Any
2332 def __getattr__(attr: str) -> Any: ...
2333 [builtins fixtures/module.pyi]
2334 [out]
2335
2336 [case testModuleGetattrInit3]
2337 import a.b
2338
2339 x = a.b.f()
2340 [file a/__init__.py]
2341 from typing import Any
2342 def __getattr__(attr: str) -> Any: ...
2343 [builtins fixtures/module.pyi]
2344 [out]
2345 main:1: error: Cannot find implementation or library stub for module named "a.b"
2346 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
2347
2348 [case testModuleGetattrInit4]
2349 import a.b.c
2350
2351 x = a.b.c.f()
2352 [file a/__init__.pyi]
2353 from typing import Any
2354 def __getattr__(attr: str) -> Any: ...
2355 [builtins fixtures/module.pyi]
2356 [out]
2357
2358 [case testModuleGetattrInit5]
2359 from a.b import f
2360
2361 x = f()
2362 [file a/__init__.pyi]
2363 from typing import Any
2364 def __getattr__(attr: str) -> Any: ...
2365 [builtins fixtures/module.pyi]
2366 [out]
2367
2368 [case testModuleGetattrInit5a]
2369 from a.b import f
2370
2371 x = f()
2372 [file a/__init__.pyi]
2373 from types import ModuleType
2374 def __getattr__(attr: str) -> ModuleType: ...
2375 [builtins fixtures/module.pyi]
2376 [out]
2377
2378
2379 [case testModuleGetattrInit8]
2380 import a.b.c.d
2381
2382 x = a.b.c.d.f()
2383 [file a/__init__.pyi]
2384 from typing import Any
2385 def __getattr__(attr: str) -> Any: ...
2386 [file a/b/__init__.pyi]
2387 # empty (i.e. complete subpackage)
2388 [builtins fixtures/module.pyi]
2389 [out]
2390 main:1: error: Cannot find implementation or library stub for module named "a.b.c.d"
2391 main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
2392 main:1: error: Cannot find implementation or library stub for module named "a.b.c"
2393
2394 [case testModuleGetattrInit8a]
2395 import a.b.c # E: Cannot find implementation or library stub for module named "a.b.c" # N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
2396 import a.d # OK
2397 [file a/__init__.pyi]
2398 from typing import Any
2399 def __getattr__(attr: str) -> Any: ...
2400 [file a/b/__init__.pyi]
2401 # empty (i.e. complete subpackage)
2402 [builtins fixtures/module.pyi]
2403
2404 [case testModuleGetattrInit10]
2405 # flags: --config-file tmp/mypy.ini
2406 import a.b.c # silenced
2407 import a.b.d # error
2408
2409 [file a/__init__.pyi]
2410 from typing import Any
2411 def __getattr__(attr: str) -> Any: ...
2412 [file a/b/__init__.pyi]
2413 # empty (i.e. complete subpackage)
2414
2415 [file mypy.ini]
2416 \[mypy]
2417 \[mypy-a.b.c]
2418 ignore_missing_imports = True
2419 [builtins fixtures/module.pyi]
2420 [out]
2421 main:3: error: Cannot find implementation or library stub for module named "a.b.d"
2422 main:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
2423
2424
2425 [case testModuleGetattrInit10PyProjectTOML]
2426 # flags: --config-file tmp/pyproject.toml
2427 import a.b.c # silenced
2428 import a.b.d # error
2429
2430 [file a/__init__.pyi]
2431 from typing import Any
2432 def __getattr__(attr: str) -> Any: ...
2433
2434 [file a/b/__init__.pyi]
2435 # empty (i.e. complete subpackage)
2436
2437 [file pyproject.toml]
2438 \[tool.mypy]
2439 \[[tool.mypy.overrides]]
2440 module = 'a.b.c'
2441 ignore_missing_imports = true
2442
2443 [builtins fixtures/module.pyi]
2444
2445 [out]
2446 main:3: error: Cannot find implementation or library stub for module named "a.b.d"
2447 main:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
2448
2449
2450 [case testMultipleModulesInOverridePyProjectTOML]
2451 # flags: --config-file tmp/pyproject.toml
2452 import a
2453 import b
2454
2455 [file pyproject.toml]
2456 \[tool.mypy]
2457 \[[tool.mypy.overrides]]
2458 module = ['a', 'b']
2459 ignore_missing_imports = true
2460
2461
2462 [case testIndirectFromImportWithinCycleUsedAsBaseClass]
2463 import a
2464 [file a.py]
2465 from b import f
2466 from c import B
2467 [file b.py]
2468 from c import y
2469 class A(B): pass
2470 reveal_type(A().x) # N: Revealed type is "builtins.int"
2471 from a import B
2472 def f() -> None: pass
2473 [file c.py]
2474 class B:
2475 x: int
2476 x = str()
2477 y = int()
2478
2479 [case testImportFromReExportInCycleUsingRelativeImport1]
2480 from m import One
2481 reveal_type(One) # N: Revealed type is "def () -> m.one.One"
2482 [file m/__init__.py]
2483 from .one import One
2484 from .two import Two
2485 reveal_type(One) # N: Revealed type is "def () -> m.one.One"
2486 [file m/one.py]
2487 class One:
2488 pass
2489 [file m/two.py]
2490 from m import One
2491 reveal_type(One) # N: Revealed type is "def () -> m.one.One"
2492 x: One
2493 reveal_type(x) # N: Revealed type is "m.one.One"
2494
2495 class Two(One):
2496 pass
2497 y: Two
2498 y = x # E: Incompatible types in assignment (expression has type "One", variable has type "Two")
2499 x = y
2500
2501 [case testImportReExportInCycleUsingRelativeImport2]
2502 from m import One
2503 reveal_type(One) # N: Revealed type is "def () -> m.one.One"
2504 [file m/__init__.py]
2505 from .one import One
2506 from .two import Two
2507 reveal_type(One) # N: Revealed type is "def () -> m.one.One"
2508 [file m/one.py]
2509 class One:
2510 pass
2511 [file m/two.py]
2512 import m
2513 reveal_type(m.One) # N: Revealed type is "def () -> m.one.One"
2514 x: m.One
2515 reveal_type(x) # N: Revealed type is "m.one.One"
2516 class Two:
2517 pass
2518
2519 [case testImportReExportedNamedTupleInCycle1]
2520 from m import One
2521 [file m/__init__.py]
2522 from .one import One
2523 from .two import Two
2524 [file m/one.py]
2525 from typing import NamedTuple
2526 class One(NamedTuple):
2527 name: str
2528 [file m/two.py]
2529 import m
2530 x = m.One(name="Foo")
2531 reveal_type(x.name)
2532 class Two:
2533 pass
2534 [builtins fixtures/tuple.pyi]
2535 [out]
2536 tmp/m/two.py:3: note: Revealed type is "builtins.str"
2537
2538 [case testImportReExportedNamedTupleInCycle2]
2539 from m import One
2540 [file m/__init__.py]
2541 from .one import One
2542 from .two import Two
2543 [file m/one.py]
2544 from typing import NamedTuple
2545 One = NamedTuple('One', [('name', str)])
2546 [file m/two.py]
2547 import m
2548 x = m.One(name="Foo")
2549 reveal_type(x.name)
2550 class Two:
2551 pass
2552 [builtins fixtures/tuple.pyi]
2553 [out]
2554 tmp/m/two.py:3: note: Revealed type is "builtins.str"
2555
2556 [case testImportReExportedTypeAliasInCycle]
2557 from m import One
2558 [file m/__init__.py]
2559 from .one import One
2560 from .two import Two
2561 [file m/one.py]
2562 from typing import Union
2563 One = Union[int, str]
2564 [file m/two.py]
2565 import m
2566 x: m.One
2567 reveal_type(x)
2568 class Two:
2569 pass
2570 [out]
2571 tmp/m/two.py:3: note: Revealed type is "builtins.int | builtins.str"
2572
2573 [case testImportCycleSpecialCase]
2574 import p
2575 [file p/__init__.py]
2576 from . import a
2577 from . import b
2578 reveal_type(a.foo()) # N: Revealed type is "builtins.int"
2579 [file p/a.py]
2580 import p
2581 def foo() -> int: pass
2582 [file p/b.py]
2583 import p
2584
2585 def run() -> None:
2586 reveal_type(p.a.foo()) # N: Revealed type is "builtins.int"
2587 [builtins fixtures/module.pyi]
2588
2589 [case testMissingSubmoduleImportedWithIgnoreMissingImports]
2590 # flags: --ignore-missing-imports
2591 import whatever.works
2592 import a.b
2593 x = whatever.works.f()
2594 y = a.b.f()
2595 [file a/__init__.py]
2596 # empty
2597 [out]
2598
2599 [case testMissingSubmoduleImportedWithIgnoreMissingImportsStub]
2600 # flags: --ignore-missing-imports --follow-imports=skip
2601 import whatever.works
2602 import a.b
2603 x = whatever.works.f()
2604 y = a.b.f()
2605 xx: whatever.works.C
2606 yy: a.b.C
2607 xx2: whatever.works.C.D
2608 yy2: a.b.C.D
2609 [file a/__init__.pyi]
2610 # empty
2611 [out]
2612
2613 [case testMissingSubmoduleImportedWithIgnoreMissingImportsNested]
2614 # flags: --ignore-missing-imports
2615 import a.b.c.d
2616 y = a.b.c.d.f()
2617 [file a/__init__.py]
2618 # empty
2619 [file a/b/__init__.py]
2620 # empty
2621 [out]
2622
2623 [case testModuleGetattrBusted]
2624 from a import A
2625 x: A
2626 reveal_type(x) # N: Revealed type is "Any"
2627 [file a.pyi]
2628 from typing import Any
2629 def __getattr__(attr: str) -> Any: ...
2630 [builtins fixtures/module.pyi]
2631 [out]
2632
2633 [case testModuleGetattrBusted2]
2634 from a import A
2635 def f(x: A.B) -> None: ...
2636 reveal_type(f) # N: Revealed type is "def (x: Any)"
2637 [file a.pyi]
2638 from typing import Any
2639 def __getattr__(attr: str) -> Any: ...
2640 [builtins fixtures/module.pyi]
2641 [out]
2642
2643 [case testNoGetattrInterference]
2644 import testmod as t
2645 def f(x: t.Cls) -> None:
2646 reveal_type(x) # N: Revealed type is "testmod.Cls"
2647 [file testmod.pyi]
2648 from typing import Any
2649 def __getattr__(attr: str) -> Any: ...
2650 class Cls: ...
2651 [builtins fixtures/module.pyi]
2652 [out]
2653
2654 [case testFunctionWithDunderName]
2655 def __add__(self) -> int: ...
2656
2657 [case testFunctionWithReversibleDunderName]
2658 def __radd__(self) -> int: ...
2659
2660 [case testFunctionWithInPlaceDunderName]
2661 def __iadd__(self) -> int: ...
2662
2663 -- Tests for PEP 420 namespace packages.
2664
2665 [case testClassicPackage]
2666 from foo.bar import x
2667 [file foo/__init__.py]
2668 # empty
2669 [file foo/bar.py]
2670 x = 0
2671
2672 [case testClassicNotPackage]
2673 # flags: --no-namespace-packages
2674 from foo.bar import x
2675 [file foo/bar.py]
2676 x = 0
2677 [out]
2678 main:2: error: Cannot find implementation or library stub for module named "foo.bar"
2679 main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
2680
2681 [case testNamespacePackage]
2682 # flags: --namespace-packages
2683 from foo.bar import x
2684 reveal_type(x) # N: Revealed type is "builtins.int"
2685 [file foo/bar.py]
2686 x = 0
2687
2688 [case testNamespacePackageWithMypyPath]
2689 # flags: --namespace-packages --config-file tmp/mypy.ini
2690 from foo.bax import x
2691 from foo.bay import y
2692 from foo.baz import z
2693 reveal_type(x) # N: Revealed type is "builtins.int"
2694 reveal_type(y) # N: Revealed type is "builtins.int"
2695 reveal_type(z) # N: Revealed type is "builtins.int"
2696 [file xx/foo/bax.py]
2697 x = 0
2698 [file yy/foo/bay.py]
2699 y = 0
2700 [file foo/baz.py]
2701 z = 0
2702 [file mypy.ini]
2703 \[mypy]
2704 mypy_path = tmp/xx, tmp/yy
2705
2706
2707 [case testNamespacePackageWithMypyPathPyProjectTOML]
2708 # flags: --namespace-packages --config-file tmp/pyproject.toml
2709 from foo.bax import x
2710 from foo.bay import y
2711 from foo.baz import z
2712 reveal_type(x) # N: Revealed type is "builtins.int"
2713 reveal_type(y) # N: Revealed type is "builtins.int"
2714 reveal_type(z) # N: Revealed type is "builtins.int"
2715
2716 [file xx/foo/bax.py]
2717 x = 0
2718
2719 [file yy/foo/bay.py]
2720 y = 0
2721
2722 [file foo/baz.py]
2723 z = 0
2724
2725 [file pyproject.toml]
2726 \[tool.mypy]
2727 mypy_path = ["tmp/xx", "tmp/yy"]
2728
2729
2730 [case testClassicPackageIgnoresEarlierNamespacePackage]
2731 # flags: --namespace-packages --config-file tmp/mypy.ini
2732 from foo.bar import y
2733 reveal_type(y) # N: Revealed type is "builtins.int"
2734 [file xx/foo/bar.py]
2735 x = ''
2736 [file yy/foo/bar.py]
2737 y = 0
2738 [file yy/foo/__init__.py]
2739 [file mypy.ini]
2740 \[mypy]
2741 mypy_path = tmp/xx, tmp/yy
2742
2743 [case testNamespacePackagePickFirstOnMypyPath]
2744 # flags: --namespace-packages --config-file tmp/mypy.ini
2745 from foo.bar import x
2746 reveal_type(x) # N: Revealed type is "builtins.int"
2747 [file xx/foo/bar.py]
2748 x = 0
2749 [file yy/foo/bar.py]
2750 x = ''
2751 [file mypy.ini]
2752 \[mypy]
2753 mypy_path = tmp/xx, tmp/yy
2754
2755 [case testNamespacePackageInsideClassicPackage]
2756 # flags: --namespace-packages --config-file tmp/mypy.ini
2757 from foo.bar.baz import x
2758 reveal_type(x) # N: Revealed type is "builtins.int"
2759 [file xx/foo/bar/baz.py]
2760 x = ''
2761 [file yy/foo/bar/baz.py]
2762 x = 0
2763 [file yy/foo/__init__.py]
2764 [file mypy.ini]
2765 \[mypy]
2766 mypy_path = tmp/xx, tmp/yy
2767
2768 [case testClassicPackageInsideNamespacePackage]
2769 # flags: --namespace-packages --config-file tmp/mypy.ini
2770 from foo.bar.baz.boo import x
2771 reveal_type(x) # N: Revealed type is "builtins.int"
2772 [file xx/foo/bar/baz/boo.py]
2773 x = ''
2774 [file xx/foo/bar/baz/__init__.py]
2775 [file yy/foo/bar/baz/boo.py]
2776 x = 0
2777 [file yy/foo/bar/__init__.py]
2778 [file mypy.ini]
2779 \[mypy]
2780 mypy_path = tmp/xx, tmp/yy
2781
2782 [case testNamespacePackagePlainImport]
2783 # flags: --namespace-packages
2784 import foo.bar.baz
2785 reveal_type(foo.bar.baz.x) # N: Revealed type is "builtins.int"
2786 [file foo/bar/baz.py]
2787 x = 0
2788
2789 [case testModuleGetAttrAssignUnannotated]
2790 import roles
2791 # this should not crash
2792 roles.role = 1
2793
2794 [file roles.pyi]
2795 def __getattr__(name): ...
2796
2797 [case testModuleGetAttrAssignUnannotatedDouble]
2798 import roles
2799 # this also should not crash
2800 roles.role.attr = 1
2801
2802 [file roles.pyi]
2803 def __getattr__(name): ...
2804
2805 [case testModuleGetAttrAssignAny]
2806 import roles
2807 roles.role = 1
2808
2809 [file roles.pyi]
2810 from typing import Any
2811 def __getattr__(name: str) -> Any: ...
2812
2813 [case testModuleGetAttrAssignError]
2814 import roles
2815
2816 roles.role = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
2817
2818 [file roles.pyi]
2819 def __getattr__(name: str) -> str: ...
2820
2821 [case testModuleGetAttrAssignSubmodule]
2822 import roles
2823 roles.role = 1
2824 roles.missing.attr = 1
2825
2826 [file roles/__init__.pyi]
2827 from typing import Any
2828 def __getattr__(name: str) -> Any: ...
2829
2830 [case testModuleGetAttrAssignSubmoduleStrict]
2831 import roles
2832 roles.role = 1 # E: Incompatible types in assignment (expression has type "int", variable has type Module)
2833
2834 [file roles/__init__.pyi]
2835 from types import ModuleType
2836 def __getattr__(name: str) -> ModuleType: ...
2837 [builtins fixtures/module.pyi]
2838
2839 [case testAlwaysReportMissingAttributesOnFoundModules]
2840 # flags: --ignore-missing-imports
2841 import pack.mod as alias
2842
2843 x: alias.NonExistent # E: Name "alias.NonExistent" is not defined
2844
2845 [file pack/__init__.py]
2846 [file pack/mod.py]
2847 class Existent: pass
2848
2849 [case testModuleAttributeTwoSuggestions]
2850 import m
2851 m.aaaa # E: Module has no attribute "aaaa"; maybe "aaaaa" or "aaa"?
2852
2853 [file m.py]
2854 aaa: int
2855 aaaaa: int
2856 [builtins fixtures/module.pyi]
2857
2858 [case testModuleAttributeThreeSuggestions]
2859 import m
2860 m.aaaaa # E: Module has no attribute "aaaaa"; maybe "aaaab", "aaaba", or "aabaa"?
2861
2862 [file m.py]
2863 aaaab: int
2864 aaaba: int
2865 aabaa: int
2866 [builtins fixtures/module.pyi]
2867
2868 [case testDirectlyImportTypedDictObjectAtTopLevel]
2869 import foo.bar.custom_dict
2870 from foo import bar
2871 from foo.bar import custom_dict
2872 from foo.bar.custom_dict import CustomDict
2873
2874 foo.bar.custom_dict.CustomDict(foo="abc", bar="def")
2875 bar.custom_dict.CustomDict(foo="abc", bar="def")
2876 custom_dict.CustomDict(foo="abc", bar="def")
2877 CustomDict(foo="abc", bar="def")
2878
2879 [file foo/__init__.py]
2880 [file foo/bar/__init__.py]
2881 [file foo/bar/custom_dict.py]
2882 from typing import TypedDict
2883
2884 CustomDict = TypedDict(
2885 "CustomDict",
2886 {
2887 "foo": str,
2888 "bar": str,
2889 },
2890 )
2891 [typing fixtures/typing-full.pyi]
2892 [builtins fixtures/tuple.pyi]
2893
2894 [case testNoReExportFromMissingStubs]
2895 from stub import a # E: Module "stub" does not explicitly export attribute "a"
2896 from stub import b
2897 from stub import c # E: Module "stub" has no attribute "c"
2898 from stub import d # E: Module "stub" does not explicitly export attribute "d"
2899
2900 [file stub.pyi]
2901 from mystery import a, b as b, c as d # E: Cannot find implementation or library stub for module named "mystery" \
2902 # N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
2903
2904 [case testPackagePath]
2905 import p
2906 reveal_type(p.__path__) # N: Revealed type is "builtins.list[builtins.str]"
2907 p.m.__path__ # E: "object" has no attribute "__path__"
2908
2909 [file p/__init__.py]
2910 from . import m as m
2911 [file p/m.py]
2912 [builtins fixtures/list.pyi]
2913
2914 [case testSpecialModulesNameImplicitAttr]
2915 import typing
2916 import builtins
2917 import abc
2918
2919 reveal_type(abc.__name__) # N: Revealed type is "builtins.str"
2920 reveal_type(builtins.__name__) # N: Revealed type is "builtins.str"
2921 reveal_type(typing.__name__) # N: Revealed type is "builtins.str"
2922
2923 [case testSpecialAttrsAreAvailableInClasses]
2924 class Some:
2925 name = __name__
2926 reveal_type(Some.name) # N: Revealed type is "builtins.str"
2927
2928 [case testReExportAllInStub]
2929 from m1 import C
2930 from m1 import D # E: Module "m1" has no attribute "D"
2931 C()
2932 C(1) # E: Too many arguments for "C"
2933 [file m1.pyi]
2934 from m2 import *
2935 [file m2.pyi]
2936 from m3 import *
2937 from m3 import __all__ as __all__
2938 class D: pass
2939 [file m3.pyi]
2940 from m4 import C as C
2941 __all__ = ['C']
2942 [file m4.pyi]
2943 class C: pass
2944 [builtins fixtures/list.pyi]
2945
2946 [case testMypyPathAndPython2Dir]
2947 # flags: --config-file tmp/mypy.ini
2948 from m import f
2949 f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "str"
2950 f('x')
2951
2952 [file xx/@python2/m.pyi]
2953 def f(x: int) -> None: ...
2954
2955 [file xx/m.pyi]
2956 def f(x: str) -> None: ...
2957
2958 [file mypy.ini]
2959 \[mypy]
2960 mypy_path = tmp/xx
2961
2962 [case testImportCycleSpecialCase2]
2963 import m
2964
2965 [file m.pyi]
2966 from f import F
2967 class M: pass
2968
2969 [file f.pyi]
2970 from m import M
2971
2972 from typing import Generic, TypeVar
2973
2974 T = TypeVar("T")
2975
2976 class W(Generic[T]): ...
2977
2978 class F(M):
2979 A = W[int]
2980 x: C
2981 class C(W[F.A]): ...
2982
2983 [case testImportCycleSpecialCase3]
2984 import f
2985
2986 [file m.pyi]
2987 from f import F
2988 class M: pass
2989
2990 [file f.pyi]
2991 from m import M
2992
2993 from typing import Generic, TypeVar
2994
2995 T = TypeVar("T")
2996
2997 class F(M):
2998 x: C
2999 class C: ...
3000
3001 [case testLimitLegacyStubErrorVolume]
3002 # flags: --disallow-any-expr --soft-error-limit=5
3003 import certifi # E: Cannot find implementation or library stub for module named "certifi" \
3004 # N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
3005 certifi.x # E: Expression has type "Any"
3006 certifi.x # E: Expression has type "Any"
3007 certifi.x # E: Expression has type "Any"
3008 certifi.x # N: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)
3009 certifi.x
3010 certifi.x
3011 certifi.x
3012 certifi.x
3013
3014 [case testDoNotLimitErrorVolumeIfNotImportErrors]
3015 # flags: --disallow-any-expr --soft-error-limit=5
3016 def f(): pass
3017 certifi = f() # E: Expression has type "Any"
3018 1() # E: "int" not callable
3019 certifi.x # E: Expression has type "Any"
3020 certifi.x # E: Expression has type "Any"
3021 certifi.x # E: Expression has type "Any"
3022 certifi.x # E: Expression has type "Any"
3023 certifi.x # E: Expression has type "Any"
3024 certifi.x # E: Expression has type "Any"
3025 certifi.x # E: Expression has type "Any"
3026 certifi.x # E: Expression has type "Any"
3027 1() # E: "int" not callable
3028
3029 [case testDoNotLimitImportErrorVolume]
3030 # flags: --disallow-any-expr --soft-error-limit=3
3031 import xyz1 # E: Cannot find implementation or library stub for module named "xyz1" \
3032 # N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
3033 import xyz2 # E: Cannot find implementation or library stub for module named "xyz2"
3034 import xyz3 # E: Cannot find implementation or library stub for module named "xyz3"
3035 import xyz4 # E: Cannot find implementation or library stub for module named "xyz4"
3036
3037 [case testUnlimitedStubErrorVolume]
3038 # flags: --disallow-any-expr --soft-error-limit=-1
3039 import certifi # E: Cannot find implementation or library stub for module named "certifi" \
3040 # N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
3041 certifi.x # E: Expression has type "Any"
3042 certifi.x # E: Expression has type "Any"
3043 certifi.x # E: Expression has type "Any"
3044 certifi.x # E: Expression has type "Any"
3045 certifi.x # E: Expression has type "Any"
3046 certifi.x # E: Expression has type "Any"
3047 certifi.x # E: Expression has type "Any"
3048 certifi.x # E: Expression has type "Any"
3049 certifi.x # E: Expression has type "Any"
3050 certifi.x # E: Expression has type "Any"
3051 certifi.x # E: Expression has type "Any"
3052 certifi.x # E: Expression has type "Any"
3053 certifi.x # E: Expression has type "Any"
3054 certifi.x # E: Expression has type "Any"
3055 certifi.x # E: Expression has type "Any"
3056 certifi.x # E: Expression has type "Any"
3057 certifi.x # E: Expression has type "Any"
3058 certifi.x # E: Expression has type "Any"
3059 certifi.x # E: Expression has type "Any"
3060 certifi.x # E: Expression has type "Any"
3061 certifi.x # E: Expression has type "Any"
3062 certifi.x # E: Expression has type "Any"
3063 certifi.x # E: Expression has type "Any"
3064 certifi.x # E: Expression has type "Any"
3065 certifi.x # E: Expression has type "Any"
3066 certifi.x # E: Expression has type "Any"
3067 certifi.x # E: Expression has type "Any"
3068 certifi.x # E: Expression has type "Any"
3069 certifi.x # E: Expression has type "Any"
3070 certifi.x # E: Expression has type "Any"
3071 certifi.x # E: Expression has type "Any"
3072 certifi.x # E: Expression has type "Any"
3073 certifi.x # E: Expression has type "Any"
3074 certifi.x # E: Expression has type "Any"
3075 certifi.x # E: Expression has type "Any"
3076 certifi.x # E: Expression has type "Any"
3077 certifi.x # E: Expression has type "Any"
3078 certifi.x # E: Expression has type "Any"
3079 certifi.x # E: Expression has type "Any"
3080 certifi.x # E: Expression has type "Any"
3081
3082 [case testIgnoreErrorFromMissingStubs1]
3083 # flags: --config-file tmp/pyproject.toml
3084 import certifi
3085 from foobar1 import x
3086 import foobar2
3087 [file pyproject.toml]
3088 \[tool.mypy]
3089 ignore_missing_imports = true
3090 \[[tool.mypy.overrides]]
3091 module = "certifi"
3092 ignore_missing_imports = true
3093 \[[tool.mypy.overrides]]
3094 module = "foobar1"
3095 ignore_missing_imports = true
3096
3097 [case testIgnoreErrorFromMissingStubs2]
3098 # flags: --config-file tmp/pyproject.toml
3099 import certifi
3100 from foobar1 import x
3101 import foobar2 # E: Cannot find implementation or library stub for module named "foobar2" \
3102 # N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
3103 [file pyproject.toml]
3104 \[tool.mypy]
3105 ignore_missing_imports = false
3106 \[[tool.mypy.overrides]]
3107 module = "certifi"
3108 ignore_missing_imports = true
3109 \[[tool.mypy.overrides]]
3110 module = "foobar1"
3111 ignore_missing_imports = true
3112
3113 [case testIgnoreErrorFromGoogleCloud]
3114 # flags: --ignore-missing-imports
3115 import google.cloud
3116 from google.cloud import x
3117
3118 [case testErrorFromGoogleCloud]
3119 import google.cloud # E: Cannot find implementation or library stub for module named "google.cloud" \
3120 # E: Cannot find implementation or library stub for module named "google"
3121 from google.cloud import x
3122 import google.non_existent # E: Cannot find implementation or library stub for module named "google.non_existent"
3123 from google.non_existent import x
3124
3125 import google.cloud.ndb # E: Library stubs not installed for "google.cloud.ndb" \
•3126 # N: Hint: On Debian systems, you can install the python3-typeshed package to provide mypy with stubs for many popular libraries. In virtual Python environments, you can instead run "python3 -m pip install types-google-cloud-ndb". \
3127 # N: (or run "mypy --install-types" to install all missing stub packages) \
3128 # N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
3129 from google.cloud import ndb
3130
3131 [case testMissingSubmoduleOfInstalledStubPackage]
3132 import bleach.exists
3133 import bleach.xyz # E: Cannot find implementation or library stub for module named "bleach.xyz" \
3134 # N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
3135 from bleach.abc import fgh # E: Cannot find implementation or library stub for module named "bleach.abc"
3136 [file bleach/__init__.pyi]
3137 [file bleach/exists.pyi]
3138
3139 [case testMissingSubmoduleOfInstalledStubPackageIgnored]
3140 # flags: --ignore-missing-imports
3141 import bleach.xyz
3142 from bleach.abc import fgh
3143 [file bleach/__init__.pyi]
3144
3145 [case testCyclicUndefinedImportWithName]
3146 import a
3147 [file a.py]
3148 from b import no_such_export
3149 [file b.py]
3150 from a import no_such_export # E: Module "a" has no attribute "no_such_export"
3151
3152 [case testCyclicUndefinedImportWithStar1]
3153 import a
3154 [file a.py]
3155 from b import no_such_export
3156 [file b.py]
3157 from a import *
3158 [out]
3159 tmp/b.py:1: error: Cannot resolve name "no_such_export" (possible cyclic definition)
3160
3161 [case testCyclicUndefinedImportWithStar2]
3162 import a
3163 [file a.py]
3164 from b import no_such_export
3165 [file b.py]
3166 from c import *
3167 [file c.py]
3168 from a import *
3169 [out]
3170 tmp/c.py:1: error: Cannot resolve name "no_such_export" (possible cyclic definition)
3171
3172 [case testCyclicUndefinedImportWithStar3]
3173 import test1
3174 [file test1.py]
3175 from dir1 import *
3176 [file dir1/__init__.py]
3177 from .test2 import *
3178 [file dir1/test2.py]
3179 from test1 import aaaa # E: Module "test1" has no attribute "aaaa"
3180
3181 [case testIncompatibleOverrideFromCachedModuleIncremental]
3182 import b
3183 [file a.py]
3184 class Foo:
3185 def frobnicate(self, x: str, *args, **kwargs): pass
3186 [file b.py]
3187 from a import Foo
3188 class Bar(Foo):
3189 def frobnicate(self) -> None: pass
3190 [file b.py.2]
3191 from a import Foo
3192 class Bar(Foo):
3193 def frobnicate(self, *args: int) -> None: pass
3194 [file b.py.3]
3195 from a import Foo
3196 class Bar(Foo):
3197 def frobnicate(self, *args: int) -> None: pass # type: ignore[override] # I know
3198 [builtins fixtures/dict.pyi]
3199 [out1]
3200 tmp/b.py:3: error: Signature of "frobnicate" incompatible with supertype "a.Foo"
3201 tmp/b.py:3: note: Superclass:
3202 tmp/b.py:3: note: def frobnicate(self, x: str, *args: Any, **kwargs: Any) -> Any
3203 tmp/b.py:3: note: Subclass:
3204 tmp/b.py:3: note: def frobnicate(self) -> None
3205 [out2]
3206 tmp/b.py:3: error: Signature of "frobnicate" incompatible with supertype "a.Foo"
3207 tmp/b.py:3: note: Superclass:
3208 tmp/b.py:3: note: def frobnicate(self, x: str, *args: Any, **kwargs: Any) -> Any
3209 tmp/b.py:3: note: Subclass:
3210 tmp/b.py:3: note: def frobnicate(self, *args: int) -> None
3211
3212 [case testTypeIgnoredImportsWorkWithCacheIncremental]
3213 import a
3214 [file a.py]
3215 import b # type: ignore[import]
3216 [file b.py]
3217 [delete b.py.2]
3218 [out]
3219 [out2]
3220
3221 [case testTypeIgnoredImportsWorkWithCacheIncremental2]
3222 # flags: --warn-unused-ignores
3223 import a
3224 [file a.py]
3225 import b # type: ignore[import-not-found]
3226 [file b.py.2]
3227 [out]
3228 [out2]
3229 tmp/a.py:1: error: Unused "type: ignore" comment
3230
3231 [case testTypeIgnoredImportsWorkWithCacheIncremental3]
3232 # flags: --warn-unused-ignores
3233 import a
3234 [file a.py]
3235 import b # type: ignore[import-not-found]
3236 [file b.py]
3237 [delete b.py.2]
3238 [out]
3239 tmp/a.py:1: error: Unused "type: ignore" comment
3240 [out2]
3241
3242 [case testImportErrorStaleLoadedFromCacheIncremental]
3243 import a
3244 [file a.py]
3245 import b
3246 import c
3247 [file c.py]
3248 x: int
3249 [file c.py.2]
3250 x: str
3251 [out]
3252 tmp/a.py:1: error: Cannot find implementation or library stub for module named "b"
3253 tmp/a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
3254 [out2]
3255 tmp/a.py:1: error: Cannot find implementation or library stub for module named "b"
3256 tmp/a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports