/
check-modules.test
3124 lines (2617 loc) · 73.5 KB
/
check-modules.test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Type checker test cases dealing with modules and imports.
-- Towards the end there are tests for PEP 420 (namespace packages, i.e. __init__.py-less packages).
[case testAccessImportedDefinitions]
import m
import typing
m.f() # E: Missing positional argument "a" in call to "f"
m.f(object()) # E: Argument 1 to "f" has incompatible type "object"; expected "A"
m.x = object() # E: Incompatible types in assignment (expression has type "object", variable has type "A")
m.f(m.A())
m.x = m.A()
[file m.py]
class A: pass
def f(a: A) -> None: pass
x = A()
[case testAccessImportedDefinitions]
import m
import typing
m.f(object()) # E: Argument 1 to "f" has incompatible type "object"; expected "A"
m.f(m.A())
[file m.py]
class A: pass
def f(a: A) -> None: pass
[case testAccessImportedDefinitions2]
from m import f, A
import typing
f(object()) # E: Argument 1 to "f" has incompatible type "object"; expected "A"
f(A())
[file m.py]
class A: pass
def f(a: A) -> None: pass
[case testImportedExceptionType]
import m
import typing
try:
pass
except m.Err:
pass
except m.Bad: # E: Exception type must be derived from BaseException
pass
[file m.py]
class Err(BaseException): pass
class Bad: pass
[builtins fixtures/exception.pyi]
[case testImportedExceptionType2]
from m import Err, Bad
import typing
try:
pass
except Err:
pass
except Bad: # E: Exception type must be derived from BaseException
pass
[file m.py]
class Err(BaseException): pass
class Bad: pass
[builtins fixtures/exception.pyi]
[case testImportWithinBlock]
import typing
if 1:
import m
m.a = m.b # E: Incompatible types in assignment (expression has type "B", variable has type "A")
m.a = m.a
m.f()
m.f(m.a) # E: Too many arguments for "f"
m.a = m.A()
m.a = m.B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
[file m.py]
class A: pass
class B: pass
a = A()
b = B()
def f() -> None: pass
[case testImportWithinFunction]
import typing
def f() -> None:
from m import a, b, f, A, B
if int():
a = b \
# E: Incompatible types in assignment (expression has type "B", variable has type "A")
a = a
f()
f(a) # E: Too many arguments for "f"
a = A()
a = B() \
# E: Incompatible types in assignment (expression has type "B", variable has type "A")
[file m.py]
class A: pass
class B: pass
a = A()
b = B()
def f() -> None: pass
[out]
[case testImportWithinMethod]
import typing
class C:
def f(self) -> None:
from m import *
if int():
a = b # E: Incompatible types in assignment (expression has type "B", variable has type "A")
a = a
f()
f(a) # E: Too many arguments for "f"
a = A()
a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
[file m.py]
class A: pass
class B: pass
a = A()
b = B()
def f() -> None: pass
[out]
[case testImportWithinClassBody]
import typing
class C:
import m
m.f()
m.f(C) # E: Too many arguments for "f"
[file m.py]
def f() -> None: pass
[out]
[case testImportWithinClassBody2]
import typing
class C:
from m import f
f()
f(C) # E: Too many arguments for "f"
[file m.py]
def f() -> None: pass
[out]
[case testImportWithStub]
import _m
_m.f("hola")
[file _m.pyi]
def f(c:str) -> None: pass
[out]
[case testImportWithStubIncompatibleType]
import _m
_m.f("hola")
_m.f(12) # E: Argument 1 to "f" has incompatible type "int"; expected "str"
[file _m.py]
def f(c):
print(c)
[file _m.pyi]
def f(c:str) -> None: pass
[case testInvalidOperationsOnModules]
import m
import typing
class A: pass
m() # E: Module not callable
a = m # type: A # E: Incompatible types in assignment (expression has type Module, variable has type "A")
m + None # E: Unsupported left operand type for + (Module)
[file m.py]
[builtins fixtures/module.pyi]
[case testNameDefinedInDifferentModule]
import m, n
import typing
m.x # E: Module has no attribute "x"
[file m.py]
y = object()
[file n.py]
x = object()
[builtins fixtures/module.pyi]
[case testChainedAssignmentAndImports]
import m
i, s = None, None # type: (int, str)
if int():
i = m.x
if int():
i = m.y
if int():
s = m.x # E: Incompatible types in assignment (expression has type "int", variable has type "str")
if int():
s = m.y # E: Incompatible types in assignment (expression has type "int", variable has type "str")
[file m.py]
x = y = 1
[builtins fixtures/primitives.pyi]
[case testConditionalFunctionDefinitionAndImports]
import m
import typing
m.f(1)
m.f('x') # E: Argument 1 to "f" has incompatible type "str"; expected "int"
[file m.py]
x = object()
if x:
def f(x: int) -> None: pass
else:
def f(x: int) -> None: pass
[case testTypeCheckWithUnknownModule]
import nonexistent
None + ''
[out]
main:1: error: Cannot find implementation or library stub for module named "nonexistent"
main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:2: error: Unsupported left operand type for + ("None")
[case testTypeCheckWithUnknownModule2]
import m, nonexistent
None + ''
m.x = 1
m.x = ''
[file m.py]
x = 1
[out]
main:1: error: Cannot find implementation or library stub for module named "nonexistent"
main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:2: error: Unsupported left operand type for + ("None")
main:4: error: Incompatible types in assignment (expression has type "str", variable has type "int")
[case testTypeCheckWithUnknownModule3]
import nonexistent, m
None + ''
m.x = 1
m.x = ''
[file m.py]
x = 1
[out]
main:1: error: Cannot find implementation or library stub for module named "nonexistent"
main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:2: error: Unsupported left operand type for + ("None")
main:4: error: Incompatible types in assignment (expression has type "str", variable has type "int")
[case testTypeCheckWithUnknownModule4]
import nonexistent, another
None + ''
[out]
main:1: error: Cannot find implementation or library stub for module named "nonexistent"
main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:1: error: Cannot find implementation or library stub for module named "another"
main:2: error: Unsupported left operand type for + ("None")
[case testTypeCheckWithUnknownModule5]
import nonexistent as x
None + ''
[out]
main:1: error: Cannot find implementation or library stub for module named "nonexistent"
main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:2: error: Unsupported left operand type for + ("None")
[case testTypeCheckWithUnknownModuleUsingFromImport]
from nonexistent import x
None + ''
[out]
main:1: error: Cannot find implementation or library stub for module named "nonexistent"
main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:2: error: Unsupported left operand type for + ("None")
[case testTypeCheckWithUnknownModuleUsingImportStar]
from nonexistent import *
None + ''
[out]
main:1: error: Cannot find implementation or library stub for module named "nonexistent"
main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:2: error: Unsupported left operand type for + ("None")
[case testAccessingUnknownModule]
import xyz
xyz.foo()
xyz()
[out]
main:1: error: Cannot find implementation or library stub for module named "xyz"
main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
[case testAccessingUnknownModule2]
import xyz, bar
xyz.foo()
bar()
[out]
main:1: error: Cannot find implementation or library stub for module named "xyz"
main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:1: error: Cannot find implementation or library stub for module named "bar"
[case testAccessingUnknownModule3]
import xyz as z
xyz.foo()
z()
[out]
main:1: error: Cannot find implementation or library stub for module named "xyz"
main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:2: error: Name "xyz" is not defined
[case testAccessingNameImportedFromUnknownModule]
from xyz import y, z
y.foo()
z()
[out]
main:1: error: Cannot find implementation or library stub for module named "xyz"
main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
[case testAccessingNameImportedFromUnknownModule2]
from xyz import *
y
[out]
main:1: error: Cannot find implementation or library stub for module named "xyz"
main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:2: error: Name "y" is not defined
[case testAccessingNameImportedFromUnknownModule3]
from xyz import y as z
y
z
[out]
main:1: error: Cannot find implementation or library stub for module named "xyz"
main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:2: error: Name "y" is not defined
[case testUnknownModuleRedefinition]
# Error messages differ with the new analyzer
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
def xab(): pass # E: Name "xab" already defined (possibly by an import)
[case testAccessingUnknownModuleFromOtherModule]
import x
x.nonexistent.foo
x.z
[file x.py]
import nonexistent
[builtins fixtures/module.pyi]
[out]
tmp/x.py:1: error: Cannot find implementation or library stub for module named "nonexistent"
tmp/x.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:3: error: Module has no attribute "z"
[case testUnknownModuleImportedWithinFunction]
def f():
import foobar
def foobar(): pass
foobar('')
[out]
main:2: error: Cannot find implementation or library stub for module named "foobar"
main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:4: error: Too many arguments for "foobar"
[case testUnknownModuleImportedWithinFunction2]
def f():
from foobar import x
def x(): pass
x('')
[out]
main:2: error: Cannot find implementation or library stub for module named "foobar"
main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
main:4: error: Too many arguments for "x"
[case testRelativeImports]
import typing
import m.a
m.a.x = m.a.y # Error
[file m/__init__.py]
[file m/a.py]
import typing
from .b import A, B, x, y
z = x
if int():
z = y # Error
[file m/b.py]
import typing
class A: pass
class B: pass
x = A()
y = B()
[out]
tmp/m/a.py:5: error: Incompatible types in assignment (expression has type "B", variable has type "A")
main:3: error: Incompatible types in assignment (expression has type "B", variable has type "A")
[case testRelativeImports2]
import typing
import m.a
m.a.x = m.a.y # E: Incompatible types in assignment (expression has type "B", variable has type "A")
[file m/__init__.py]
[file m/a.py]
import typing
from .b import A, B, x, y
[file m/b.py]
import typing
class A: pass
class B: pass
x = A()
y = B()
[case testExportedValuesInImportAll]
import typing
from m import *
_ = a
_ = b
_ = c
_ = d
_ = e
_ = f # E: Name "f" is not defined
_ = _g # E: Name "_g" is not defined
[file m.py]
__all__ = ['a']
__all__ += ('b',)
__all__.append('c')
__all__.extend(('d', 'e'))
a = b = c = d = e = f = _g = 1
[builtins fixtures/module_all.pyi]
[case testAllMustBeSequenceStr]
import typing
__all__ = [1, 2, 3]
[builtins fixtures/module_all.pyi]
[out]
main:2: error: Type of __all__ must be "Sequence[str]", not "List[int]"
[case testAllMustBeSequenceStr_python2]
import typing
__all__ = [1, 2, 3]
[builtins_py2 fixtures/module_all_python2.pyi]
[out]
main:2: error: Type of __all__ must be "Sequence[unicode]", not "List[int]"
[case testAllUnicodeSequenceOK_python2]
import typing
__all__ = [u'a', u'b', u'c']
[builtins_py2 fixtures/module_all_python2.pyi]
[out]
[case testUnderscoreExportedValuesInImportAll]
import typing
from m import *
_ = a
_ = _b
_ = __c__
_ = ___d
_ = e
_ = f # E: Name "f" is not defined
_ = _g # E: Name "_g" is not defined
[file m.py]
__all__ = ['a']
__all__ += ('_b',)
__all__.append('__c__')
__all__.extend(('___d', 'e'))
a = _b = __c__ = ___d = e = f = _g = 1
[builtins fixtures/module_all.pyi]
[case testEllipsisInitializerInStubFileWithType]
import m
m.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[file m.pyi]
x = ... # type: int
[case testEllipsisInitializerInStubFileWithoutType]
import m
m.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "ellipsis")
[file m.pyi]
# Ellipsis is only special with a # type: comment (not sure though if this is great)
x = ...
[case testEllipsisInitializerInModule]
x = ... # type: int # E: Incompatible types in assignment (expression has type "ellipsis", variable has type "int")
[case testEllipsisDefaultArgValueInStub]
import m
m.f(1)
m.f('') # E: Argument 1 to "f" has incompatible type "str"; expected "int"
[file m.pyi]
def f(x: int = ...) -> None: pass
[case testEllipsisDefaultArgValueInStub2]
import m
def f1(x: int = ...) -> int: return 1
def f2(x: int = '') -> int: return 1
[file m.pyi]
def g1(x: int = ...) -> int: pass
def g2(x: int = '') -> int: pass
[out]
tmp/m.pyi:2: error: Incompatible default for argument "x" (default has type "str", argument has type "int")
main:2: error: Incompatible default for argument "x" (default has type "ellipsis", argument has type "int")
main:3: error: Incompatible default for argument "x" (default has type "str", argument has type "int")
[case testEllipsisDefaultArgValueInNonStub]
def ok_1(x: int = ...) -> None: pass
def ok_2(x: int = ...) -> None: ...
def ok_3(x: int = ...) -> None: raise NotImplementedError
def ok_4(x: int = ...) -> None: raise NotImplementedError()
def ok_5(x: int = ...) -> None:
"""Docstring here"""
pass
def bad_1(x: int = ...) -> None: 1 # E: Incompatible default for argument "x" (default has type "ellipsis", argument has type "int")
def bad_2(x: int = ...) -> None: # E: Incompatible default for argument "x" (default has type "ellipsis", argument has type "int")
"""Docstring here"""
ok_1()
def bad_3(x: int = ...) -> None: # E: Incompatible default for argument "x" (default has type "ellipsis", argument has type "int")
raise Exception("Some other exception")
[builtins fixtures/exception.pyi]
[out]
[case testEllipsisDefaultArgValueInNonStubsOverload]
from typing import overload, Union
Both = Union[int, str]
@overload
def foo(x: int, y: int = ...) -> int: ...
@overload
def foo(x: str, y: str = ...) -> str: ...
def foo(x: Both, y: Both = ...) -> Both: # E: Incompatible default for argument "y" (default has type "ellipsis", argument has type "Union[int, str]")
return x
@overload
def bar(x: int, y: int = ...) -> int: ...
@overload
def bar(x: str, y: str = ...) -> str: ...
def bar(x: Both, y: Both = ...) -> Both:
raise NotImplementedError
[builtins fixtures/exception.pyi]
[out]
[case testEllipsisDefaultArgValueInNonStubsMethods]
from typing import Generic, TypeVar
from typing_extensions import Protocol
from abc import abstractmethod
T = TypeVar('T')
class Wrap(Generic[T]): ...
class MyProtocol(Protocol):
def no_impl(self, x: Wrap[int] = ...) -> int: ...
def default_impl(self, x: Wrap[int] = ...) -> int: return 3 # E: Incompatible default for argument "x" (default has type "ellipsis", argument has type "Wrap[int]")
class MyAbstractClass:
@abstractmethod
def no_impl(self, x: Wrap[int] = ...) -> int: raise NotImplementedError
@abstractmethod
def default_impl(self, x: Wrap[int] = ...) -> int: return 3 # E: Incompatible default for argument "x" (default has type "ellipsis", argument has type "Wrap[int]")
[builtins fixtures/exception.pyi]
[out]
[case testStarImportOverlapping]
from m1 import *
from m2 import *
j = ''
[file m1.py]
x = 1
[file m2.py]
x = 1
[case testStarImportOverlappingMismatch]
from m1 import *
from m2 import * # E: Incompatible import of "x" (imported name has type "int", local name has type "str")
j = ''
[file m1.py]
x = ''
[file m2.py]
x = 1
[case testStarImportOverridingLocalImports]
from m1 import *
from m2 import *
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[file m1.py]
x = 1
[file m2.py]
x = 1
[case testAssignToFuncDefViaImport]
# flags: --strict-optional
# Errors differ with the new analyzer. (Old analyzer gave error on the
# input, which is maybe better, but no error about f, which seems
# wrong)
from m import *
f = None # E: Incompatible types in assignment (expression has type "None", variable has type "Callable[[], Any]")
x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[file m.py]
def f(): pass
x = 1+0
[out]
-- Conditional definitions and function redefinitions via module object
-- --------------------------------------------------------------------
[case testConditionalImportAndAssign]
try:
from m import x
except:
x = None
try:
from m import x as y
except:
y = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str")
[file m.py]
x = ''
[case testAssignAndConditionalImport]
x = ''
try:
from m import x
except:
pass
y = 1
try:
from m import x as y # E: Incompatible import of "y" (imported name has type "str", local name has type "int")
except:
pass
[file m.py]
x = ''
[case testAssignAndConditionalStarImport]
x = ''
y = 1
try:
from m import * # E: Incompatible import of "y" (imported name has type "str", local name has type "int")
except:
pass
[file m.py]
x = ''
y = ''
[case testRedefineImportedFunctionViaImport]
try:
from m import f, g
except:
def f(x): pass
def g(x): pass # E: All conditional function variants must have identical signatures
[file m.py]
def f(x): pass
def g(x, y): pass
[case testImportedVariableViaImport]
try:
from m import x
except:
from n import x # E: Incompatible import of "x" (imported name has type "str", local name has type "int")
[file m.py]
x = 1
[file n.py]
x = ''
[case testRedefineFunctionViaImport]
def f(x): pass
def g(x): pass
try:
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]")
except:
pass
[file m.py]
def f(x): pass
def g(x, y): pass
[case testImportVariableAndAssignNone]
try:
from m import x
except:
x = None
[file m.py]
x = 1
[case testImportFunctionAndAssignNone]
try:
from m import f
except:
f = None
[file m.py]
def f(): pass
[case testImportFunctionAndAssignFunction]
def g(x): pass
try:
from m import f
except:
f = g
[file m.py]
def f(x): pass
[case testImportFunctionAndAssignIncompatible]
try:
from m import f
except:
f = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Callable[[], Any]")
[file m.py]
def f(): pass
[case testAssignToFuncDefViaGlobalDecl2]
import typing
from m import f
def g() -> None:
global f
f = None
if int():
f = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Callable[[], Any]")
[file m.py]
def f(): pass
[out]
[case testAssignToFuncDefViaNestedModules]
import m.n
m.n.f = None
m.n.f = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Callable[[], Any]")
[file m/__init__.py]
[file m/n.py]
def f(): pass
[out]
[case testAssignToFuncDefViaModule]
import m
m.f = None
m.f = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "Callable[[], Any]")
[file m.py]
def f(): pass
[out]
[case testConditionalImportAndAssignNoneToModule]
if object():
import m
else:
m = None
m.f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "str"
[file m.py]
def f(x: str) -> None: pass
[builtins fixtures/module.pyi]
[out]
[case testConditionalImportAndAssignInvalidToModule]
if object():
import m
else:
m = 1 # E: Incompatible types in assignment (expression has type "int", variable has type Module)
[file m.py]
[builtins fixtures/module.pyi]
[out]
[case testImportAndAssignToModule]
import m
m = None
m.f(1) # E: Argument 1 to "f" has incompatible type "int"; expected "str"
[file m.py]
def f(x: str) -> None: pass
[builtins fixtures/module.pyi]
[out]
-- Test cases that simulate 'mypy -m modname'
--
-- The module name to import is encoded in a comment.
[case testTypeCheckNamedModule]
# cmd: mypy -m m.a
[file m/__init__.py]
None + 1
[file m/a.py]
[out]
tmp/m/__init__.py:1: error: Unsupported left operand type for + ("None")
[case testTypeCheckNamedModule2]
# cmd: mypy -m m.a
[file m/__init__.py]
[file m/a.py]
None + 1
[out]
tmp/m/a.py:1: error: Unsupported left operand type for + ("None")
[case testTypeCheckNamedModule3]
# cmd: mypy -m m
[file m/__init__.py]
None + 1
[file m/a.py]
[out]
tmp/m/__init__.py:1: error: Unsupported left operand type for + ("None")
[case testTypeCheckNamedModule4]
# cmd: mypy -m m
[file m/__init__.py]
[file m/a.py]
None + 1 # Not analyzed.
[out]
[case testTypeCheckNamedModule5]
# cmd: mypy -m m
None + '' # Not analyzed.
[file m.py]
None + 1
[out]
tmp/m.py:1: error: Unsupported left operand type for + ("None")
[case testTypeCheckNamedModuleWithImportCycle]
# cmd: mypy -m m.a
None + 1 # Does not generate error, as this file won't be analyzed.
[file m/__init__.py]
import m.a
[file m/a.py]
[out]
[case testCheckDecoratedFuncAsAnnotWithImportCycle]
import a
[file a.py]
from typing import TypeVar
import b
T = TypeVar('T')
def idf(x: T) -> T: return x
@idf
def Session() -> None: pass
[file b.py]
MYPY = False
if MYPY:
from a import Session
def f(self, session: Session) -> None: # E: Function "a.Session" is not valid as a type \
# N: Perhaps you need "Callable[...]" or a callback protocol?
pass
[builtins fixtures/bool.pyi]
-- Checks dealing with submodules and different kinds of imports
-- -------------------------------------------------------------
[case testSubmoduleRegularImportAddsAllParents]
import a.b.c
reveal_type(a.value) # N: Revealed type is "builtins.int"
reveal_type(a.b.value) # N: Revealed type is "builtins.str"
reveal_type(a.b.c.value) # N: Revealed type is "builtins.float"
b.value # E: Name "b" is not defined
c.value # E: Name "c" is not defined
[file a/__init__.py]
value = 3
[file a/b/__init__.py]
value = "a"
[file a/b/c.py]
value = 3.2
[out]
[case testSubmoduleImportAsDoesNotAddParents]
import a.b.c as foo
reveal_type(foo.value) # N: Revealed type is "builtins.float"
a.value # E: Name "a" is not defined
b.value # E: Name "b" is not defined
c.value # E: Name "c" is not defined
[file a/__init__.py]
value = 3
[file a/b/__init__.py]
value = "a"
[file a/b/c.py]
value = 3.2
[out]
[case testSubmoduleImportFromDoesNotAddParents]
from a import b
reveal_type(b.value) # N: Revealed type is "builtins.str"
b.c.value # E: Module has no attribute "c"
a.value # E: Name "a" is not defined
[file a/__init__.py]
value = 3
[file a/b/__init__.py]
value = "a"
[file a/b/c.py]
value = 3.2
[builtins fixtures/module.pyi]
[out]
[case testSubmoduleImportFromDoesNotAddParents2]
from a.b import c
reveal_type(c.value) # N: Revealed type is "builtins.float"
a.value # E: Name "a" is not defined
b.value # E: Name "b" is not defined
[file a/__init__.py]
value = 3
[file a/b/__init__.py]
value = "a"
[file a/b/c.py]
value = 3.2
[out]
[case testSubmoduleRegularImportNotDirectlyAddedToParent]
import a.b.c
def accept_float(x: float) -> None: pass
accept_float(a.b.c.value)
[file a/__init__.py]
value = 3
b.value
a.b.value
[file a/b/__init__.py]
value = "a"
c.value
a.b.c.value
[file a/b/c.py]
value = 3.2
[out]
tmp/a/b/__init__.py:2: error: Name "c" is not defined
tmp/a/b/__init__.py:3: error: Name "a" is not defined
tmp/a/__init__.py:2: error: Name "b" is not defined
tmp/a/__init__.py:3: error: Name "a" is not defined
[case testSubmoduleMixingLocalAndQualifiedNames]
from a.b import MyClass
val1 = None # type: a.b.MyClass # E: Name "a" is not defined
val2 = None # type: MyClass
[file a/__init__.py]
[file a/b.py]
class MyClass: pass
[out]
[case testSubmoduleMixingImportFrom]
import parent.child
[file parent/__init__.py]
[file parent/common.py]
class SomeClass: pass
[file parent/child.py]
from parent.common import SomeClass
from parent import common
foo = parent.common.SomeClass()
[builtins fixtures/module.pyi]
[out]
tmp/parent/child.py:3: error: Name "parent" is not defined
[case testSubmoduleMixingImportFromAndImport]
import parent.child
[file parent/__init__.py]
[file parent/common.py]
class SomeClass: pass
[file parent/unrelated.py]
class ShouldNotLoad: pass
[file parent/child.py]
from parent.common import SomeClass
import parent
# Note, since this might be unintuitive -- when `parent.common` is loaded in any way,
# shape, or form, it's added to `parent`'s namespace, which is why the below line
# succeeds.
foo = parent.common.SomeClass()
reveal_type(foo)
bar = parent.unrelated.ShouldNotLoad()
[builtins fixtures/module.pyi]
[out]
tmp/parent/child.py:8: note: Revealed type is "parent.common.SomeClass"
tmp/parent/child.py:9: error: Module has no attribute "unrelated"
[case testSubmoduleMixingImportFromAndImport2]
import parent.child
[file parent/__init__.py]
[file parent/common.py]
class SomeClass: pass
[file parent/child.py]
from parent import common
import parent
foo = parent.common.SomeClass()
reveal_type(foo)
[builtins fixtures/module.pyi]
[out]
tmp/parent/child.py:4: note: Revealed type is "parent.common.SomeClass"
-- Tests repeated imports
[case testIdenticalImportFromTwice]
from a import x, y, z
from b import x, y, z
[file a.py]
from common import x, y, z
[file b.py]
from common import x, y, z