File: check-generic-subtyping.test

package info (click to toggle)
mypy 1.19.1-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 22,464 kB
  • sloc: python: 114,757; ansic: 13,343; cpp: 11,380; makefile: 254; sh: 31
file content (1236 lines) | stat: -rw-r--r-- 34,574 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
-- Test cases for the type checker related to subtyping and inheritance with
-- generics.


-- Subtyping + inheritance
-- -----------------------


[case testSubtypingAndInheritingNonGenericTypeFromGenericType]
from typing import TypeVar, Generic
T = TypeVar('T')
ac: A[C]
ad: A[D]
b: B

if int():
    b = ad # E: Incompatible types in assignment (expression has type "A[D]", variable has type "B")
    ad = b # E: Incompatible types in assignment (expression has type "B", variable has type "A[D]")
if int():
    b = ac # E: Incompatible types in assignment (expression has type "A[C]", variable has type "B")

if int():
    b = b
    ac = b

class C: pass
class A(Generic[T]): pass
class B(A[C]): pass
class D: pass

[case testSubtypingAndInheritingGenericTypeFromNonGenericType]
from typing import TypeVar, Generic
T = TypeVar('T')
a: A
bc: B[C]
bd: B[D]

if int():
    bc = bd # E: Incompatible types in assignment (expression has type "B[D]", variable has type "B[C]")
    bd = bc # E: Incompatible types in assignment (expression has type "B[C]", variable has type "B[D]")
if int():
    bc = a  # E: Incompatible types in assignment (expression has type "A", variable has type "B[C]")
    bd = a  # E: Incompatible types in assignment (expression has type "A", variable has type "B[D]")

if int():
    a = bc
if int():
    a = bd

class A: pass
class B(A, Generic[T]): pass
class C: pass
class D: pass

[case testSubtypingAndInheritingGenericTypeFromGenericType]
from typing import TypeVar, Generic
T = TypeVar('T')
S = TypeVar('S')
ac: A[C]
ad: A[D]
bcc: B[C, C]
bdc: B[D, C]

if int():
    ad = bcc # E: Incompatible types in assignment (expression has type "B[C, C]", variable has type "A[D]")
if int():
    ad = bdc # E: Incompatible types in assignment (expression has type "B[D, C]", variable has type "A[D]")
    bcc = ac # E: Incompatible types in assignment (expression has type "A[C]", variable has type "B[C, C]")
    bdc = ac # E: Incompatible types in assignment (expression has type "A[C]", variable has type "B[D, C]")

if int():
    bcc = bcc
    bdc = bdc
    ac = bcc
if int():
    ac = bdc

class A(Generic[T]): pass
class B(A[S], Generic[T, S]): pass
class C: pass
class D: pass

[case testSubtypingAndInheritingGenericTypeFromGenericTypeAcrossHierarchy]
from typing import TypeVar, Generic
T = TypeVar('T')
S = TypeVar('S')
X = TypeVar('X')
Y = TypeVar('Y')
ae: A[A[E]]
af: A[A[F]]

cef: C[E, F]
cff: C[F, F]
cfe: C[F, E]

if int():
    ae = cef # E: Incompatible types in assignment (expression has type "C[E, F]", variable has type "A[A[E]]")
    af = cfe # E: Incompatible types in assignment (expression has type "C[F, E]", variable has type "A[A[F]]")

if int():
    ae = cfe
    af = cef
if int():
    af = cff

class A(Generic[T]): pass
class B(A[S], Generic[T, S]): pass
class C(B[A[X], A[Y]], Generic[X, Y]): pass
class E: pass
class F: pass

[case testIncludingBaseClassTwice]
from typing import TypeVar, Generic
t = TypeVar('t')
class I(Generic[t]): pass
class A(I[C], I[object]): pass # E: Duplicate base class "I"
class C: pass


-- Accessing inherited generic members
-- -----------------------------------


[case testAccessingMethodInheritedFromGenericType]
from typing import TypeVar, Generic
T = TypeVar('T')
S = TypeVar('S')
b: B[C, D]
c: C
d: D

b.f(c) # E: Argument 1 to "f" of "A" has incompatible type "C"; expected "D"
b.f(d)

class A(Generic[T]):
    def f(self, a: T) -> None:
        pass
class B(A[S], Generic[T, S]): pass
class C: pass
class D: pass
[builtins fixtures/tuple.pyi]

[case testAccessingMethodInheritedFromGenericTypeInNonGenericType]
from typing import TypeVar, Generic
T = TypeVar('T')
b: B
c: C
d: D

b.f(c) # E: Argument 1 to "f" of "A" has incompatible type "C"; expected "D"
b.f(d)

class C: pass
class D: pass
class A(Generic[T]):
    def f(self, a: T) -> None:
        pass
