-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
check-parameter-specification.test
1028 lines (745 loc) · 32.6 KB
/
check-parameter-specification.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
[case testBasicParamSpec]
from typing_extensions import ParamSpec
P = ParamSpec('P')
[builtins fixtures/tuple.pyi]
[case testInvalidParamSpecDefinitions]
from typing import ParamSpec
P1 = ParamSpec("P1", covariant=True) # E: Only the first argument to ParamSpec has defined semantics
P2 = ParamSpec("P2", contravariant=True) # E: Only the first argument to ParamSpec has defined semantics
P3 = ParamSpec("P3", bound=int) # E: Only the first argument to ParamSpec has defined semantics
P4 = ParamSpec("P4", int, str) # E: Only the first argument to ParamSpec has defined semantics
P5 = ParamSpec("P5", covariant=True, bound=int) # E: Only the first argument to ParamSpec has defined semantics
[builtins fixtures/tuple.pyi]
[case testParamSpecLocations]
from typing import Callable, List
from typing_extensions import ParamSpec, Concatenate
P = ParamSpec('P')
x: P # E: ParamSpec "P" is unbound
def foo1(x: Callable[P, int]) -> Callable[P, str]: ...
def foo2(x: P) -> P: ... # E: Invalid location for ParamSpec "P" \
# N: You can use ParamSpec as the first argument to Callable, e.g., 'Callable[P, int]'
def foo3(x: Concatenate[int, P]) -> int: ... # E: Invalid location for Concatenate
def foo4(x: List[P]) -> None: ... # E: Invalid location for ParamSpec "P" \
# N: You can use ParamSpec as the first argument to Callable, e.g., 'Callable[P, int]'
def foo5(x: Callable[[int, str], P]) -> None: ... # E: Invalid location for ParamSpec "P" \
# N: You can use ParamSpec as the first argument to Callable, e.g., 'Callable[P, int]'
def foo6(x: Callable[[P], int]) -> None: ... # E: Invalid location for ParamSpec "P" \
# N: You can use ParamSpec as the first argument to Callable, e.g., 'Callable[P, int]'
[builtins fixtures/tuple.pyi]
[case testParamSpecContextManagerLike]
from typing import Callable, List, Iterator, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec('P')
T = TypeVar('T')
def tmpcontextmanagerlike(x: Callable[P, Iterator[T]]) -> Callable[P, List[T]]: ...
@tmpcontextmanagerlike
def whatever(x: int) -> Iterator[int]:
yield x
reveal_type(whatever) # N: Revealed type is "def (x: builtins.int) -> builtins.list[builtins.int*]"
reveal_type(whatever(217)) # N: Revealed type is "builtins.list[builtins.int*]"
[builtins fixtures/tuple.pyi]
[case testInvalidParamSpecType]
# flags: --python-version 3.10
from typing import ParamSpec
P = ParamSpec("P")
class MyFunction(P): # E: Invalid base class "P"
...
[case testParamSpecRevealType]
from typing import Callable
from typing_extensions import ParamSpec
P = ParamSpec('P')
def f(x: Callable[P, int]) -> None: ...
reveal_type(f) # N: Revealed type is "def [P] (x: def (*P.args, **P.kwargs) -> builtins.int)"
[builtins fixtures/tuple.pyi]
[case testParamSpecSimpleFunction]
from typing import Callable, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec('P')
def changes_return_type_to_str(x: Callable[P, int]) -> Callable[P, str]: ...
def returns_int(a: str, b: bool) -> int: ...
reveal_type(changes_return_type_to_str(returns_int)) # N: Revealed type is "def (a: builtins.str, b: builtins.bool) -> builtins.str"
[builtins fixtures/tuple.pyi]
[case testParamSpecSimpleClass]
from typing import Callable, TypeVar, Generic
from typing_extensions import ParamSpec
P = ParamSpec('P')
class C(Generic[P]):
def __init__(self, x: Callable[P, None]) -> None: ...
def m(self, *args: P.args, **kwargs: P.kwargs) -> int:
return 1
def f(x: int, y: str) -> None: ...
reveal_type(C(f)) # N: Revealed type is "__main__.C[[x: builtins.int, y: builtins.str]]"
reveal_type(C(f).m) # N: Revealed type is "def (x: builtins.int, y: builtins.str) -> builtins.int"
[builtins fixtures/dict.pyi]
[case testParamSpecClassWithPrefixArgument]
from typing import Callable, TypeVar, Generic
from typing_extensions import ParamSpec
P = ParamSpec('P')
class C(Generic[P]):
def __init__(self, x: Callable[P, None]) -> None: ...
def m(self, a: str, *args: P.args, **kwargs: P.kwargs) -> int:
return 1
def f(x: int, y: str) -> None: ...
reveal_type(C(f).m) # N: Revealed type is "def (a: builtins.str, x: builtins.int, y: builtins.str) -> builtins.int"
reveal_type(C(f).m('', 1, '')) # N: Revealed type is "builtins.int"
[builtins fixtures/dict.pyi]
[case testParamSpecDecorator]
from typing import Callable, TypeVar, Generic
from typing_extensions import ParamSpec
P = ParamSpec('P')
R = TypeVar('R')
class W(Generic[P, R]):
f: Callable[P, R]
x: int
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R:
reveal_type(self.f(*args, **kwargs)) # N: Revealed type is "R`2"
return self.f(*args, **kwargs)
def dec() -> Callable[[Callable[P, R]], W[P, R]]:
pass
@dec()
def f(a: int, b: str) -> None: ...
reveal_type(f) # N: Revealed type is "__main__.W[[a: builtins.int, b: builtins.str], None]"
reveal_type(f(1, '')) # N: Revealed type is "None"
reveal_type(f.x) # N: Revealed type is "builtins.int"
## TODO: How should this work?
#
# class C:
# @dec()
# def m(self, x: int) -> str: ...
#
# reveal_type(C().m(x=1))
[builtins fixtures/dict.pyi]
[case testParamSpecFunction]
from typing import Callable, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec('P')
R = TypeVar('R')
def f(x: Callable[P, R], *args: P.args, **kwargs: P.kwargs) -> R:
return x(*args, **kwargs)
def g(x: int, y: str) -> None: ...
reveal_type(f(g, 1, y='x')) # N: Revealed type is "None"
f(g, 'x', y='x') # E: Argument 2 to "f" has incompatible type "str"; expected "int"
f(g, 1, y=1) # E: Argument "y" to "f" has incompatible type "int"; expected "str"
f(g) # E: Missing positional arguments "x", "y" in call to "f"
[builtins fixtures/dict.pyi]
[case testParamSpecSpecialCase]
from typing import Callable, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec('P')
T = TypeVar('T')
def register(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> Callable[P, T]: ...
def f(x: int, y: str, z: int, a: str) -> None: ...
x = register(f, 1, '', 1, '')
[builtins fixtures/dict.pyi]
[case testParamSpecInferredFromAny]
from typing import Callable, Any
from typing_extensions import ParamSpec
P = ParamSpec('P')
def f(x: Callable[P, int]) -> Callable[P, str]: ...
g: Any
reveal_type(f(g)) # N: Revealed type is "def (*Any, **Any) -> builtins.str"
f(g)(1, 3, x=1, y=2)
[builtins fixtures/tuple.pyi]
[case testParamSpecDecoratorImplementation]
from typing import Callable, Any, TypeVar, List
from typing_extensions import ParamSpec
P = ParamSpec('P')
T = TypeVar('T')
def dec(f: Callable[P, T]) -> Callable[P, List[T]]:
def wrapper(*args: P.args, **kwargs: P.kwargs) -> List[T]:
return [f(*args, **kwargs)]
return wrapper
@dec
def g(x: int, y: str = '') -> int: ...
reveal_type(g) # N: Revealed type is "def (x: builtins.int, y: builtins.str =) -> builtins.list[builtins.int*]"
[builtins fixtures/dict.pyi]
[case testParamSpecArgsAndKwargsTypes]
from typing import Callable, TypeVar, Generic
from typing_extensions import ParamSpec
P = ParamSpec('P')
class C(Generic[P]):
def __init__(self, x: Callable[P, None]) -> None: ...
def m(self, *args: P.args, **kwargs: P.kwargs) -> None:
reveal_type(args) # N: Revealed type is "P.args`1"
reveal_type(kwargs) # N: Revealed type is "P.kwargs`1"
[builtins fixtures/dict.pyi]
[case testParamSpecSubtypeChecking1]
from typing import Callable, TypeVar, Generic, Any
from typing_extensions import ParamSpec
P = ParamSpec('P')
class C(Generic[P]):
def __init__(self, x: Callable[P, None]) -> None: ...
def m(self, *args: P.args, **kwargs: P.kwargs) -> None:
args = args
kwargs = kwargs
o: object
o = args
o = kwargs
o2: object
args = o2 # E: Incompatible types in assignment (expression has type "object", variable has type "P.args")
kwargs = o2 # E: Incompatible types in assignment (expression has type "object", variable has type "P.kwargs")
a: Any
a = args
a = kwargs
args = kwargs # E: Incompatible types in assignment (expression has type "P.kwargs", variable has type "P.args")
kwargs = args # E: Incompatible types in assignment (expression has type "P.args", variable has type "P.kwargs")
args = a
kwargs = a
[builtins fixtures/dict.pyi]
[case testParamSpecSubtypeChecking2]
from typing import Callable, Generic
from typing_extensions import ParamSpec
P = ParamSpec('P')
P2 = ParamSpec('P2')
class C(Generic[P]):
pass
def f(c1: C[P], c2: C[P2]) -> None:
c1 = c1
c2 = c2
c1 = c2 # E: Incompatible types in assignment (expression has type "C[P2]", variable has type "C[P]")
c2 = c1 # E: Incompatible types in assignment (expression has type "C[P]", variable has type "C[P2]")
def g(f: Callable[P, None], g: Callable[P2, None]) -> None:
f = f
g = g
f = g # E: Incompatible types in assignment (expression has type "Callable[P2, None]", variable has type "Callable[P, None]")
g = f # E: Incompatible types in assignment (expression has type "Callable[P, None]", variable has type "Callable[P2, None]")
[builtins fixtures/dict.pyi]
[case testParamSpecJoin]
from typing import Callable, Generic, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec('P')
P2 = ParamSpec('P2')
P3 = ParamSpec('P3')
T = TypeVar('T')
def join(x: T, y: T) -> T: ...
class C(Generic[P, P2]):
def m(self, f: Callable[P, None], g: Callable[P2, None]) -> None:
reveal_type(join(f, f)) # N: Revealed type is "def (*P.args, **P.kwargs)"
reveal_type(join(f, g)) # N: Revealed type is "builtins.function*"
def m2(self, *args: P.args, **kwargs: P.kwargs) -> None:
reveal_type(join(args, args)) # N: Revealed type is "P.args`1"
reveal_type(join(kwargs, kwargs)) # N: Revealed type is "P.kwargs`1"
reveal_type(join(args, kwargs)) # N: Revealed type is "builtins.object*"
def f(*args2: P2.args, **kwargs2: P2.kwargs) -> None:
reveal_type(join(args, args2)) # N: Revealed type is "builtins.object*"
reveal_type(join(kwargs, kwargs2)) # N: Revealed type is "builtins.object*"
def m3(self, c: C[P, P3]) -> None:
reveal_type(join(c, c)) # N: Revealed type is "__main__.C*[P`1, P3`-1]"
reveal_type(join(self, c)) # N: Revealed type is "builtins.object*"
[builtins fixtures/dict.pyi]
[case testParamSpecClassWithAny]
from typing import Callable, Generic, Any
from typing_extensions import ParamSpec
P = ParamSpec('P')
class C(Generic[P]):
def __init__(self, x: Callable[P, None]) -> None: ...
def m(self, *args: P.args, **kwargs: P.kwargs) -> int:
return 1
c: C[Any]
reveal_type(c) # N: Revealed type is "__main__.C[Any]"
reveal_type(c.m) # N: Revealed type is "def (*args: Any, **kwargs: Any) -> builtins.int"
c.m(4, 6, y='x')
c = c
def f() -> None: pass
c2 = C(f)
c2 = c
c3 = C(f)
c = c3
[builtins fixtures/dict.pyi]
[case testParamSpecInferredFromLambda]
from typing import Callable, TypeVar
from typing_extensions import ParamSpec
P = ParamSpec('P')
T = TypeVar('T')
# Similar to atexit.register
def register(f: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> Callable[P, T]: ... # N: "register" defined here
def f(x: int) -> None: pass
reveal_type(register(lambda: f(1))) # N: Revealed type is "def ()"
reveal_type(register(lambda x: f(x), x=1)) # N: Revealed type is "def (x: Any)"
register(lambda x: f(x)) # E: Missing positional argument "x" in call to "register"
register(lambda x: f(x), y=1) # E: Unexpected keyword argument "y" for "register"
[builtins fixtures/dict.pyi]
[case testParamSpecInvalidCalls]
from typing import Callable, Generic
from typing_extensions import ParamSpec
P = ParamSpec('P')
P2 = ParamSpec('P2')
class C(Generic[P, P2]):
def m1(self, *args: P.args, **kwargs: P.kwargs) -> None:
self.m1(*args, **kwargs)
self.m2(*args, **kwargs) # E: Argument 1 to "m2" of "C" has incompatible type "*P.args"; expected "P2.args" \
# E: Argument 2 to "m2" of "C" has incompatible type "**P.kwargs"; expected "P2.kwargs"
self.m1(*kwargs, **args) # E: Argument 1 to "m1" of "C" has incompatible type "*P.kwargs"; expected "P.args" \
# E: Argument 2 to "m1" of "C" has incompatible type "**P.args"; expected "P.kwargs"
self.m3(*args, **kwargs) # E: Argument 1 to "m3" of "C" has incompatible type "*P.args"; expected "int" \
# E: Argument 2 to "m3" of "C" has incompatible type "**P.kwargs"; expected "int"
self.m4(*args, **kwargs) # E: Argument 1 to "m4" of "C" has incompatible type "*P.args"; expected "int" \
# E: Argument 2 to "m4" of "C" has incompatible type "**P.kwargs"; expected "int"
self.m1(*args, **args) # E: Argument 2 to "m1" of "C" has incompatible type "**P.args"; expected "P.kwargs"
self.m1(*kwargs, **kwargs) # E: Argument 1 to "m1" of "C" has incompatible type "*P.kwargs"; expected "P.args"
def m2(self, *args: P2.args, **kwargs: P2.kwargs) -> None:
pass
def m3(self, *args: int, **kwargs: int) -> None:
pass
def m4(self, x: int) -> None:
pass
[builtins fixtures/dict.pyi]
[case testParamSpecOverUnannotatedDecorator]
from typing import Callable, Iterator, TypeVar, ContextManager, Any
from typing_extensions import ParamSpec
from nonexistent import deco2 # type: ignore
T = TypeVar("T")
P = ParamSpec("P")
T_co = TypeVar("T_co", covariant=True)
class CM(ContextManager[T_co]):
def __call__(self, func: T) -> T: ...
def deco1(
func: Callable[P, Iterator[T]]) -> Callable[P, CM[T]]: ...
@deco1
@deco2
def f():
pass
reveal_type(f) # N: Revealed type is "def (*Any, **Any) -> __main__.CM[Any]"
with f() as x:
pass
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]
[case testParamSpecLiterals]
from typing_extensions import ParamSpec, TypeAlias
from typing import Generic, TypeVar
P = ParamSpec("P")
T = TypeVar("T")
class Z(Generic[P]): ...
# literals can be applied
n: Z[[int]]
# TODO: type aliases too
nt1 = Z[[int]]
nt2: TypeAlias = Z[[int]]
unt1: nt1
unt2: nt2
# literals actually keep types
reveal_type(n) # N: Revealed type is "__main__.Z[[builtins.int]]"
reveal_type(unt1) # N: Revealed type is "__main__.Z[[builtins.int]]"
reveal_type(unt2) # N: Revealed type is "__main__.Z[[builtins.int]]"
# passing into a function keeps the type
def fT(a: T) -> T: ...
def fP(a: Z[P]) -> Z[P]: ...
reveal_type(fT(n)) # N: Revealed type is "__main__.Z*[[builtins.int]]"
reveal_type(fP(n)) # N: Revealed type is "__main__.Z[[builtins.int]]"
# literals can be in function args and return type
def k(a: Z[[int]]) -> Z[[str]]: ...
# functions work
reveal_type(k(n)) # N: Revealed type is "__main__.Z[[builtins.str]]"
# literals can be matched in arguments
def kb(a: Z[[bytes]]) -> Z[[str]]: ...
reveal_type(kb(n)) # N: Revealed type is "__main__.Z[[builtins.str]]" \
# E: Argument 1 to "kb" has incompatible type "Z[[int]]"; expected "Z[[bytes]]"
n2: Z[bytes]
reveal_type(kb(n2)) # N: Revealed type is "__main__.Z[[builtins.str]]"
[builtins fixtures/tuple.pyi]
[case testParamSpecConcatenateFromPep]
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, TypeVar, Generic
P = ParamSpec("P")
R = TypeVar("R")
# CASE 1
class Request:
...
def with_request(f: Callable[Concatenate[Request, P], R]) -> Callable[P, R]:
def inner(*args: P.args, **kwargs: P.kwargs) -> R:
return f(Request(), *args, **kwargs)
return inner
@with_request
def takes_int_str(request: Request, x: int, y: str) -> int:
# use request
return x + 7
reveal_type(takes_int_str) # N: Revealed type is "def (x: builtins.int, y: builtins.str) -> builtins.int*"
takes_int_str(1, "A") # Accepted
takes_int_str("B", 2) # E: Argument 1 to "takes_int_str" has incompatible type "str"; expected "int" \
# E: Argument 2 to "takes_int_str" has incompatible type "int"; expected "str"
# CASE 2
T = TypeVar("T")
P_2 = ParamSpec("P_2")
class X(Generic[T, P]):
f: Callable[P, int]
x: T
def f1(x: X[int, P_2]) -> str: ... # Accepted
def f2(x: X[int, Concatenate[int, P_2]]) -> str: ... # Accepted
def f3(x: X[int, [int, bool]]) -> str: ... # Accepted
# ellipsis only show up here, but I can assume it works like Callable[..., R]
def f4(x: X[int, ...]) -> str: ... # Accepted
# TODO: this is not rejected:
# def f5(x: X[int, int]) -> str: ... # Rejected
# CASE 3
def bar(x: int, *args: bool) -> int: ...
def add(x: Callable[P, int]) -> Callable[Concatenate[str, P], bool]: ...
reveal_type(add(bar)) # N: Revealed type is "def (builtins.str, x: builtins.int, *args: builtins.bool) -> builtins.bool"
def remove(x: Callable[Concatenate[int, P], int]) -> Callable[P, bool]: ...
reveal_type(remove(bar)) # N: Revealed type is "def (*args: builtins.bool) -> builtins.bool"
def transform(
x: Callable[Concatenate[int, P], int]
) -> Callable[Concatenate[str, P], bool]: ...
# In the PEP, "__a" appears. What is that? Autogenerated names? To what spec?
reveal_type(transform(bar)) # N: Revealed type is "def (builtins.str, *args: builtins.bool) -> builtins.bool"
# CASE 4
def expects_int_first(x: Callable[Concatenate[int, P], int]) -> None: ...
@expects_int_first # E: Argument 1 to "expects_int_first" has incompatible type "Callable[[str], int]"; expected "Callable[[int], int]" \
# N: This may be because "one" has arguments named: "x"
def one(x: str) -> int: ...
@expects_int_first # E: Argument 1 to "expects_int_first" has incompatible type "Callable[[NamedArg(int, 'x')], int]"; expected "Callable[[int], int]"
def two(*, x: int) -> int: ...
@expects_int_first # E: Argument 1 to "expects_int_first" has incompatible type "Callable[[KwArg(int)], int]"; expected "Callable[[int], int]"
def three(**kwargs: int) -> int: ...
@expects_int_first # Accepted
def four(*args: int) -> int: ...
[builtins fixtures/tuple.pyi]
[builtins fixtures/dict.pyi]
[case testParamSpecTwiceSolving]
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, TypeVar
P = ParamSpec("P")
R = TypeVar("R")
def f(one: Callable[Concatenate[int, P], R], two: Callable[Concatenate[str, P], R]) -> Callable[P, R]: ...
a: Callable[[int, bytes], str]
b: Callable[[str, bytes], str]
reveal_type(f(a, b)) # N: Revealed type is "def (builtins.bytes) -> builtins.str*"
[builtins fixtures/tuple.pyi]
[case testParamSpecConcatenateInReturn]
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, Protocol
P = ParamSpec("P")
def f(i: Callable[Concatenate[int, P], str]) -> Callable[Concatenate[int, P], str]: ...
n: Callable[[int, bytes], str]
reveal_type(f(n)) # N: Revealed type is "def (builtins.int, builtins.bytes) -> builtins.str"
[builtins fixtures/tuple.pyi]
[case testParamSpecConcatenateNamedArgs]
# flags: --strict-concatenate
# this is one noticeable deviation from PEP but I believe it is for the better
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, TypeVar
P = ParamSpec("P")
R = TypeVar("R")
def f1(c: Callable[P, R]) -> Callable[Concatenate[int, P], R]:
def result(x: int, /, *args: P.args, **kwargs: P.kwargs) -> R: ...
return result # Accepted
def f2(c: Callable[P, R]) -> Callable[Concatenate[int, P], R]:
def result(x: int, *args: P.args, **kwargs: P.kwargs) -> R: ...
return result # Rejected
# reason for rejection:
f2(lambda x: 42)(42, x=42)
[builtins fixtures/tuple.pyi]
[out]
main:10: error: invalid syntax
[out version>=3.8]
main:17: error: Incompatible return value type (got "Callable[Concatenate[Arg(int, 'x'), P], R]", expected "Callable[Concatenate[int, P], R]")
main:17: note: This may be because "result" has arguments named: "x"
[case testNonStrictParamSpecConcatenateNamedArgs]
# this is one noticeable deviation from PEP but I believe it is for the better
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, TypeVar
P = ParamSpec("P")
R = TypeVar("R")
def f1(c: Callable[P, R]) -> Callable[Concatenate[int, P], R]:
def result(x: int, /, *args: P.args, **kwargs: P.kwargs) -> R: ...
return result # Accepted
def f2(c: Callable[P, R]) -> Callable[Concatenate[int, P], R]:
def result(x: int, *args: P.args, **kwargs: P.kwargs) -> R: ...
return result # Rejected -> Accepted
# reason for rejection:
f2(lambda x: 42)(42, x=42)
[builtins fixtures/tuple.pyi]
[out]
main:9: error: invalid syntax
[out version>=3.8]
[case testParamSpecConcatenateWithTypeVar]
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, TypeVar
P = ParamSpec("P")
R = TypeVar("R")
S = TypeVar("S")
def f(c: Callable[Concatenate[S, P], R]) -> Callable[Concatenate[S, P], R]: ...
def a(n: int) -> None: ...
n = f(a)
reveal_type(n) # N: Revealed type is "def (builtins.int*)"
reveal_type(n(42)) # N: Revealed type is "None"
[builtins fixtures/tuple.pyi]
[case testCallablesAsParameters]
# credits to https://github.com/microsoft/pyright/issues/2705
from typing_extensions import ParamSpec, Concatenate
from typing import Generic, Callable, Any
P = ParamSpec("P")
class Foo(Generic[P]):
def __init__(self, func: Callable[P, Any]) -> None: ...
def bar(baz: Foo[Concatenate[int, P]]) -> Foo[P]: ...
def test(a: int, /, b: str) -> str: ...
abc = Foo(test)
reveal_type(abc)
bar(abc)
[builtins fixtures/tuple.pyi]
[out]
main:11: error: invalid syntax
[out version>=3.8]
main:14: note: Revealed type is "__main__.Foo[[builtins.int, b: builtins.str]]"
[case testSolveParamSpecWithSelfType]
from typing_extensions import ParamSpec, Concatenate
from typing import Callable, Generic
P = ParamSpec("P")
class Foo(Generic[P]):
def foo(self: 'Foo[P]', other: Callable[P, None]) -> None: ...
n: Foo[[int]]
def f(x: int) -> None: ...
n.foo(f)
[builtins fixtures/tuple.pyi]
[case testParamSpecLiteralsTypeApplication]
from typing_extensions import ParamSpec
from typing import Generic, Callable
P = ParamSpec("P")
class Z(Generic[P]):
def __init__(self, c: Callable[P, None]) -> None:
...
# it allows valid functions
reveal_type(Z[[int]](lambda x: None)) # N: Revealed type is "__main__.Z[[builtins.int]]"
reveal_type(Z[[]](lambda: None)) # N: Revealed type is "__main__.Z[[]]"
reveal_type(Z[bytes, str](lambda b, s: None)) # N: Revealed type is "__main__.Z[[builtins.bytes, builtins.str]]"
# it disallows invalid functions
def f1(n: str) -> None: ...
def f2(b: bytes, i: int) -> None: ...
Z[[int]](lambda one, two: None) # E: Cannot infer type of lambda \
# E: Argument 1 to "Z" has incompatible type "Callable[[Any, Any], None]"; expected "Callable[[int], None]"
Z[[int]](f1) # E: Argument 1 to "Z" has incompatible type "Callable[[str], None]"; expected "Callable[[int], None]"
Z[[]](lambda one: None) # E: Cannot infer type of lambda \
# E: Argument 1 to "Z" has incompatible type "Callable[[Any], None]"; expected "Callable[[], None]"
Z[bytes, str](lambda one: None) # E: Cannot infer type of lambda \
# E: Argument 1 to "Z" has incompatible type "Callable[[Any], None]"; expected "Callable[[bytes, str], None]"
Z[bytes, str](f2) # E: Argument 1 to "Z" has incompatible type "Callable[[bytes, int], None]"; expected "Callable[[bytes, str], None]"
[builtins fixtures/tuple.pyi]
[case testParamSpecLiteralEllipsis]
from typing_extensions import ParamSpec
from typing import Generic, Callable
P = ParamSpec("P")
class Z(Generic[P]):
def __init__(self: 'Z[P]', c: Callable[P, None]) -> None:
...
def f1() -> None: ...
def f2(*args: int) -> None: ...
def f3(a: int, *, b: bytes) -> None: ...
def f4(b: bytes) -> None: ...
argh: Callable[..., None] = f4
# check it works
Z[...](f1)
Z[...](f2)
Z[...](f3)
# check subtyping works
n: Z[...]
n = Z(f1)
n = Z(f2)
n = Z(f3)
[builtins fixtures/tuple.pyi]
[case testParamSpecApplyConcatenateTwice]
from typing_extensions import ParamSpec, Concatenate
from typing import Generic, Callable, Optional
P = ParamSpec("P")
class C(Generic[P]):
# think PhantomData<T> from rust
phantom: Optional[Callable[P, None]]
def add_str(self) -> C[Concatenate[int, P]]:
return C[Concatenate[int, P]]()
def add_int(self) -> C[Concatenate[str, P]]:
return C[Concatenate[str, P]]()
def f(c: C[P]) -> None:
reveal_type(c) # N: Revealed type is "__main__.C[P`-1]"
n1 = c.add_str()
reveal_type(n1) # N: Revealed type is "__main__.C[Concatenate[builtins.int, P`-1]]"
n2 = n1.add_int()
reveal_type(n2) # N: Revealed type is "__main__.C[Concatenate[builtins.str, builtins.int, P`-1]]"
p1 = c.add_int()
reveal_type(p1) # N: Revealed type is "__main__.C[Concatenate[builtins.str, P`-1]]"
p2 = p1.add_str()
reveal_type(p2) # N: Revealed type is "__main__.C[Concatenate[builtins.int, builtins.str, P`-1]]"
[builtins fixtures/tuple.pyi]
[case testParamSpecLiteralJoin]
from typing import Generic, Callable, Union
from typing_extensions import ParamSpec
_P = ParamSpec("_P")
class Job(Generic[_P]):
def __init__(self, target: Callable[_P, None]) -> None:
self.target = target
def func(
action: Union[Job[int], Callable[[int], None]],
) -> None:
job = action if isinstance(action, Job) else Job(action)
reveal_type(job) # N: Revealed type is "__main__.Job[[builtins.int]]"
[builtins fixtures/tuple.pyi]
[case testApplyParamSpecToParamSpecLiterals]
from typing import TypeVar, Generic, Callable
from typing_extensions import ParamSpec
_P = ParamSpec("_P")
_R_co = TypeVar("_R_co", covariant=True)
class Job(Generic[_P, _R_co]):
def __init__(self, target: Callable[_P, _R_co]) -> None:
self.target = target
def run_job(job: Job[_P, None], *args: _P.args, **kwargs: _P.kwargs) -> None: # N: "run_job" defined here
...
def func(job: Job[[int, str], None]) -> None:
run_job(job, 42, "Hello")
run_job(job, "Hello", 42) # E: Argument 2 to "run_job" has incompatible type "str"; expected "int" \
# E: Argument 3 to "run_job" has incompatible type "int"; expected "str"
run_job(job, 42, msg="Hello") # E: Unexpected keyword argument "msg" for "run_job"
run_job(job, "Hello") # E: Too few arguments for "run_job" \
# E: Argument 2 to "run_job" has incompatible type "str"; expected "int"
def func2(job: Job[..., None]) -> None:
run_job(job, 42, "Hello")
run_job(job, "Hello", 42)
run_job(job, 42, msg="Hello")
run_job(job, x=42, msg="Hello")
[builtins fixtures/tuple.pyi]
[case testExpandNonBareParamSpecAgainstCallable]
from typing import Callable, TypeVar, Any
from typing_extensions import ParamSpec
CallableT = TypeVar("CallableT", bound=Callable[..., Any])
_P = ParamSpec("_P")
_R = TypeVar("_R")
def simple_decorator(callable: CallableT) -> CallableT:
# set some attribute on 'callable'
return callable
class A:
@simple_decorator
def func(self, action: Callable[_P, _R], *args: _P.args, **kwargs: _P.kwargs) -> _R:
...
reveal_type(A.func) # N: Revealed type is "def [_P, _R] (self: __main__.A, action: def (*_P.args, **_P.kwargs) -> _R`-2, *_P.args, **_P.kwargs) -> _R`-2"
# TODO: _R`<n> keeps flip-flopping between 5 (?), 13, 14, 15. Spooky.
# reveal_type(A().func) $ N: Revealed type is "def [_P, _R] (action: def (*_P.args, **_P.kwargs) -> _R`13, *_P.args, **_P.kwargs) -> _R`13"
def f(x: int) -> int:
...
reveal_type(A().func(f, 42)) # N: Revealed type is "builtins.int*"
# TODO: this should reveal `int`
reveal_type(A().func(lambda x: x + x, 42)) # N: Revealed type is "Any"
[builtins fixtures/tuple.pyi]
[case testParamSpecConstraintOnOtherParamSpec]
from typing import Callable, TypeVar, Any, Generic
from typing_extensions import ParamSpec
CallableT = TypeVar("CallableT", bound=Callable[..., Any])
_P = ParamSpec("_P")
_R_co = TypeVar("_R_co", covariant=True)
def simple_decorator(callable: CallableT) -> CallableT:
...
class Job(Generic[_P, _R_co]):
def __init__(self, target: Callable[_P, _R_co]) -> None:
...
class A:
@simple_decorator
def func(self, action: Job[_P, None]) -> Job[_P, None]:
...
reveal_type(A.func) # N: Revealed type is "def [_P] (self: __main__.A, action: __main__.Job[_P`-1, None]) -> __main__.Job[_P`-1, None]"
# TODO: flakey, _P`4 alternates around.
# reveal_type(A().func) $ N: Revealed type is "def [_P] (action: __main__.Job[_P`4, None]) -> __main__.Job[_P`4, None]"
reveal_type(A().func(Job(lambda x: x))) # N: Revealed type is "__main__.Job[[x: Any], None]"
def f(x: int, y: int) -> None: ...
reveal_type(A().func(Job(f))) # N: Revealed type is "__main__.Job[[x: builtins.int, y: builtins.int], None]"
[builtins fixtures/tuple.pyi]
[case testConstraintBetweenParamSpecFunctions1]
from typing import Callable, TypeVar, Any, Generic
from typing_extensions import ParamSpec
_P = ParamSpec("_P")
_R_co = TypeVar("_R_co", covariant=True)
def simple_decorator(callable: Callable[_P, _R_co]) -> Callable[_P, _R_co]: ...
class Job(Generic[_P]): ...
@simple_decorator
def func(__action: Job[_P]) -> Callable[_P, None]:
...
reveal_type(func) # N: Revealed type is "def [_P] (__main__.Job[_P`-1]) -> def (*_P.args, **_P.kwargs)"
[builtins fixtures/tuple.pyi]
[case testConstraintBetweenParamSpecFunctions2]
from typing import Callable, TypeVar, Any, Generic
from typing_extensions import ParamSpec
CallableT = TypeVar("CallableT", bound=Callable[..., Any])
_P = ParamSpec("_P")
def simple_decorator(callable: CallableT) -> CallableT: ...
class Job(Generic[_P]): ...
@simple_decorator
def func(__action: Job[_P]) -> Callable[_P, None]:
...
reveal_type(func) # N: Revealed type is "def [_P] (__main__.Job[_P`-1]) -> def (*_P.args, **_P.kwargs)"
[builtins fixtures/tuple.pyi]
[case testConstraintsBetweenConcatenatePrefixes]
from typing import Any, Callable, Generic, TypeVar
from typing_extensions import Concatenate, ParamSpec
_P = ParamSpec("_P")
_T = TypeVar("_T")
class Awaitable(Generic[_T]): ...
def adds_await() -> Callable[
[Callable[Concatenate[_T, _P], None]],
Callable[Concatenate[_T, _P], Awaitable[None]],
]:
def decorator(
func: Callable[Concatenate[_T, _P], None],
) -> Callable[Concatenate[_T, _P], Awaitable[None]]:
...
return decorator # we want `_T` and `_P` to refer to the same things.
[builtins fixtures/tuple.pyi]
[case testParamSpecVariance]
from typing import Callable, Generic
from typing_extensions import ParamSpec
_P = ParamSpec("_P")
class Job(Generic[_P]):
def __init__(self, target: Callable[_P, None]) -> None: ...
def into_callable(self) -> Callable[_P, None]: ...
class A:
def func(self, var: int) -> None: ...
def other_func(self, job: Job[[int]]) -> None: ...
job = Job(A().func)
reveal_type(job) # N: Revealed type is "__main__.Job[[var: builtins.int]]"
A().other_func(job) # This should NOT error (despite the keyword)
# and yet the keyword should remain
job.into_callable()(var=42)
job.into_callable()(x=42) # E: Unexpected keyword argument "x"
# similar for other functions
def f1(n: object) -> None: ...
def f2(n: int) -> None: ...
def f3(n: bool) -> None: ...
# just like how this is legal...
a1: Callable[[bool], None]
a1 = f3
a1 = f2
a1 = f1
# ... this is also legal
a2: Job[[bool]]
a2 = Job(f3)
a2 = Job(f2)
a2 = Job(f1)
# and this is not legal
def f4(n: bytes) -> None: ...
a1 = f4 # E: Incompatible types in assignment (expression has type "Callable[[bytes], None]", variable has type "Callable[[bool], None]")
a2 = Job(f4) # E: Argument 1 to "Job" has incompatible type "Callable[[bytes], None]"; expected "Callable[[bool], None]"
# nor is this:
a4: Job[[int]]
a4 = Job(f3) # E: Argument 1 to "Job" has incompatible type "Callable[[bool], None]"; expected "Callable[[int], None]"
a4 = Job(f2)
a4 = Job(f1)
# just like this:
a3: Callable[[int], None]
a3 = f3 # E: Incompatible types in assignment (expression has type "Callable[[bool], None]", variable has type "Callable[[int], None]")
a3 = f2
a3 = f1
[builtins fixtures/tuple.pyi]
[case testGenericsInInferredParamspec]