-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Copy pathcheck-python310.test
2265 lines (1809 loc) · 55.4 KB
/
check-python310.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
-- Capture Pattern --
[case testMatchCapturePatternType]
class A: ...
m: A
match m:
case a:
reveal_type(a) # N: Revealed type is "__main__.A"
-- Literal Pattern --
[case testMatchLiteralPatternNarrows]
m: object
match m:
case 1:
reveal_type(m) # N: Revealed type is "Literal[1]"
[case testMatchLiteralPatternAlreadyNarrower-skip]
m: bool
match m:
case 1:
reveal_type(m) # This should probably be unreachable, but isn't detected as such.
[builtins fixtures/primitives.pyi]
[case testMatchLiteralPatternUnreachable]
# primitives are needed because otherwise mypy doesn't see that int and str are incompatible
m: int
match m:
case "str":
reveal_type(m)
[builtins fixtures/primitives.pyi]
-- Value Pattern --
[case testMatchValuePatternNarrows]
import b
m: object
match m:
case b.b:
reveal_type(m) # N: Revealed type is "builtins.int"
[file b.py]
b: int
[case testMatchValuePatternAlreadyNarrower]
import b
m: bool
match m:
case b.b:
reveal_type(m) # N: Revealed type is "builtins.bool"
[file b.py]
b: int
[case testMatchValuePatternIntersect]
import b
class A: ...
m: A
match m:
case b.b:
reveal_type(m) # N: Revealed type is "__main__.<subclass of "A" and "B">1"
[file b.py]
class B: ...
b: B
[case testMatchValuePatternUnreachable]
# primitives are needed because otherwise mypy doesn't see that int and str are incompatible
import b
m: int
match m:
case b.b:
reveal_type(m)
[file b.py]
b: str
[builtins fixtures/primitives.pyi]
-- Sequence Pattern --
[case testMatchSequencePatternCaptures]
from typing import List
m: List[int]
match m:
case [a]:
reveal_type(a) # N: Revealed type is "builtins.int"
[builtins fixtures/list.pyi]
[case testMatchSequencePatternCapturesStarred]
from typing import Sequence
m: Sequence[int]
match m:
case [a, *b]:
reveal_type(a) # N: Revealed type is "builtins.int"
reveal_type(b) # N: Revealed type is "builtins.list[builtins.int]"
[builtins fixtures/list.pyi]
[case testMatchSequencePatternNarrowsInner]
from typing import Sequence
m: Sequence[object]
match m:
case [1, True]:
reveal_type(m) # N: Revealed type is "typing.Sequence[builtins.int]"
[case testMatchSequencePatternNarrowsOuter]
from typing import Sequence
m: object
match m:
case [1, True]:
reveal_type(m) # N: Revealed type is "typing.Sequence[builtins.int]"
[case testMatchSequencePatternAlreadyNarrowerInner]
from typing import Sequence
m: Sequence[bool]
match m:
case [1, True]:
reveal_type(m) # N: Revealed type is "typing.Sequence[builtins.bool]"
[case testMatchSequencePatternAlreadyNarrowerOuter]
from typing import Sequence
m: Sequence[object]
match m:
case [1, True]:
reveal_type(m) # N: Revealed type is "typing.Sequence[builtins.int]"
[case testMatchSequencePatternAlreadyNarrowerBoth]
from typing import Sequence
m: Sequence[bool]
match m:
case [1, True]:
reveal_type(m) # N: Revealed type is "typing.Sequence[builtins.bool]"
[case testMatchNestedSequencePatternNarrowsInner]
from typing import Sequence
m: Sequence[Sequence[object]]
match m:
case [[1], [True]]:
reveal_type(m) # N: Revealed type is "typing.Sequence[typing.Sequence[builtins.int]]"
[case testMatchNestedSequencePatternNarrowsOuter]
from typing import Sequence
m: object
match m:
case [[1], [True]]:
reveal_type(m) # N: Revealed type is "typing.Sequence[typing.Sequence[builtins.int]]"
[case testMatchSequencePatternDoesntNarrowInvariant]
from typing import List
m: List[object]
match m:
case [1]:
reveal_type(m) # N: Revealed type is "builtins.list[builtins.object]"
[builtins fixtures/list.pyi]
[case testMatchSequencePatternMatches]
import array, collections
from typing import Sequence, Iterable
m1: object
m2: Sequence[int]
m3: array.array[int]
m4: collections.deque[int]
m5: list[int]
m6: memoryview
m7: range
m8: tuple[int]
m9: str
m10: bytes
m11: bytearray
match m1:
case [a]:
reveal_type(a) # N: Revealed type is "builtins.object"
match m2:
case [b]:
reveal_type(b) # N: Revealed type is "builtins.int"
match m3:
case [c]:
reveal_type(c) # N: Revealed type is "builtins.int"
match m4:
case [d]:
reveal_type(d) # N: Revealed type is "builtins.int"
match m5:
case [e]:
reveal_type(e) # N: Revealed type is "builtins.int"
match m6:
case [f]:
reveal_type(f) # N: Revealed type is "builtins.int"
match m7:
case [g]:
reveal_type(g) # N: Revealed type is "builtins.int"
match m8:
case [h]:
reveal_type(h) # N: Revealed type is "builtins.int"
match m9:
case [i]:
reveal_type(i)
match m10:
case [j]:
reveal_type(j)
match m11:
case [k]:
reveal_type(k)
[builtins fixtures/primitives.pyi]
[typing fixtures/typing-full.pyi]
[case testMatchSequencePatternCapturesTuple]
from typing import Tuple
m: Tuple[int, str, bool]
match m:
case [a, b, c]:
reveal_type(a) # N: Revealed type is "builtins.int"
reveal_type(b) # N: Revealed type is "builtins.str"
reveal_type(c) # N: Revealed type is "builtins.bool"
reveal_type(m) # N: Revealed type is "Tuple[builtins.int, builtins.str, builtins.bool]"
[builtins fixtures/list.pyi]
[case testMatchSequencePatternTupleTooLong]
from typing import Tuple
m: Tuple[int, str]
match m:
case [a, b, c]:
reveal_type(a)
reveal_type(b)
reveal_type(c)
[builtins fixtures/list.pyi]
[case testMatchSequencePatternTupleTooShort]
from typing import Tuple
m: Tuple[int, str, bool]
match m:
case [a, b]:
reveal_type(a)
reveal_type(b)
[builtins fixtures/list.pyi]
[case testMatchSequencePatternTupleNarrows]
from typing import Tuple
m: Tuple[object, object]
match m:
case [1, "str"]:
reveal_type(m) # N: Revealed type is "Tuple[Literal[1], Literal['str']]"
[builtins fixtures/list.pyi]
[case testMatchSequencePatternTupleStarred]
from typing import Tuple
m: Tuple[int, str, bool]
match m:
case [a, *b, c]:
reveal_type(a) # N: Revealed type is "builtins.int"
reveal_type(b) # N: Revealed type is "builtins.list[builtins.str]"
reveal_type(c) # N: Revealed type is "builtins.bool"
reveal_type(m) # N: Revealed type is "Tuple[builtins.int, builtins.str, builtins.bool]"
[builtins fixtures/list.pyi]
[case testMatchSequencePatternTupleStarredUnion]
from typing import Tuple
m: Tuple[int, str, float, bool]
match m:
case [a, *b, c]:
reveal_type(a) # N: Revealed type is "builtins.int"
reveal_type(b) # N: Revealed type is "builtins.list[Union[builtins.str, builtins.float]]"
reveal_type(c) # N: Revealed type is "builtins.bool"
reveal_type(m) # N: Revealed type is "Tuple[builtins.int, builtins.str, builtins.float, builtins.bool]"
[builtins fixtures/list.pyi]
[case testMatchSequencePatternTupleStarredTooShort]
from typing import Tuple
m: Tuple[int]
reveal_type(m) # N: Revealed type is "Tuple[builtins.int]"
match m:
case [a, *b, c]:
reveal_type(a)
reveal_type(b)
reveal_type(c)
[builtins fixtures/list.pyi]
[case testMatchNonMatchingSequencePattern]
from typing import List
x: List[int]
match x:
case [str()]:
pass
[case testMatchSequencePatternWithInvalidClassPattern]
class Example:
__match_args__ = ("value",)
def __init__(self, value: str) -> None:
self.value = value
SubClass: type[Example]
match [SubClass("a"), SubClass("b")]:
case [SubClass(value), *rest]: # E: Expected type in class pattern; found "Type[__main__.Example]"
reveal_type(value) # E: Cannot determine type of "value" \
# N: Revealed type is "Any"
reveal_type(rest) # N: Revealed type is "builtins.list[__main__.Example]"
[builtins fixtures/tuple.pyi]
[case testMatchSequenceUnion-skip]
from typing import List, Union
m: Union[List[List[str]], str]
match m:
case [list(['str'])]:
reveal_type(m) # N: Revealed type is "builtins.list[builtins.list[builtins.str]]"
[builtins fixtures/list.pyi]
[case testMatchSequencePatternNarrowSubjectItems]
m: int
n: str
o: bool
match m, n, o:
case [3, "foo", True]:
reveal_type(m) # N: Revealed type is "Literal[3]"
reveal_type(n) # N: Revealed type is "Literal['foo']"
reveal_type(o) # N: Revealed type is "Literal[True]"
case [a, b, c]:
reveal_type(m) # N: Revealed type is "builtins.int"
reveal_type(n) # N: Revealed type is "builtins.str"
reveal_type(o) # N: Revealed type is "builtins.bool"
reveal_type(m) # N: Revealed type is "builtins.int"
reveal_type(n) # N: Revealed type is "builtins.str"
reveal_type(o) # N: Revealed type is "builtins.bool"
[builtins fixtures/tuple.pyi]
[case testMatchSequencePatternNarrowSubjectItemsRecursive]
m: int
n: int
o: int
p: int
q: int
r: int
match m, (n, o), (p, (q, r)):
case [0, [1, 2], [3, [4, 5]]]:
reveal_type(m) # N: Revealed type is "Literal[0]"
reveal_type(n) # N: Revealed type is "Literal[1]"
reveal_type(o) # N: Revealed type is "Literal[2]"
reveal_type(p) # N: Revealed type is "Literal[3]"
reveal_type(q) # N: Revealed type is "Literal[4]"
reveal_type(r) # N: Revealed type is "Literal[5]"
[builtins fixtures/tuple.pyi]
[case testMatchSequencePatternSequencesLengthMismatchNoNarrowing]
m: int
n: str
o: bool
match m, n, o:
case [3, "foo"]:
pass
case [3, "foo", True, True]:
pass
[builtins fixtures/tuple.pyi]
[case testMatchSequencePatternSequencesLengthMismatchNoNarrowingRecursive]
m: int
n: int
o: int
match m, (n, o):
case [0]:
pass
case [0, 1, [2]]:
pass
case [0, [1]]:
pass
case [0, [1, 2, 3]]:
pass
[builtins fixtures/tuple.pyi]
-- Mapping Pattern --
[case testMatchMappingPatternCaptures]
from typing import Dict
import b
m: Dict[str, int]
match m:
case {"key": v}:
reveal_type(v) # N: Revealed type is "builtins.int"
case {b.b: v2}:
reveal_type(v2) # N: Revealed type is "builtins.int"
[file b.py]
b: str
[builtins fixtures/dict.pyi]
[case testMatchMappingPatternCapturesWrongKeyType]
# This is not actually unreachable, as a subclass of dict could accept keys with different types
from typing import Dict
import b
m: Dict[str, int]
match m:
case {1: v}:
reveal_type(v) # N: Revealed type is "builtins.int"
case {b.b: v2}:
reveal_type(v2) # N: Revealed type is "builtins.int"
[file b.py]
b: int
[builtins fixtures/dict.pyi]
[case testMatchMappingPatternCapturesTypedDict]
from typing import TypedDict
class A(TypedDict):
a: str
b: int
m: A
match m:
case {"a": v}:
reveal_type(v) # N: Revealed type is "builtins.str"
case {"b": v2}:
reveal_type(v2) # N: Revealed type is "builtins.int"
case {"a": v3, "b": v4}:
reveal_type(v3) # N: Revealed type is "builtins.str"
reveal_type(v4) # N: Revealed type is "builtins.int"
case {"o": v5}:
reveal_type(v5) # N: Revealed type is "builtins.object"
[typing fixtures/typing-typeddict.pyi]
[case testMatchMappingPatternCapturesTypedDictWithLiteral]
from typing import TypedDict
import b
class A(TypedDict):
a: str
b: int
m: A
match m:
case {b.a: v}:
reveal_type(v) # N: Revealed type is "builtins.str"
case {b.b: v2}:
reveal_type(v2) # N: Revealed type is "builtins.int"
case {b.a: v3, b.b: v4}:
reveal_type(v3) # N: Revealed type is "builtins.str"
reveal_type(v4) # N: Revealed type is "builtins.int"
case {b.o: v5}:
reveal_type(v5) # N: Revealed type is "builtins.object"
[file b.py]
from typing import Final, Literal
a: Final = "a"
b: Literal["b"] = "b"
o: Final[str] = "o"
[typing fixtures/typing-typeddict.pyi]
[case testMatchMappingPatternCapturesTypedDictWithNonLiteral]
from typing import TypedDict
import b
class A(TypedDict):
a: str
b: int
m: A
match m:
case {b.a: v}:
reveal_type(v) # N: Revealed type is "builtins.object"
[file b.py]
from typing import Final, Literal
a: str
[typing fixtures/typing-typeddict.pyi]
[case testMatchMappingPatternCapturesTypedDictUnreachable]
# TypedDict keys are always str, so this is actually unreachable
from typing import TypedDict
import b
class A(TypedDict):
a: str
b: int
m: A
match m:
case {1: v}:
reveal_type(v)
case {b.b: v2}:
reveal_type(v2)
[file b.py]
b: int
[typing fixtures/typing-typeddict.pyi]
[case testMatchMappingPatternCaptureRest]
m: object
match m:
case {'k': 1, **r}:
reveal_type(r) # N: Revealed type is "builtins.dict[builtins.object, builtins.object]"
[builtins fixtures/dict.pyi]
[case testMatchMappingPatternCaptureRestFromMapping]
from typing import Mapping
m: Mapping[str, int]
match m:
case {'k': 1, **r}:
reveal_type(r) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
[builtins fixtures/dict.pyi]
-- Mapping patterns currently do not narrow --
-- Class Pattern --
[case testMatchClassPatternCapturePositional]
from typing import Final
class A:
__match_args__: Final = ("a", "b")
a: str
b: int
m: A
match m:
case A(i, j):
reveal_type(i) # N: Revealed type is "builtins.str"
reveal_type(j) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]
[case testMatchClassPatternMemberClassCapturePositional]
import b
m: b.A
match m:
case b.A(i, j):
reveal_type(i) # N: Revealed type is "builtins.str"
reveal_type(j) # N: Revealed type is "builtins.int"
[file b.py]
from typing import Final
class A:
__match_args__: Final = ("a", "b")
a: str
b: int
[builtins fixtures/tuple.pyi]
[case testMatchClassPatternCaptureKeyword]
class A:
a: str
b: int
m: A
match m:
case A(a=i, b=j):
reveal_type(i) # N: Revealed type is "builtins.str"
reveal_type(j) # N: Revealed type is "builtins.int"
[case testMatchClassPatternCaptureSelf]
m: object
match m:
case bool(a):
reveal_type(a) # N: Revealed type is "builtins.bool"
case bytearray(b):
reveal_type(b) # N: Revealed type is "builtins.bytearray"
case bytes(c):
reveal_type(c) # N: Revealed type is "builtins.bytes"
case dict(d):
reveal_type(d) # N: Revealed type is "builtins.dict[Any, Any]"
case float(e):
reveal_type(e) # N: Revealed type is "builtins.float"
case frozenset(f):
reveal_type(f) # N: Revealed type is "builtins.frozenset[Any]"
case int(g):
reveal_type(g) # N: Revealed type is "builtins.int"
case list(h):
reveal_type(h) # N: Revealed type is "builtins.list[Any]"
case set(i):
reveal_type(i) # N: Revealed type is "builtins.set[Any]"
case str(j):
reveal_type(j) # N: Revealed type is "builtins.str"
case tuple(k):
reveal_type(k) # N: Revealed type is "builtins.tuple[Any, ...]"
[builtins fixtures/primitives.pyi]
[case testMatchClassPatternNarrowSelfCapture]
m: object
match m:
case bool():
reveal_type(m) # N: Revealed type is "builtins.bool"
case bytearray():
reveal_type(m) # N: Revealed type is "builtins.bytearray"
case bytes():
reveal_type(m) # N: Revealed type is "builtins.bytes"
case dict():
reveal_type(m) # N: Revealed type is "builtins.dict[Any, Any]"
case float():
reveal_type(m) # N: Revealed type is "builtins.float"
case frozenset():
reveal_type(m) # N: Revealed type is "builtins.frozenset[Any]"
case int():
reveal_type(m) # N: Revealed type is "builtins.int"
case list():
reveal_type(m) # N: Revealed type is "builtins.list[Any]"
case set():
reveal_type(m) # N: Revealed type is "builtins.set[Any]"
case str():
reveal_type(m) # N: Revealed type is "builtins.str"
case tuple():
reveal_type(m) # N: Revealed type is "builtins.tuple[Any, ...]"
[builtins fixtures/primitives.pyi]
[case testMatchInvalidClassPattern]
m: object
match m:
case xyz(y): # E: Name "xyz" is not defined
reveal_type(m) # N: Revealed type is "Any"
reveal_type(y) # E: Cannot determine type of "y" \
# N: Revealed type is "Any"
match m:
case xyz(z=x): # E: Name "xyz" is not defined
reveal_type(x) # E: Cannot determine type of "x" \
# N: Revealed type is "Any"
[case testMatchClassPatternCaptureDataclass]
from dataclasses import dataclass
@dataclass
class A:
a: str
b: int
m: A
match m:
case A(i, j):
reveal_type(i) # N: Revealed type is "builtins.str"
reveal_type(j) # N: Revealed type is "builtins.int"
[builtins fixtures/dataclasses.pyi]
[case testMatchClassPatternCaptureDataclassNoMatchArgs]
from dataclasses import dataclass
@dataclass(match_args=False)
class A:
a: str
b: int
m: A
match m:
case A(i, j): # E: Class "__main__.A" doesn't define "__match_args__"
pass
[builtins fixtures/dataclasses.pyi]
[case testMatchClassPatternCaptureDataclassPartialMatchArgs]
from dataclasses import dataclass, field
@dataclass
class A:
a: str
b: int = field(init=False)
m: A
match m:
case A(i, j): # E: Too many positional patterns for class pattern
pass
case A(k):
reveal_type(k) # N: Revealed type is "builtins.str"
[builtins fixtures/dataclasses.pyi]
[case testMatchClassPatternCaptureNamedTupleInline]
from collections import namedtuple
A = namedtuple("A", ["a", "b"])
m: A
match m:
case A(i, j):
reveal_type(i) # N: Revealed type is "Any"
reveal_type(j) # N: Revealed type is "Any"
[builtins fixtures/list.pyi]
[case testMatchClassPatternCaptureNamedTupleInlineTyped]
from typing import NamedTuple
A = NamedTuple("A", [("a", str), ("b", int)])
m: A
match m:
case A(i, j):
reveal_type(i) # N: Revealed type is "builtins.str"
reveal_type(j) # N: Revealed type is "builtins.int"
[builtins fixtures/list.pyi]
[case testMatchClassPatternCaptureNamedTupleClass]
from typing import NamedTuple
class A(NamedTuple):
a: str
b: int
m: A
match m:
case A(i, j):
reveal_type(i) # N: Revealed type is "builtins.str"
reveal_type(j) # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]
[case testMatchClassPatternCaptureGeneric]
from typing import Generic, TypeVar
T = TypeVar('T')
class A(Generic[T]):
a: T
m: object
match m:
case A(a=i):
reveal_type(m) # N: Revealed type is "__main__.A[Any]"
reveal_type(i) # N: Revealed type is "Any"
[case testMatchClassPatternCaptureVariadicGeneric]
from typing import Generic, Tuple
from typing_extensions import TypeVarTuple, Unpack
Ts = TypeVarTuple('Ts')
class A(Generic[Unpack[Ts]]):
a: Tuple[Unpack[Ts]]
m: object
match m:
case A(a=i):
reveal_type(m) # N: Revealed type is "__main__.A[Unpack[builtins.tuple[Any, ...]]]"
reveal_type(i) # N: Revealed type is "builtins.tuple[Any, ...]"
[builtins fixtures/tuple.pyi]
[case testMatchClassPatternCaptureGenericAlreadyKnown]
from typing import Generic, TypeVar
T = TypeVar('T')
class A(Generic[T]):
a: T
m: A[int]
match m:
case A(a=i):
reveal_type(m) # N: Revealed type is "__main__.A[builtins.int]"
reveal_type(i) # N: Revealed type is "builtins.int"
[case testMatchClassPatternCaptureFilledGenericTypeAlias]
from typing import Generic, TypeVar
T = TypeVar('T')
class A(Generic[T]):
a: T
B = A[int]
m: object
match m:
case B(a=i): # E: Class pattern class must not be a type alias with type parameters
reveal_type(i)
[case testMatchClassPatternCaptureGenericTypeAlias]
from typing import Generic, TypeVar
T = TypeVar('T')
class A(Generic[T]):
a: T
B = A
m: object
match m:
case B(a=i):
pass
[case testMatchClassPatternNarrows]
from typing import Final
class A:
__match_args__: Final = ("a", "b")
a: str
b: int
m: object
match m:
case A():
reveal_type(m) # N: Revealed type is "__main__.A"
case A(i, j):
reveal_type(m) # N: Revealed type is "__main__.A"
[builtins fixtures/tuple.pyi]
[case testMatchClassPatternNarrowsUnion]
from typing import Final, Union
class A:
__match_args__: Final = ("a", "b")
a: str
b: int
class B:
__match_args__: Final = ("a", "b")
a: int
b: str
m: Union[A, B]
match m:
case A():
reveal_type(m) # N: Revealed type is "__main__.A"
match m:
case A(i, j):
reveal_type(m) # N: Revealed type is "__main__.A"
reveal_type(i) # N: Revealed type is "builtins.str"
reveal_type(j) # N: Revealed type is "builtins.int"
match m:
case B():
reveal_type(m) # N: Revealed type is "__main__.B"
match m:
case B(k, l):
reveal_type(m) # N: Revealed type is "__main__.B"
reveal_type(k) # N: Revealed type is "builtins.int"
reveal_type(l) # N: Revealed type is "builtins.str"
[builtins fixtures/tuple.pyi]
[case testMatchClassPatternAlreadyNarrower]
from typing import Final
class A:
__match_args__: Final = ("a", "b")
a: str
b: int
class B(A): ...
m: B
match m:
case A():
reveal_type(m) # N: Revealed type is "__main__.B"
match m:
case A(i, j):
reveal_type(m) # N: Revealed type is "__main__.B"
[builtins fixtures/tuple.pyi]
[case testMatchClassPatternIntersection]
from typing import Final
class A:
__match_args__: Final = ("a", "b")
a: str
b: int
class B: ...
m: B
match m:
case A():
reveal_type(m) # N: Revealed type is "__main__.<subclass of "B" and "A">2"
case A(i, j):
reveal_type(m) # N: Revealed type is "__main__.<subclass of "B" and "A">3"
[builtins fixtures/tuple.pyi]
[case testMatchClassPatternNonexistentKeyword]
class A: ...
m: object
match m:
case A(a=j): # E: Class "__main__.A" has no attribute "a"
reveal_type(m) # N: Revealed type is "__main__.A"
reveal_type(j) # N: Revealed type is "Any"
[case testMatchClassPatternDuplicateKeyword]
class A:
a: str
m: object
match m:
case A(a=i, a=j): # E: Duplicate keyword pattern "a"
pass
[case testMatchClassPatternDuplicateImplicitKeyword]
from typing import Final
class A:
__match_args__: Final = ("a",)
a: str
m: object
match m:
case A(i, a=j): # E: Keyword "a" already matches a positional pattern
pass
[builtins fixtures/tuple.pyi]
[case testMatchClassPatternTooManyPositionals]
from typing import Final
class A:
__match_args__: Final = ("a", "b")
a: str
b: int
m: object
match m:
case A(i, j, k): # E: Too many positional patterns for class pattern
pass
[builtins fixtures/tuple.pyi]
[case testMatchClassPatternIsNotType]
a = 1
m: object
match m:
case a(i, j): # E: Expected type in class pattern; found "builtins.int"
reveal_type(i)
reveal_type(j)
[case testMatchClassPatternAny]
from typing import Any
Foo: Any
m: object
match m:
case Foo():
pass
[case testMatchClassPatternNestedGenerics]
# From cpython test_patma.py
x = [[{0: 0}]]
match x:
case list([({-0-0j: int(real=0+0j, imag=0-0j) | (1) as z},)]):
y = 0
reveal_type(x) # N: Revealed type is "builtins.list[builtins.list[builtins.dict[builtins.int, builtins.int]]]"
reveal_type(y) # N: Revealed type is "builtins.int"
reveal_type(z) # N: Revealed type is "builtins.int"
[builtins fixtures/dict-full.pyi]