class B(A[D]): pass
[builtins fixtures/tuple.pyi]

[case testAccessingMemberVarInheritedFromGenericType]
from typing import TypeVar, Generic
T = TypeVar('T')
S = TypeVar('S')
class A(Generic[T]):
    def __init__(self, a: T) -> None:
        self.a = a

b: B[C, D]
c: C
d: D

b.a = c # E: Incompatible types in assignment (expression has type "C", variable has type "D")
b.a = d

class B(A[S], Generic[T, S]): pass
class C: pass
class D: pass
[builtins fixtures/tuple.pyi]


-- Overriding with generic types
-- -----------------------------


[case testOverridingMethodInSimpleTypeInheritingGenericType]
from typing import TypeVar, Generic
T = TypeVar('T')
class B(Generic[T]):
    def f(self, a: T) -> None: pass
    def g(self, a: T) -> None: pass
class C: pass
class D: pass
class A(B[C]):
    def f(self, a: D) -> None: pass \
        # E: Argument 1 of "f" is incompatible with supertype "B"; supertype defines the argument type as "C" \
        # N: This violates the Liskov substitution principle \
        # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
    def g(self, a: C) -> None: pass
[out]

[case testOverridingMethodInGenericTypeInheritingSimpleType]
from typing import TypeVar, Generic
T = TypeVar('T')
class C: pass
class B:
    def f(self, a: C) -> None: pass
    def g(self, a: C) -> None: pass
class A(B, Generic[T]):
    def f(self, a: T) -> None: pass \
        # E: Argument 1 of "f" is incompatible with supertype "B"; supertype defines the argument type as "C" \
        # N: This violates the Liskov substitution principle \
        # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
    def g(self, a: 'C') -> None: pass
[out]

[case testOverridingMethodInGenericTypeInheritingGenericType]
from typing import TypeVar, Generic
T = TypeVar('T')
S = TypeVar('S')
class B(Generic[T]):
    def f(self, a: T) -> None: pass
    def g(self, a: T) -> None: pass
class A(B[S], Generic[T, S]):
    def f(self, a: T) -> None: pass \
        # E: Argument 1 of "f" is incompatible with supertype "B"; supertype defines the argument type as "S" \
        # N: This violates the Liskov substitution principle \
        # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
    def g(self, a: S) -> None: pass
[out]

[case testOverridingMethodInMultilevelHierarchyOfGenericTypes]
from typing import TypeVar, Generic
T = TypeVar('T')
S = TypeVar('S')
U = TypeVar('U')
V = TypeVar('V')

class D: pass
class C(Generic[T, U, V]):
    def f(self, a: V) -> None: pass
    def g(self, a: V) -> None: pass
class B(C[D, D, T], Generic[T]): pass
class A(B[S], Generic[T, S]):
    def f(self, a: T) -> None: pass \
        # E: Argument 1 of "f" is incompatible with supertype "C"; supertype defines the argument type as "S" \
        # N: This violates the Liskov substitution principle \
        # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
    def g(self, a: S) -> None: pass
[out]

[case testOverrideGenericMethodInNonGenericClass]
from typing import TypeVar

T = TypeVar('T')
S = TypeVar('S')

class A:
    def f(self, x: T, y: S) -> None: pass
class B(A):
    def f(self, x: S, y: T) -> None: pass
class C(A):
    # Okay, because T = object allows any type for the arguments.
    def f(self, x: T, y: T) -> None: pass

[case testOverrideGenericMethodInNonGenericClassLists]
from typing import TypeVar, List

T = TypeVar('T')
S = TypeVar('S')

class A:
    def f(self, x: List[T], y: List[S]) -> None: pass
class B(A):
    def f(self, x: List[S], y: List[T]) -> None: pass
class C(A):
    def f(self, x: List[T], y: List[T]) -> None: pass  # Fail
[builtins fixtures/list.pyi]
[out]
main:11: error: Signature of "f" incompatible with supertype "A"
main:11: note:      Superclass:
main:11: note:          def [T, S] f(self, x: list[T], y: list[S]) -> None
main:11: note:      Subclass:
main:11: note:          def [T] f(self, x: list[T], y: list[T]) -> None

[case testOverrideGenericMethodInNonGenericClassGeneralize]
from typing import TypeVar

T = TypeVar('T')
T1 = TypeVar('T1', bound=str)
S = TypeVar('S')

class A:
    def f(self, x: int, y: S) -> None: pass
class B(A):
    def f(self, x: T, y: S) -> None: pass
class C(A):
    def f(self, x: T, y: str) -> None: pass
class D(A):
    def f(self, x: T1, y: S) -> None: pass # TODO: This error could be more specific.
