-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
check-classes.test
8172 lines (6717 loc) · 217 KB
/
check-classes.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
-- Methods
-- -------
[case testMethodCall]
class A:
def foo(self, x: 'A') -> None: pass
class B:
def bar(self, x: 'B', y: A) -> None: pass
a: A
b: B
a.foo(B()) # E: Argument 1 to "foo" of "A" has incompatible type "B"; expected "A"
a.bar(B(), A()) # E: "A" has no attribute "bar"
a.foo(A())
b.bar(B(), A())
[case testMethodCallWithSubtype]
class A:
def foo(self, x: 'A') -> None: pass
def bar(self, x: 'B') -> None: pass
class B(A): pass
a: A
a.foo(A())
a.foo(B())
a.bar(A()) # E: Argument 1 to "bar" of "A" has incompatible type "A"; expected "B"
a.bar(B())
[case testInheritingMethod]
class A:
def foo(self, x: 'B') -> None: pass
class B(A): pass
a: B
a.foo(A()) # Fail
a.foo(B())
[targets __main__, __main__.A.foo]
[out]
main:6: error: Argument 1 to "foo" of "A" has incompatible type "A"; expected "B"
[case testMethodCallWithInvalidNumberOfArguments]
class A:
def foo(self, x: 'A') -> None: pass
a: A
a.foo() # Fail
a.foo(object(), A()) # Fail
[out]
main:5: error: Missing positional argument "x" in call to "foo" of "A"
main:6: error: Too many arguments for "foo" of "A"
main:6: error: Argument 1 to "foo" of "A" has incompatible type "object"; expected "A"
[case testMethodBody]
import typing
class A:
def f(self) -> None:
a = object() # type: A # Fail
[out]
main:4: error: Incompatible types in assignment (expression has type "object", variable has type "A")
[case testMethodArguments]
import typing
class A:
def f(self, a: 'A', b: 'B') -> None:
if int():
a = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
b = A() # E: Incompatible types in assignment (expression has type "A", variable has type "B")
a = A()
b = B()
a = a
a = b # E: Incompatible types in assignment (expression has type "B", variable has type "A")
class B: pass
[out]
[case testReturnFromMethod]
import typing
class A:
def f(self) -> 'A':
return B() # Fail
return A()
class B: pass
[out]
main:4: error: Incompatible return value type (got "B", expected "A")
[case testSelfArgument]
import typing
class A:
def f(self) -> None:
o = self # type: B # Fail
self.g() # Fail
a = self # type: A
self.f()
class B: pass
[out]
main:4: error: Incompatible types in assignment (expression has type "A", variable has type "B")
main:5: error: "A" has no attribute "g"
[case testAssignToMethodViaInstance]
import typing
class A:
def f(self): pass
A().f = None # E: Cannot assign to a method \
# E: Incompatible types in assignment (expression has type "None", variable has type "Callable[[], Any]")
[case testOverrideAttributeWithMethod]
# This was crashing:
# https://github.com/python/mypy/issues/10134
from typing import Protocol
class Base:
__hash__ = None
class Derived(Base):
def __hash__(self) -> int: # E: Signature of "__hash__" incompatible with supertype "Base" \
# N: Superclass: \
# N: None \
# N: Subclass: \
# N: def __hash__(self) -> int
pass
# Correct:
class CallableProtocol(Protocol):
def __call__(self, arg: int) -> int:
pass
class CorrectBase:
attr: CallableProtocol
class CorrectDerived(CorrectBase):
def attr(self, arg: int) -> int:
pass
[case testOverrideMethodWithAttribute]
# The reverse should not crash as well:
from typing import Callable
class Base:
def __hash__(self) -> int:
pass
class Derived(Base):
__hash__ = 1 # E: Incompatible types in assignment (expression has type "int", base class "Base" defined the type as "Callable[[Base], int]")
[case testOverridePartialAttributeWithMethod]
# This was crashing: https://github.com/python/mypy/issues/11686.
class Base:
def __init__(self, arg: int):
self.partial_type = [] # E: Need type annotation for "partial_type" (hint: "partial_type: List[<type>] = ...")
self.force_deferral = []
# Force inference of the `force_deferral` attribute in `__init__` to be
# deferred to a later pass by providing a definition in another context,
# which means `partial_type` remains only partially inferred.
force_deferral = [] # E: Need type annotation for "force_deferral" (hint: "force_deferral: List[<type>] = ...")
class Derived(Base):
def partial_type(self) -> int: # E: Signature of "partial_type" incompatible with supertype "Base" \
# N: Superclass: \
# N: List[Any] \
# N: Subclass: \
# N: def partial_type(self) -> int
...
-- Attributes
-- ----------
[case testReferToInvalidAttribute]
class A:
def __init__(self) -> None:
self.x = object()
a: A
a.y # E: "A" has no attribute "y"
a.y = object() # E: "A" has no attribute "y"
a.x
a.x = object()
[case testReferToInvalidAttributeUnannotatedInit]
class A:
def __init__(self):
self.x = object()
a: A
a.y # E: "A" has no attribute "y"
a.y = object() # E: "A" has no attribute "y"
a.x
a.x = object()
[case testArgumentTypeInference]
class A:
def __init__(self, aa: 'A', bb: 'B') -> None:
self.a = aa
self.b = bb
class B: pass
a: A
b: B
a.a = b # Fail
a.b = a # Fail
b.a # Fail
a.a = a
a.b = b
[out]
main:9: error: Incompatible types in assignment (expression has type "B", variable has type "A")
main:10: error: Incompatible types in assignment (expression has type "A", variable has type "B")
main:11: error: "B" has no attribute "a"
[case testExplicitAttributeInBody]
class A:
x: A
a: A
a.x = object() # E: Incompatible types in assignment (expression has type "object", variable has type "A")
a.x = A()
[case testAttributeDefinedInNonInitMethod]
import typing
class A:
def f(self) -> None:
self.x = 1
self.y = ''
self.x = 1
a = A()
a.x = 1
a.y = ''
a.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
a.z = 0 # E: "A" has no attribute "z"
[case testInheritanceAndAttributeAssignment]
import typing
class A:
def f(self) -> None:
self.x = 0
class B(A):
def f(self) -> None:
self.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
[targets __main__, __main__.A.f, __main__.B.f]
[case testAssignmentToAttributeInMultipleMethods]
import typing
class A:
def f(self) -> None:
self.x = 0
def g(self) -> None:
self.x = '' # Fail
def __init__(self) -> None:
self.x = '' # Fail
[out]
main:6: error: Incompatible types in assignment (expression has type "str", variable has type "int")
main:8: error: Incompatible types in assignment (expression has type "str", variable has type "int")
[case testClassNamesDefinedOnSelUsedInClassBody]
class A(object):
def f(self):
self.attr = 1
attr = 0
class B(object):
attr = 0
def f(self):
self.attr = 1
class C(object):
attr = 0
def f(self):
self.attr = 1
attr = 0
class D(object):
def g(self):
self.attr = 1
attr = 0
def f(self):
self.attr = 1
[out]
[case testClassNamesDefinedOnSelUsedInClassBodyReveal]
class A(object):
def f(self) -> None:
self.attr = 1
attr # E: Name "attr" is not defined
class B(object):
attr = 0
def f(self) -> None:
reveal_type(self.attr) # N: Revealed type is "builtins.int"
[out]
-- Method overriding
-- -----------------
[case testMethodOverridingWithIdenticalSignature]
import typing
class A:
def f(self, x: 'A') -> None: pass
def g(self, x: 'B' , y: object) -> 'A': pass
def h(self) -> None: pass
class B(A):
def f(self, x: A) -> None: pass
def g(self, x: 'B' , y: object) -> A: pass
def h(self) -> None: pass
[out]
[case testMethodOverridingWithCovariantType]
import typing
class A:
def f(self, x: 'A', y: 'B') -> 'A': pass
def g(self, x: 'A', y: 'B') -> 'A': pass
class B(A):
def f(self, x: A, y: 'B') -> 'B': pass
def g(self, x: A, y: A) -> 'A': pass
[out]
[case testMethodOverridingWithIncompatibleTypes]
import typing
class A:
def f(self, x: 'A', y: 'B') -> 'A': pass
def g(self, x: 'A', y: 'B') -> 'A': pass
def h(self, x: 'A', y: 'B') -> 'A': pass
class B(A):
def f(self, x: 'B', y: 'B') -> A: pass # Fail
def g(self, x: A, y: A) -> A: pass
def h(self, x: A, y: 'B') -> object: pass # Fail
[out]
main:7: error: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "A"
main:7: note: This violates the Liskov substitution principle
main:7: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
main:9: error: Return type "object" of "h" incompatible with return type "A" in supertype "A"
[case testMethodOverridingWithIncompatibleTypesOnMultipleLines]
class A:
def f(self, x: int, y: str) -> None: pass
class B(A):
def f(
self,
x: int,
y: bool,
) -> None:
pass
[out]
main:7: error: Argument 2 of "f" is incompatible with supertype "A"; supertype defines the argument type as "str"
main:7: note: This violates the Liskov substitution principle
main:7: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
[case testMultiLineMethodOverridingWithIncompatibleTypesIgnorableAtArgument]
class A:
def f(self, x: int, y: str) -> None: pass
class B(A):
def f(
self,
x: int,
y: bool, # type: ignore[override]
) -> None:
pass
[case testMultiLineMethodOverridingWithIncompatibleTypesIgnorableAtDefinition]
class A:
def f(self, x: int, y: str) -> None: pass
class B(A):
def f( # type: ignore[override]
self,
x: int,
y: bool,
) -> None:
pass
[case testMultiLineMethodOverridingWithIncompatibleTypesWrongIgnore]
class A:
def f(self, x: int, y: str) -> None: pass
class B(A):
def f( # type: ignore[return-type]
self,
x: int,
y: bool,
) -> None:
pass
[out]
main:7: error: Argument 2 of "f" is incompatible with supertype "A"; supertype defines the argument type as "str"
main:7: note: This violates the Liskov substitution principle
main:7: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
[case testEqMethodsOverridingWithNonObjects]
class A:
def __eq__(self, other: A) -> bool: pass # Fail
[builtins fixtures/plugin_attrs.pyi]
[out]
main:2: error: Argument 1 of "__eq__" is incompatible with supertype "object"; supertype defines the argument type as "object"
main:2: note: This violates the Liskov substitution principle
main:2: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
main:2: note: It is recommended for "__eq__" to work with arbitrary objects, for example:
main:2: note: def __eq__(self, other: object) -> bool:
main:2: note: if not isinstance(other, A):
main:2: note: return NotImplemented
main:2: note: return <logic to compare two A instances>
[case testMethodOverridingWithIncompatibleArgumentCount]
import typing
class A:
def f(self, x: 'A') -> None: pass
def g(self, x: 'A', y: 'B') -> 'A': pass
class B(A):
def f(self, x: A, y: A) -> None: pass # Fail
def g(self, x: A) -> A: pass # Fail
[out]
main:6: error: Signature of "f" incompatible with supertype "A"
main:6: note: Superclass:
main:6: note: def f(self, x: A) -> None
main:6: note: Subclass:
main:6: note: def f(self, x: A, y: A) -> None
main:7: error: Signature of "g" incompatible with supertype "A"
main:7: note: Superclass:
main:7: note: def g(self, x: A, y: B) -> A
main:7: note: Subclass:
main:7: note: def g(self, x: A) -> A
[case testMethodOverridingAcrossDeepInheritanceHierarchy1]
import typing
class A:
def f(self, x: 'B') -> None: pass
class B(A): pass
class C(B): # with gap in implementations
def f(self, x: 'C') -> None: # Fail
pass
[out]
main:6: error: Argument 1 of "f" is incompatible with supertype "A"; supertype defines the argument type as "B"
main:6: note: This violates the Liskov substitution principle
main:6: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
[case testMethodOverridingAcrossDeepInheritanceHierarchy2]
import typing
class A:
def f(self) -> 'B': pass
class B(A):
def f(self) -> 'C': pass
class C(B): # with multiple implementations
def f(self) -> B: # Fail
pass
[out]
main:7: error: Return type "B" of "f" incompatible with return type "C" in supertype "B"
[case testMethodOverridingWithVoidReturnValue]
import typing
class A:
def f(self) -> None: pass
def g(self) -> 'A': pass
class B(A):
def f(self) -> A: pass # Fail
def g(self) -> None: pass # Fail
[out]
main:6: error: Return type "A" of "f" incompatible with return type "None" in supertype "A"
main:7: error: Return type "None" of "g" incompatible with return type "A" in supertype "A"
[case testOverride__new__WithDifferentSignature]
class A:
def __new__(cls, x: int) -> A:
pass
class B(A):
def __new__(cls) -> B:
pass
[case testOverride__new__AndCallObject]
from typing import TypeVar, Generic
class A:
def __new__(cls, x: int) -> 'A':
return object.__new__(cls)
T = TypeVar('T')
class B(Generic[T]):
def __new__(cls, foo: T) -> 'B[T]':
x = object.__new__(cls)
# object.__new__ doesn't have a great type :(
reveal_type(x) # N: Revealed type is "Any"
return x
[builtins fixtures/__new__.pyi]
[case testInnerFunctionNotOverriding]
class A:
def f(self) -> int: pass
class B(A):
def g(self) -> None:
def f(self) -> str: pass
[case testOverride__init_subclass__WithDifferentSignature]
class A:
def __init_subclass__(cls, x: int) -> None: pass
class B(A): # E: Missing positional argument "x" in call to "__init_subclass__" of "A"
def __init_subclass__(cls) -> None: pass
[case testOverrideWithDecorator]
from typing import Callable
def int_to_none(f: Callable[..., int]) -> Callable[..., None]: ...
def str_to_int(f: Callable[..., str]) -> Callable[..., int]: ...
class A:
def f(self) -> None: pass
def g(self) -> str: pass
def h(self) -> None: pass
class B(A):
@int_to_none
def f(self) -> int: pass
@str_to_int
def g(self) -> str: pass # Fail
@int_to_none
@str_to_int
def h(self) -> str: pass
[out]
main:15: error: Signature of "g" incompatible with supertype "A"
main:15: note: Superclass:
main:15: note: def g(self) -> str
main:15: note: Subclass:
main:15: note: def g(*Any, **Any) -> int
[case testOverrideDecorated]
from typing import Callable
def str_to_int(f: Callable[..., str]) -> Callable[..., int]: ...
class A:
@str_to_int
def f(self) -> str: pass
@str_to_int
def g(self) -> str: pass
@str_to_int
def h(self) -> str: pass
class B(A):
def f(self) -> int: pass
def g(self) -> str: pass # Fail
@str_to_int
def h(self) -> str: pass
[out]
main:15: error: Signature of "g" incompatible with supertype "A"
main:15: note: Superclass:
main:15: note: def g(*Any, **Any) -> int
main:15: note: Subclass:
main:15: note: def g(self) -> str
[case testOverrideWithDecoratorReturningAny]
def dec(f): pass
class A:
def f(self) -> str: pass
class B(A):
@dec
def f(self) -> int: pass
[case testOverrideWithDecoratorReturningInstance]
def dec(f) -> str: pass
class A:
def f(self) -> str: pass
@dec
def g(self) -> int: pass
@dec
def h(self) -> int: pass
class B(A):
@dec
def f(self) -> int: pass # E: Signature of "f" incompatible with supertype "A" \
# N: Superclass: \
# N: def f(self) -> str \
# N: Subclass: \
# N: str
def g(self) -> int: pass # E: Signature of "g" incompatible with supertype "A" \
# N: Superclass: \
# N: str \
# N: Subclass: \
# N: def g(self) -> int
@dec
def h(self) -> str: pass
[case testOverrideIncompatibleWithMultipleSupertypes]
class A:
def f(self, *, a: int) -> None:
return
class B(A):
def f(self, *, b: int) -> None: # E: Signature of "f" incompatible with supertype "A" \
# N: Superclass: \
# N: def f(self, *, a: int) -> None \
# N: Subclass: \
# N: def f(self, *, b: int) -> None
return
class C(B):
def f(self, *, c: int) -> None: # E: Signature of "f" incompatible with supertype "B" \
# N: Superclass: \
# N: def f(self, *, b: int) -> None \
# N: Subclass: \
# N: def f(self, *, c: int) -> None \
# E: Signature of "f" incompatible with supertype "A" \
# N: Superclass: \
# N: def f(self, *, a: int) -> None \
# N: Subclass: \
# N: def f(self, *, c: int) -> None
return
[case testOverrideStaticMethodWithStaticMethod]
class A:
@staticmethod
def f(x: int, y: str) -> None: pass
@staticmethod
def g(x: int, y: str) -> None: pass
class B(A):
@staticmethod
def f(x: int, y: str) -> None: pass
@staticmethod
def g(x: str, y: str) -> None: pass # E: Argument 1 of "g" is incompatible with supertype "A"; supertype defines the argument type as "int" \
# N: This violates the Liskov substitution principle \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
[builtins fixtures/classmethod.pyi]
[case testOverrideClassMethodWithClassMethod]
class A:
@classmethod
def f(cls, x: int, y: str) -> None: pass
@classmethod
def g(cls, x: int, y: str) -> None: pass
class B(A):
@classmethod
def f(cls, x: int, y: str) -> None: pass
@classmethod
def g(cls, x: str, y: str) -> None: pass # E: Argument 1 of "g" is incompatible with supertype "A"; supertype defines the argument type as "int" \
# N: This violates the Liskov substitution principle \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
[builtins fixtures/classmethod.pyi]
[case testOverrideClassMethodWithStaticMethod]
class A:
@classmethod
def f(cls, x: int) -> None: pass
@classmethod
def g(cls, x: int) -> int: pass
@classmethod
def h(cls) -> int: pass
class B(A):
@staticmethod
def f(x: int) -> None: pass
@staticmethod
def g(x: str) -> int: pass # E: Argument 1 of "g" is incompatible with supertype "A"; supertype defines the argument type as "int" \
# N: This violates the Liskov substitution principle \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
@staticmethod
def h() -> int: pass
[builtins fixtures/classmethod.pyi]
[case testOverrideStaticMethodWithClassMethod]
class A:
@staticmethod
def f(x: int) -> None: pass
@staticmethod
def g(x: str) -> int: pass
@staticmethod
def h() -> int: pass
class B(A):
@classmethod
def f(cls, x: int) -> None: pass
@classmethod
def g(cls, x: int) -> int: pass # E: Argument 1 of "g" is incompatible with supertype "A"; supertype defines the argument type as "str" \
# N: This violates the Liskov substitution principle \
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
@classmethod
def h(cls) -> int: pass
[builtins fixtures/classmethod.pyi]
[case testAllowCovarianceInReadOnlyAttributes]
from typing import Callable, TypeVar
T = TypeVar('T')
class X:
pass
class Y(X):
pass
def dec(f: Callable[..., T]) -> T: pass
class A:
@dec
def f(self) -> X: pass
class B(A):
@dec
def f(self) -> Y: pass
[case testOverrideCallableAttributeWithMethod]
from typing import Callable
class A:
f1: Callable[[str], None]
f2: Callable[[str], None]
f3: Callable[[str], None]
class B(A):
def f1(self, x: object) -> None:
pass
@classmethod
def f2(cls, x: object) -> None:
pass
@staticmethod
def f3(x: object) -> None:
pass
[builtins fixtures/classmethod.pyi]
[case testOverrideCallableAttributeWithMethodMutableOverride]
# flags: --enable-error-code=mutable-override
from typing import Callable
class A:
f1: Callable[[str], None]
f2: Callable[[str], None]
f3: Callable[[str], None]
class B(A):
def f1(self, x: object) -> None: pass # E: Covariant override of a mutable attribute (base class "A" defined the type as "Callable[[str], None]", override has type "Callable[[object], None]")
@classmethod
def f2(cls, x: object) -> None: pass # E: Covariant override of a mutable attribute (base class "A" defined the type as "Callable[[str], None]", override has type "Callable[[object], None]")
@staticmethod
def f3(x: object) -> None: pass # E: Covariant override of a mutable attribute (base class "A" defined the type as "Callable[[str], None]", override has type "Callable[[object], None]")
[builtins fixtures/classmethod.pyi]
[case testOverrideCallableAttributeWithSettableProperty]
from typing import Callable
class A:
f: Callable[[str], None]
class B(A):
@property
def f(self) -> Callable[[object], None]: pass
@func.setter
def f(self, x: object) -> None: pass
[builtins fixtures/property.pyi]
[case testOverrideCallableAttributeWithSettablePropertyMutableOverride]
# flags: --enable-error-code=mutable-override
from typing import Callable
class A:
f: Callable[[str], None]
class B(A):
@property # E: Covariant override of a mutable attribute (base class "A" defined the type as "Callable[[str], None]", override has type "Callable[[object], None]")
def f(self) -> Callable[[object], None]: pass
@func.setter
def f(self, x: object) -> None: pass
[builtins fixtures/property.pyi]
[case testOverrideCallableUnionAttributeWithMethod]
from typing import Callable, Union
class A:
f1: Union[Callable[[str], str], str]
f2: Union[Callable[[str], str], str]
f3: Union[Callable[[str], str], str]
f4: Union[Callable[[str], str], str]
class B(A):
def f1(self, x: str) -> str:
pass
def f2(self, x: object) -> str:
pass
@classmethod
def f3(cls, x: str) -> str:
pass
@staticmethod
def f4(x: str) -> str:
pass
[builtins fixtures/classmethod.pyi]
[case testOverrideCallableUnionAttributeWithMethodMutableOverride]
# flags: --enable-error-code=mutable-override
from typing import Callable, Union
class A:
f1: Union[Callable[[str], str], str]
f2: Union[Callable[[str], str], str]
f3: Union[Callable[[str], str], str]
f4: Union[Callable[[str], str], str]
class B(A):
def f1(self, x: str) -> str: # E: Covariant override of a mutable attribute (base class "A" defined the type as "Union[Callable[[str], str], str]", override has type "Callable[[str], str]")
pass
def f2(self, x: object) -> str: # E: Covariant override of a mutable attribute (base class "A" defined the type as "Union[Callable[[str], str], str]", override has type "Callable[[object], str]")
pass
@classmethod
def f3(cls, x: str) -> str: # E: Covariant override of a mutable attribute (base class "A" defined the type as "Union[Callable[[str], str], str]", override has type "Callable[[str], str]")
pass
@staticmethod
def f4(x: str) -> str: # E: Covariant override of a mutable attribute (base class "A" defined the type as "Union[Callable[[str], str], str]", override has type "Callable[[str], str]")
pass
[builtins fixtures/classmethod.pyi]
-- Constructors
-- ------------
[case testTrivialConstructor]
class A:
def __init__(self) -> None: pass
a = A() # type: A
b = A() # type: B # E: Incompatible types in assignment (expression has type "A", variable has type "B")
class B: pass
[case testConstructor]
class A:
def __init__(self, x: 'B') -> None: pass
class B: pass
a = A(B()) # type: A
aa = A(object()) # type: A # E: Argument 1 to "A" has incompatible type "object"; expected "B"
b = A(B()) # type: B # E: Incompatible types in assignment (expression has type "A", variable has type "B")
[case testConstructorWithTwoArguments]
class A:
def __init__(self, x: 'B', y: 'C') -> None: pass
class B: pass
class C(B): pass
a = A(C(), B()) # type: A # E: Argument 2 to "A" has incompatible type "B"; expected "C"
[case testInheritedConstructor]
class B(A): pass
class C: pass
class D: pass
b = B(C()) # type: B
a = B(D()) # type: A # E: Argument 1 to "B" has incompatible type "D"; expected "C"
class A:
def __init__(self, x: 'C') -> None: pass
[case testOverridingWithIncompatibleConstructor]
class A:
def __init__(self, x: 'C') -> None: pass
class B(A):
def __init__(self) -> None: pass
class C: pass
A() # E: Missing positional argument "x" in call to "A"
B(C()) # E: Too many arguments for "B"
A(C())
B()
[case testConstructorWithReturnValueType]
import typing
class A:
def __init__(self) -> 'A': pass
[out]
main:3: error: The return type of "__init__" must be None
[case testConstructorWithImplicitReturnValueType]
import typing
class A:
def __init__(self, x: int): pass
[out]
[case testDecoratedConstructorWithImplicitReturnValueType]
import typing
from typing import Callable
def deco(fn: Callable) -> Callable:
return fn
class A:
@deco
def __init__(self, x: int): pass
[out]
[case testOverloadedConstructorWithImplicitReturnValueType]
from foo import *
[file foo.pyi]
from typing import overload
class Foo:
@overload
def __init__(self, a: int):
pass
@overload
def __init__(self, a: str):
pass
[case testConstructorWithAnyReturnValueType]
import typing
from typing import Any
class A:
def __init__(self) -> Any: pass # E: The return type of "__init__" must be None
[case testDecoratedConstructorWithAnyReturnValueType]
import typing
from typing import Callable, Any
def deco(fn: Callable) -> Callable:
return fn
class A:
@deco
def __init__(self) -> Any: pass # E: The return type of "__init__" must be None
[case testOverloadedConstructorWithAnyReturnValueType]
from foo import *
[file foo.pyi]
from typing import overload, Any
class Foo:
@overload
def __init__(self, a: int) -> Any: # E: The return type of "__init__" must be None
pass
@overload
def __init__(self, a: str) -> Any: # E: The return type of "__init__" must be None
pass
[case testInitSubclassWithReturnValueType]
import typing
class A:
def __init_subclass__(cls) -> 'A': pass
[out]
main:3: error: The return type of "__init_subclass__" must be None
[case testInitSubclassWithImplicitReturnValueType]
import typing
class A:
def __init_subclass__(cls, x: int=1): pass
[out]
[case testDecoratedInitSubclassWithImplicitReturnValueType]
import typing
from typing import Callable
def deco(fn: Callable) -> Callable:
return fn
class A:
@deco
def __init_subclass__(cls, x: int=1): pass
[out]
[case testOverloadedInitSubclassWithImplicitReturnValueType]
from foo import *
[file foo.pyi]
from typing import overload
class Foo:
@overload
def __init_subclass__(cls, a: int):
pass
@overload
def __init_subclass__(cls, a: str):
pass
[case testInitSubclassWithAnyReturnValueType]
import typing
from typing import Any
class A:
def __init_subclass__(cls) -> Any: pass # E: The return type of "__init_subclass__" must be None
[case testDecoratedInitSubclassWithAnyReturnValueType]
import typing
from typing import Callable, Any
def deco(fn: Callable) -> Callable:
return fn
class A:
@deco