[out]
main:12: error: Argument 2 of "f" is incompatible with supertype "A"; supertype defines the argument type as "S"
main:12: note: This violates the Liskov substitution principle
main:12: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
main:14: error: Signature of "f" incompatible with supertype "A"
main:14: note:      Superclass:
main:14: note:          def [S] f(self, x: int, y: S) -> None
main:14: note:      Subclass:
main:14: note:          def [T1: str, S] f(self, x: T1, y: S) -> None

-- Inheritance from generic types with implicit dynamic supertype
-- --------------------------------------------------------------


[case testInheritanceFromGenericWithImplicitDynamicAndSubtyping]
from typing import TypeVar, Generic
T = TypeVar('T')
a: A
bc: B[C]
bd: B[D]

if int():
    a = bc # E: Incompatible types in assignment (expression has type "B[C]", variable has type "A")
    bc = a
    bd = a

class B(Generic[T]): pass
class A(B): pass
class C: pass
class D: pass
[out]

[case testInheritanceFromGenericWithImplicitDynamicAndExternalAccess]
from typing import TypeVar, Generic
T = TypeVar('T')

class B(Generic[T]):
    def f(self, a: 'B[T]') -> None: pass
    def __init__(self, x: 'B[T]') -> None:
        self.x = x
class A(B): pass
class C: pass

a: A
c: C
bc: B[C]

a.x = c # E: Incompatible types in assignment (expression has type "C", variable has type "B[Any]")
a.f(c)  # E: Argument 1 to "f" of "B" has incompatible type "C"; expected "B[Any]"
a.x = bc
a.f(bc)
[out]

[case testInheritanceFromGenericWithImplicitDynamic]
from typing import TypeVar, Generic
T = TypeVar('T')
a: A
c: C
bc: B[C]

class B(Generic[T]):
  def f(self, a: 'B[T]') -> None: pass
  def __init__(self, x: 'B[T]') -> None:
    self.x = x

class A(B):
  def g(self) -> None:
    self.x = c # E: Incompatible types in assignment (expression has type "C", variable has type "B[Any]")
    self.f(c)  # E: Argument 1 to "f" of "B" has incompatible type "C"; expected "B[Any]"
    self.x = bc
    self.f(bc)

class C: pass
[out]

[case testInheritanceFromGenericWithImplicitDynamicAndOverriding]
from typing import TypeVar, Generic, Tuple
T = TypeVar('T')
class B(Generic[T]):
    def f(self, a: T, b: 'Tuple[T, B[T]]') -> None:
        pass
class A(B):
    def f(self, a, b): pass
[builtins fixtures/tuple.pyi]
[out]


-- Inheritance from generic types and super expressions
-- ----------------------------------------------------


[case testSuperExpressionsWhenInheritingFromGenericType]
from typing import TypeVar, Generic
T = TypeVar('T')
S = TypeVar('S')
class B(Generic[T]):
    def f(self, a: T) -> None: pass
class A(B[S], Generic[T, S]):
    def g(self, t: T, s: S) -> None:
        super().f(t)   # E: Argument 1 to "f" of "B" has incompatible type "T"; expected "S"
        super().f(s)
[out]

[case testSuperExpressionsWhenInheritingFromGenericTypeAndDeepHierarchy]
from typing import TypeVar, Generic
T = TypeVar('T')
S = TypeVar('S')
U = TypeVar('U')
V = TypeVar('V')
class C(Generic[T, U, V]):
    def f(self, a: V) -> None: pass
class D: pass
class B(C[D, D, T], Generic[T]): pass
class A(B[S], Generic[T, S]):
    def g(self, t: T, s: S) -> None:
        super().f(t)   # E: Argument 1 to "f" of "C" has incompatible type "T"; expected "S"
        super().f(s)
[out]


-- Type of inherited constructor
-- -----------------------------


[case testInheritedConstructor]
from typing import TypeVar, Generic
T = TypeVar('T')
class A(Generic[T]):
    def __init__(self, x: T) -> None: pass
class B(A[T], Generic[T]): pass
class C(A[int]): pass
class D(A[A[T]], Generic[T]): pass
B(1)
C(1)
C('a')  # E: Argument 1 to "C" has incompatible type "str"; expected "int"
D(A(1))
D(1)  # E: Argument 1 to "D" has incompatible type "int"; expected "A[Never]"


[case testInheritedConstructor2]
from typing import TypeVar, Generic
T = TypeVar('T')
U = TypeVar('U')
Z = TypeVar('Z')
class A(Generic[T, U]):
    def __init__(self, x: T, y: U, z: Z) -> None: pass
class B(A[int, T], Generic[T]): pass
class C(B[A[T, str]], Generic[T, U]): pass
# C[T, U] <: B[A[T, str]] <: A[int, A[T, str]]
C(1, A(1, 'a', 0), 'z')
C(1, A('1', 'a', 0), 'z')
C('1', A(1, 'a', 0), 'z')  # E: Argument 1 to "C" has incompatible type "str"; expected "int"
C(1, A(1, 1, 0), 'z')  # E: Argument 2 to "A" has incompatible type "int"; expected "str"


-- Subtyping with a generic abstract base class
-- --------------------------------------------


[case testSubtypingWithGenericTypeSubclassingGenericAbstractClass]
from typing import TypeVar, Generic
from abc import abstractmethod
T = TypeVar('T')
S = TypeVar('S')
acd: A[C, D]
adc: A[D, C]
ic: I[C]
id: I[D]

if int():
    ic = acd # E: Incompatible types in assignment (expression has type "A[C, D]", variable has type "I[C]")
    id = adc # E: Incompatible types in assignment (expression has type "A[D, C]", variable has type "I[D]")
    adc = ic # E: Incompatible types in assignment (expression has type "I[C]", variable has type "A[D, C]")

if int():
    ic = adc
    id = acd

class I(Generic[T]):
    @abstractmethod
    def f(self): pass
class A(I[S], Generic[T, S]): pass
class C: pass
class D: pass

[case testSubtypingWithTypeImplementingGenericABCViaInheritance]
from typing import TypeVar, Generic
S = TypeVar('S')
a: A
b: B
ic: I[C]
id: I[D]
ie: I[E]

class I(Generic[S]): pass
class B(I[C]): pass
class A(B): pass

if int():
    ie = a # E: Incompatible types in assignment (expression has type "A", variable has type "I[E]")
    a = ic # E: Incompatible types in assignment (expression has type "I[C]", variable has type "A")
if int():
    a = id # E: Incompatible types in assignment (expression has type "I[D]", variable has type "A")
if int():
    a = b  # E: Incompatible types in assignment (expression has type "B", variable has type "A")
    id = a # E: Incompatible types in assignment (expression has type "A", variable has type "I[D]")

    ic = a
    b = a

class C: pass
class D: pass
class E: pass
[builtins fixtures/tuple.pyi]
[out]

[case testSubtypingWithTypeImplementingGenericABCViaInheritance2-skip]
from typing import TypeVar, Generic
T = TypeVar('T')
class I(Generic[T]): pass
class A(I[C]): pass
class B(A, I[D]): pass # Fail

class C: pass
class D: pass
[out]
main:5: error: Class "B" has base "I" duplicated inconsistently

[case testSubtypingAndABCExtension]
from typing import TypeVar, Generic
from abc import abstractmethod, ABCMeta
t = TypeVar('t')
a: A[object]
i: I[object]
j: J[object]
(ii, jj) = (i, j)
if int():
    ii = a
    jj = a
if int():
    jj = i
    a = i # E: Incompatible types in assignment (expression has type "I[object]", variable has type "A[object]")
if int():
    a = j # E: Incompatible types in assignment (expression has type "J[object]", variable has type "A[object]")

class J(Generic[t]): pass
class X(metaclass=ABCMeta): pass
class I(X, J[t], Generic[t]): pass
class A(I[t], Generic[t]): pass
[builtins fixtures/tuple.pyi]


-- Subclassing a generic ABC
-- -------------------------


[case testSubclassingGenericABC1]
from typing import TypeVar, Generic
from abc import abstractmethod
T = TypeVar('T')
class I(Generic[T]):
    @abstractmethod
    def f(self, a: T) -> None: pass
    @abstractmethod
    def g(self, a: T) -> None: pass
class A(I[C]):
    def f(self, a: 'D') -> None: pass \
        # E: Argument 1 of "f" is incompatible with supertype "I"; supertype defines the argument type as "C" \
        # N: This violates the Liskov substitution principle \
        # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
    def g(self, a: 'C') -> None: pass
class C: pass
class D: pass
[out]


-- Extending a generic ABC with deep type hierarchy
-- ------------------------------------------------


[case testSubclassingGenericABCWithDeepHierarchy]
from typing import Any, TypeVar, Generic
from abc import abstractmethod
T = TypeVar('T')
a: A
ic: I[C]
id: I[D]

if int():
    id = a # E: Incompatible types in assignment (expression has type "A", variable has type "I[D]")
    ic = a

class I(Generic[T]):
    @abstractmethod
    def f(self, a: T, b: T) -> None: pass
    @abstractmethod
    def g(self, a: T, b: 'D') -> None: pass
class B(I[C]):
    def f(self, a: 'C', b: 'C') -> None: pass
    def g(self, a: 'C', b: Any) -> None: pass
class A(B):
    def g(self, a: 'C', b: 'C') -> None: pass \
        # E: Argument 2 of "g" is incompatible with supertype "I"; supertype defines the argument type as "D" \
        # N: This violates the Liskov substitution principle \
        # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
    def f(self, a: 'C', b: 'C') -> None: pass
class C: pass
class D: pass
[builtins fixtures/tuple.pyi]

[case testSubclassingGenericABCWithDeepHierarchy2]
from typing import Any, TypeVar, Generic
from abc import abstractmethod
T = TypeVar('T')
class I(Generic[T]):
    @abstractmethod
    def f(self, a: T, b: T) -> None: pass
class B(I[C]):
    def f(self, a: 'C', b: Any) -> None: pass
class A(B):
    def f(self, a: 'C', b: 'D') -> None: pass \
        # E: Argument 2 of "f" is incompatible with supertype "I"; supertype defines the argument type as "C" \
        # N: This violates the Liskov substitution principle \
        # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
class C: pass
class D: pass
[out]


-- Implicit Any types and subclassing generic ABC
-- ----------------------------------------------


[case testSubclassingGenericABCWithImplicitAny]
from typing import Any, TypeVar, Generic
from abc import abstractmethod
T = TypeVar('T')
a: Any
ic: I[C]
id: I[D]

ic = a
id = a

class I(Generic[T]):
    @abstractmethod
    def f(self, a: T) -> None: pass
class A(I):
    def f(self, a): pass

class C: pass
class D: pass

[case testSubclassingGenericABCWithImplicitAnyAndDeepHierarchy]
from typing import Any, TypeVar, Generic
from abc import abstractmethod
T = TypeVar('T')
a: Any
ic: I[C]
id: I[D]

ic = a
id = a

class I(Generic[T]):
    @abstractmethod
    def f(self, a: T, b: T) -> None: pass
class B(I):
    def f(self, a, b): pass
class A(B):
    def f(self, a: 'C', b: 'D') -> None: pass
class C: pass
class D: pass

[case testImplementingGenericABCWithImplicitAnyAndDeepHierarchy2]
from typing import Any, TypeVar, Generic
from abc import abstractmethod
T = TypeVar('T')
a: Any
jc: J[C]
jd: J[D]

jc = a
jd = a

class J(Generic[T]):
    @abstractmethod
    def f(self, a: T, b: T) -> None: pass
class I(J):
    @abstractmethod
    def f(self, a, b): pass
class A(I):
    def f(self, a: 'C', b: 'D') -> None: pass

class C: pass
class D: pass


-- Accessing generic ABC members
-- -----------------------------


[case testAccessingGenericABCMembers]
from typing import TypeVar, Generic
from abc import abstractmethod
T = TypeVar('T')
class I(Generic[T]):
    @abstractmethod
    def f(self, a: T) -> None: pass
class A: pass
class B: pass

a: A
b: B
ia: I[A]

ia.f(b)  # E: Argument 1 to "f" of "I" has incompatible type "B"; expected "A"
ia.f(a)
[builtins fixtures/tuple.pyi]

[case testAccessingInheritedGenericABCMembers]
from typing import TypeVar, Generic
from abc import abstractmethod
T = TypeVar('T')
class J(Generic[T]):
    @abstractmethod
    def f(self, a: T) -> None: pass
class I(J[T], Generic[T]): pass
class A: pass
class B: pass
a: A
b: B
ia: I[A]

ia.f(b)  # E: Argument 1 to "f" of "J" has incompatible type "B"; expected "A"
ia.f(a)
[builtins fixtures/tuple.pyi]


-- Misc
-- ----


[case testMultipleAssignmentAndGenericSubtyping]
from typing import Iterable
n: int
s: str
class Nums(Iterable[int]):
    def __iter__(self): pass
    def __next__(self): pass
n, n = Nums()
s, s = Nums() # E: Incompatible types in assignment (expression has type "int", variable has type "str")
[builtins fixtures/for.pyi]
[out]

[case testUninhabitedCacheChecksAmbiguous]
# https://github.com/python/mypy/issues/19641
from typing import Mapping, Never, TypeVar

M = TypeVar("M", bound=Mapping[str,object])

def get(arg: M, /) -> M:
    return arg

get({})

def upcast(d: dict[Never, Never]) -> Mapping[str, object]:
    return d  # E: Incompatible return value type (got "dict[Never, Never]", expected "Mapping[str, object]")
[builtins fixtures/dict.pyi]

-- Variance
-- --------


[case testCovariant]
from typing import TypeVar, Generic
T = TypeVar('T', covariant=True)

class G(Generic[T]): pass
class A: pass
class B(A): pass
class C(B): pass

a: G[A]
b: G[B]
c: G[C]

if int():
    b = a  # E: Incompatible types in assignment (expression has type "G[A]", variable has type "G[B]")
    b = c
[builtins fixtures/bool.pyi]
[out]

[case testContravariant]
from typing import TypeVar, Generic
T = TypeVar('T', contravariant=True)

class G(Generic[T]): pass
class A: pass
class B(A): pass
class C(B): pass

a: G[A]
b: G[B]
c: G[C]

if int():
    b = a
    b = c  # E: Incompatible types in assignment (expression has type "G[C]", variable has type "G[B]")
[builtins fixtures/bool.pyi]
[out]

[case testInvariant]
from typing import TypeVar, Generic
T = TypeVar('T')  # invariant (default)

class G(Generic[T]): pass
class A: pass
class B(A): pass
class C(B): pass

a: G[A]
b: G[B]
c: G[C]

if int():
    b = a  # E: Incompatible types in assignment (expression has type "G[A]", variable has type "G[B]")
    b = c  # E: Incompatible types in assignment (expression has type "G[C]", variable has type "G[B]")
[builtins fixtures/bool.pyi]
[out]


[case testTypeVarSubtypeUnion]
from typing import Union, TypeVar, Generic

class U: pass
class W: pass

T = TypeVar('T', bound=Union[U, W])

class Y(Generic[T]):
    def __init__(self) -> None:
        pass
    def f(self) -> T:
        return U()  # E: Incompatible return value type (got "U", expected "T")


[case testTypeVarBoundToOldUnionAttributeAccess]
from typing import Union, TypeVar

class U:
    a: float
class V:
    b: float
class W:
    c: float

T = TypeVar("T", bound=Union[U, V, W])

def f(x: T) -> None:
    x.a  # E
    x.b = 1.0  # E
    del x.c  # E

[out]
main:13: error: Item "V" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "a"
main:13: error: Item "W" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "a"
main:14: error: Item "U" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "b"
main:14: error: Item "W" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "b"
main:15: error: Item "U" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "c"
main:15: error: Item "V" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "c"


[case testTypeVarBoundToNewUnionAttributeAccess]
# flags: --python-version 3.10
from typing import TypeVar

class U:
    a: int
class V:
    b: int
class W:
    c: int

T = TypeVar("T", bound=U | V | W)

def f(x: T) -> None:
    x.a  # E
    x.b = 1  # E
    del x.c  # E

[builtins fixtures/tuple.pyi]
[out]
main:14: error: Item "V" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "a"
main:14: error: Item "W" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "a"
main:15: error: Item "U" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "b"
main:15: error: Item "W" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "b"
main:16: error: Item "U" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "c"
main:16: error: Item "V" of the upper bound "Union[U, V, W]" of type variable "T" has no attribute "c"


[case testSubtypingIterableUnpacking1]
# https://github.com/python/mypy/issues/11138
from typing import Generic, Iterator, TypeVar
T = TypeVar("T")
U = TypeVar("U")

class X1(Iterator[U], Generic[T, U]):
    pass

x1: X1[str, int]
reveal_type(list(x1))  # N: Revealed type is "builtins.list[builtins.int]"
reveal_type([*x1])  # N: Revealed type is "builtins.list[builtins.int]"

class X2(Iterator[T], Generic[T, U]):
    pass

x2: X2[str, int]
reveal_type(list(x2))  # N: Revealed type is "builtins.list[builtins.str]"
reveal_type([*x2])  # N: Revealed type is "builtins.list[builtins.str]"

class X3(Generic[T, U], Iterator[U]):
    pass

x3: X3[str, int]
reveal_type(list(x3))  # N: Revealed type is "builtins.list[builtins.int]"
reveal_type([*x3])  # N: Revealed type is "builtins.list[builtins.int]"

class X4(Generic[T, U], Iterator[T]):
    pass

x4: X4[str, int]
reveal_type(list(x4))  # N: Revealed type is "builtins.list[builtins.str]"
reveal_type([*x4])  # N: Revealed type is "builtins.list[builtins.str]"

class X5(Iterator[T]):
    pass

x5: X5[str]
reveal_type(list(x5))  # N: Revealed type is "builtins.list[builtins.str]"
reveal_type([*x5])  # N: Revealed type is "builtins.list[builtins.str]"

class X6(Generic[T, U], Iterator[bool]):
    pass

x6: X6[str, int]
reveal_type(list(x6))  # N: Revealed type is "builtins.list[builtins.bool]"
reveal_type([*x6])  # N: Revealed type is "builtins.list[builtins.bool]"
[builtins fixtures/list.pyi]

[case testSubtypingIterableUnpacking2]
from typing import Generic, Iterator, TypeVar, Mapping
T = TypeVar("T")
U = TypeVar("U")

class X1(Generic[T, U], Iterator[U], Mapping[U, T]):
    pass

x1: X1[str, int]
reveal_type(list(x1))  # N: Revealed type is "builtins.list[builtins.int]"
reveal_type([*x1])  # N: Revealed type is "builtins.list[builtins.int]"

class X2(Generic[T, U], Iterator[U], Mapping[T, U]):
    pass

x2: X2[str, int]
reveal_type(list(x2))  # N: Revealed type is "builtins.list[builtins.int]"
reveal_type([*x2])  # N: Revealed type is "builtins.list[builtins.int]"
[builtins fixtures/list.pyi]

[case testSubtypingMappingUnpacking1]
# https://github.com/python/mypy/issues/11138
from typing import Generic, TypeVar, Mapping
T = TypeVar("T")
U = TypeVar("U")

class X1(Generic[T, U],  Mapping[U, T]):
    pass

x1: X1[str, int]
reveal_type(iter(x1))  # N: Revealed type is "typing.Iterator[builtins.int]"
reveal_type({**x1})  # N: Revealed type is "builtins.dict[builtins.int, builtins.str]"

class X2(Generic[T, U],  Mapping[T, U]):
    pass

x2: X2[str, int]
reveal_type(iter(x2))  # N: Revealed type is "typing.Iterator[builtins.str]"
reveal_type({**x2})  # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"

class X3(Generic[T, U],  Mapping[bool, float]):
    pass

x3: X3[str, int]
reveal_type(iter(x3))  # N: Revealed type is "typing.Iterator[builtins.bool]"
reveal_type({**x3})  # N: Revealed type is "builtins.dict[builtins.bool, builtins.float]"
[builtins fixtures/dict.pyi]

[case testSubtypingMappingUnpacking2]
from typing import Generic, TypeVar, Mapping
T = TypeVar("T")
U = TypeVar("U")

class X1(Generic[T, U],  Mapping[U, T]):
    pass

def func_with_kwargs(**kwargs: int):
    pass

x1: X1[str, int]
reveal_type(iter(x1))
reveal_type({**x1})
func_with_kwargs(**x1)
[out]
main:12: note: Revealed type is "typing.Iterator[builtins.int]"
main:13: note: Revealed type is "builtins.dict[builtins.int, builtins.str]"
main:14: error: Keywords must be strings
main:14: error: Argument 1 to "func_with_kwargs" has incompatible type "**X1[str, int]"; expected "int"
[builtins fixtures/dict.pyi]
[typing fixtures/typing-medium.pyi]

[case testSubtypingMappingUnpacking3]
from typing import Generic, TypeVar, Mapping, Iterable
T = TypeVar("T")
U = TypeVar("U")

class X1(Generic[T, U],  Mapping[U, T], Iterable[U]):
    pass

x1: X1[str, int]
reveal_type(iter(x1))  # N: Revealed type is "typing.Iterator[builtins.int]"
reveal_type({**x1})  # N: Revealed type is "builtins.dict[builtins.int, builtins.str]"

# Some people would expect this to raise an error, but this currently does not:
# `Mapping` has `Iterable[U]` base class, `X2` has direct `Iterable[T]` base class.
# It would be impossible to define correct `__iter__` method for incompatible `T` and `U`.
class X2(Generic[T, U],  Mapping[U, T], Iterable[T]):
    pass

x2: X2[str, int]
reveal_type(iter(x2))  # N: Revealed type is "typing.Iterator[builtins.int]"
reveal_type({**x2})  # N: Revealed type is "builtins.dict[builtins.int, builtins.str]"
[builtins fixtures/dict.pyi]

[case testNotDirectIterableAndMappingSubtyping]
from typing import Generic, TypeVar, Dict, Iterable, Iterator, List
T = TypeVar("T")
U = TypeVar("U")

class X1(Generic[T, U], Dict[U, T], Iterable[U]):
    def __iter__(self) -> Iterator[U]: pass

x1: X1[str, int]
reveal_type(iter(x1))  # N: Revealed type is "typing.Iterator[builtins.int]"
reveal_type({**x1})  # N: Revealed type is "builtins.dict[builtins.int, builtins.str]"

class X2(Generic[T, U], List[U]):
    def __iter__(self) -> Iterator[U]: pass

x2: X2[str, int]
reveal_type(iter(x2))  # N: Revealed type is "typing.Iterator[builtins.int]"
reveal_type([*x2])  # N: Revealed type is "builtins.list[builtins.int]"
[builtins fixtures/dict.pyi]

[case testIncompatibleVariance]
from typing import TypeVar, Generic
T = TypeVar('T')
T_co = TypeVar('T_co', covariant=True)
T_contra = TypeVar('T_contra', contravariant=True)

class A(Generic[T_co]): ...
class B(A[T_contra], Generic[T_contra]): ...  # E: Variance of TypeVar "T_contra" incompatible with variance in parent type

class C(Generic[T_contra]): ...
class D(C[T_co], Generic[T_co]): ...  # E: Variance of TypeVar "T_co" incompatible with variance in parent type

class E(Generic[T]): ...
class F(E[T_co], Generic[T_co]): ...  # E: Variance of TypeVar "T_co" incompatible with variance in parent type

class G(Generic[T]): ...
class H(G[T_contra], Generic[T_contra]): ...  # E: Variance of TypeVar "T_contra" incompatible with variance in parent type

[case testParameterizedGenericOverrideWithProperty]
from typing import TypeVar, Generic

T = TypeVar("T")

class A(Generic[T]):
    def __init__(self, val: T):
        self.member: T = val

class B(A[str]):
    member: str

class GoodPropertyOverride(A[str]):
    @property
    def member(self) -> str: ...
    @member.setter
    def member(self, val: str): ...

class BadPropertyOverride(A[str]):
    @property  # E: Signature of "member" incompatible with supertype "A" \
               # N:      Superclass: \
               # N:          str \
               # N:      Subclass: \
               # N:          int
    def member(self) -> int: ...
    @member.setter
    def member(self, val: int): ...

class BadGenericPropertyOverride(A[str], Generic[T]):
    @property  # E: Signature of "member" incompatible with supertype "A" \
               # N:      Superclass: \
               # N:          str \
               # N:      Subclass: \
               # N:          T
    def member(self) -> T: ...
    @member.setter
    def member(self, val: T): ...
[builtins fixtures/property.pyi]

[case testParameterizedGenericPropertyOverrideWithProperty]
from typing import TypeVar, Generic

T = TypeVar("T")

class A(Generic[T]):
    @property
    def member(self) -> T: ...
    @member.setter
    def member(self, val: T): ...

class B(A[str]):
    member: str

class GoodPropertyOverride(A[str]):
    @property
    def member(self) -> str: ...
    @member.setter
    def member(self, val: str): ...

class BadPropertyOverride(A[str]):
    @property  # E: Signature of "member" incompatible with supertype "A" \
               # N:      Superclass: \
               # N:          str \
               # N:      Subclass: \
               # N:          int
    def member(self) -> int: ...
    @member.setter
    def member(self, val: int): ...

class BadGenericPropertyOverride(A[str], Generic[T]):
    @property  # E: Signature of "member" incompatible with supertype "A" \
               # N:      Superclass: \
               # N:          str \
               # N:      Subclass: \
               # N:          T
    def member(self) -> T: ...
    @member.setter
    def member(self, val: T): ...
[builtins fixtures/property.pyi]

[case testParameterizedGenericOverrideSelfWithProperty]
from typing_extensions import Self

class A:
    def __init__(self, val: Self):
        self.member: Self = val

class GoodPropertyOverride(A):
    @property
    def member(self) -> "GoodPropertyOverride": ...
    @member.setter
    def member(self, val: "GoodPropertyOverride"): ...

class GoodPropertyOverrideSelf(A):
    @property
    def member(self) -> Self: ...
    @member.setter
    def member(self, val: Self): ...
[builtins fixtures/property.pyi]

[case testParameterizedGenericOverrideWithSelfProperty]
from typing import TypeVar, Generic
from typing_extensions import Self

T = TypeVar("T")

class A(Generic[T]):
    def __init__(self, val: T):
        self.member: T = val

class B(A["B"]):
    member: Self

class GoodPropertyOverride(A["GoodPropertyOverride"]):
    @property
    def member(self) -> Self: ...
    @member.setter
    def member(self, val: Self): ...
[builtins fixtures/property.pyi]

[case testMultipleInheritanceCompatibleTypeVar]
from typing import Generic, TypeVar

T = TypeVar("T")
U = TypeVar("U")

class A(Generic[T]):
    x: T
    def fn(self, t: T) -> None: ...

class A2(A[T]):
    y: str
    z: str

class B(Generic[T]):
    x: T
    def fn(self, t: T) -> None: ...

class C1(A2[str], B[str]): pass
class C2(A2[str], B[int]): pass  # E: Definition of "fn" in base class "A" is incompatible with definition in base class "B" \
                                 # E: Definition of "x" in base class "A" is incompatible with definition in base class "B"
class C3(A2[T], B[T]): pass
class C4(A2[U], B[U]): pass
class C5(A2[U], B[T]): pass  # E: Definition of "fn" in base class "A" is incompatible with definition in base class "B" \
                             # E: Definition of "x" in base class "A" is incompatible with definition in base class "B"

class D1(A[str], B[str]): pass
class D2(A[str], B[int]): pass  # E: Definition of "fn" in base class "A" is incompatible with definition in base class "B" \
                                # E: Definition of "x" in base class "A" is incompatible with definition in base class "B"
class D3(A[T], B[T]): pass
class D4(A[U], B[U]): pass
class D5(A[U], B[T]): pass  # E: Definition of "fn" in base class "A" is incompatible with definition in base class "B" \
                            # E: Definition of "x" in base class "A" is incompatible with definition in base class "B"
[builtins fixtures/tuple.pyi]