Skip to content

Commit 0243b11

Browse files
committed
Wrap extended callable params in Params type
Introduce Params[*Ps] as a wrapper for the parameter list in extended Callable types. Callable[Params[Param[...], ...], ret] replaces Callable[[Param[...], ...], ret], making it easy to distinguish extended callables from standard ones. GetArg on a Callable now always returns Params[...] for the first arg. Uses typing._ConcatenateGenericAlias internally so that Params survives the typing.Callable round-trip without monkey-patching.
1 parent aee3dd3 commit 0243b11

File tree

4 files changed

+110
-81
lines changed

4 files changed

+110
-81
lines changed

tests/test_type_dir.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ def test_type_members_func_1():
381381
assert (
382382
str(typ)
383383
== "\
384-
typing.Callable[[\
385-
typemap.typing.Param[typing.Literal['self'], tests.test_type_dir.Base[int], typing.Never], \
384+
typing.Callable[\
385+
typemap.typing.Params[typemap.typing.Param[typing.Literal['self'], tests.test_type_dir.Base[int], typing.Never], \
386386
typemap.typing.Param[typing.Literal['a'], int | None, typing.Never], \
387387
typemap.typing.Param[typing.Literal['b'], int, typing.Literal['keyword', \
388388
'default']]], \
@@ -403,7 +403,7 @@ def test_type_members_func_2():
403403
assert (
404404
str(typ)
405405
== "\
406-
classmethod[tests.test_type_dir.Base[int], tuple[typemap.typing.Param[typing.Literal['a'], int | None, typing.Never], typemap.typing.Param[typing.Literal['b'], ~K, typing.Never]], dict[str, int]]"
406+
classmethod[tests.test_type_dir.Base[int], typemap.typing.Params[typemap.typing.Param[typing.Literal['a'], int | None, typing.Never], typemap.typing.Param[typing.Literal['b'], ~K, typing.Never]], dict[str, int]]"
407407
)
408408

409409

@@ -422,7 +422,7 @@ def test_type_members_func_3():
422422
)
423423
assert (
424424
str(evaled)
425-
== "staticmethod[tuple[typemap.typing.Param[typing.Literal['a'], int | typing.Literal['gotcha!'] | Z | None, typing.Never], typemap.typing.Param[typing.Literal['b'], ~K, typing.Never]], dict[str, int | Z]]"
425+
== "staticmethod[typemap.typing.Params[typemap.typing.Param[typing.Literal['a'], int | typing.Literal['gotcha!'] | Z | None, typing.Never], typemap.typing.Param[typing.Literal['b'], ~K, typing.Never]], dict[str, int | Z]]"
426426
)
427427

428428

tests/test_type_eval.py

Lines changed: 62 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import pytest
2323

2424
from typemap.type_eval import _ensure_context, eval_typing
25-
from typemap.typing import _BoolLiteral
25+
from typemap.typing import _BoolLiteral, Params
2626

2727
from typemap_extensions import (
2828
Attrs,
@@ -236,11 +236,11 @@ def g(cls: type[C], x: int) -> int: ...
236236
def h(cls: type[Self], x: int) -> int: ...
237237

238238
d = eval_typing(GetMemberType[C, Literal["f"]])
239-
assert d == classmethod[C, tuple[Param[Literal["x"], int]], int]
239+
assert d == classmethod[C, Params[Param[Literal["x"], int]], int]
240240
d = eval_typing(GetMemberType[C, Literal["g"]])
241-
assert d == classmethod[C, tuple[Param[Literal["x"], int]], int]
241+
assert d == classmethod[C, Params[Param[Literal["x"], int]], int]
242242
d = eval_typing(GetMemberType[C, Literal["h"]])
243-
assert d == classmethod[Self, tuple[Param[Literal["x"], int]], int]
243+
assert d == classmethod[Self, Params[Param[Literal["x"], int]], int]
244244

245245

246246
def test_type_strings_1():
@@ -434,7 +434,7 @@ def f[TX](self, x: TX) -> OnlyIntToSet[TX]: ...
434434
assert (
435435
f
436436
== Callable[
437-
[Param[Literal["self"], C], Param[Literal["x"], Vs[0]]],
437+
Params[Param[Literal["self"], C], Param[Literal["x"], Vs[0]]],
438438
OnlyIntToSet[Vs[0]],
439439
]
440440
)
@@ -458,7 +458,7 @@ def f[T](self, x: T) -> OnlyIntToSet[T]: ...
458458
assert (
459459
f
460460
== Callable[
461-
[Param[Literal["self"], Self], Param[Literal["x"], Vs[0]]],
461+
Params[Param[Literal["self"], Self], Param[Literal["x"], Vs[0]]],
462462
OnlyIntToSet[Vs[0]],
463463
]
464464
)
@@ -487,7 +487,8 @@ def f(self, x): ...
487487
assert (
488488
mt.__args__[0]
489489
== Callable[
490-
[Param[Literal["self"], C], Param[Literal["x"], int]], set[int]
490+
Params[Param[Literal["self"], C], Param[Literal["x"], int]],
491+
set[int],
491492
]
492493
)
493494

@@ -500,7 +501,9 @@ def f(self, x): ...
500501
)
501502
assert (
502503
eval_typing(mt.__args__[1].__args__[1](int))
503-
== Callable[[Param[Literal["self"], C], Param[Literal["x"], int]], int]
504+
== Callable[
505+
Params[Param[Literal["self"], C], Param[Literal["x"], int]], int
506+
]
504507
)
505508

506509

@@ -524,10 +527,17 @@ class C(A):
524527
ft = m.__args__[1].__args__[1]
525528
with _ensure_context():
526529
assert (
527-
eval_typing(ft(A)) == Callable[[Param[Literal["self"], A]], Never]
530+
eval_typing(ft(A))
531+
== Callable[Params[Param[Literal["self"], A]], Never]
532+
)
533+
assert (
534+
eval_typing(ft(B))
535+
== Callable[Params[Param[Literal["self"], B]], int]
536+
)
537+
assert (
538+
eval_typing(ft(C))
539+
== Callable[Params[Param[Literal["self"], C]], str]
528540
)
529-
assert eval_typing(ft(B)) == Callable[[Param[Literal["self"], B]], int]
530-
assert eval_typing(ft(C)) == Callable[[Param[Literal["self"], C]], str]
531541

532542

533543
def test_getmember_06():
@@ -554,19 +564,22 @@ class C(A):
554564
assert (
555565
eval_typing(ft(A))
556566
== Callable[
557-
[Param[Literal["self"], A], Param[Literal["x"], Never]], None
567+
Params[Param[Literal["self"], A], Param[Literal["x"], Never]],
568+
None,
558569
]
559570
)
560571
assert (
561572
eval_typing(ft(B))
562573
== Callable[
563-
[Param[Literal["self"], B], Param[Literal["x"], int]], None
574+
Params[Param[Literal["self"], B], Param[Literal["x"], int]],
575+
None,
564576
]
565577
)
566578
assert (
567579
eval_typing(ft(C))
568580
== Callable[
569-
[Param[Literal["self"], C], Param[Literal["x"], str]], None
581+
Params[Param[Literal["self"], C], Param[Literal["x"], str]],
582+
None,
570583
]
571584
)
572585

@@ -591,9 +604,9 @@ class C(A):
591604

592605
ft = m.__args__[1].__args__[1]
593606
with _ensure_context():
594-
assert eval_typing(ft(A)) == classmethod[A, tuple[()], Never]
595-
assert eval_typing(ft(B)) == classmethod[B, tuple[()], int]
596-
assert eval_typing(ft(C)) == classmethod[C, tuple[()], str]
607+
assert eval_typing(ft(A)) == classmethod[A, Params[()], Never]
608+
assert eval_typing(ft(B)) == classmethod[B, Params[()], int]
609+
assert eval_typing(ft(C)) == classmethod[C, Params[()], str]
597610

598611

599612
def test_getmember_08():
@@ -620,15 +633,15 @@ class C(A):
620633
with _ensure_context():
621634
assert (
622635
eval_typing(ft(A))
623-
== classmethod[A, tuple[Param[Literal["x"], Never]], None]
636+
== classmethod[A, Params[Param[Literal["x"], Never]], None]
624637
)
625638
assert (
626639
eval_typing(ft(B))
627-
== classmethod[B, tuple[Param[Literal["x"], int]], None]
640+
== classmethod[B, Params[Param[Literal["x"], int]], None]
628641
)
629642
assert (
630643
eval_typing(ft(C))
631-
== classmethod[C, tuple[Param[Literal["x"], str]], None]
644+
== classmethod[C, Params[Param[Literal["x"], str]], None]
632645
)
633646

634647

@@ -663,11 +676,11 @@ class B(A):
663676
with _ensure_context():
664677
assert (
665678
eval_typing(ft(A))
666-
== Callable[[Param[Literal["self"], A]], tuple[()]]
679+
== Callable[Params[Param[Literal["self"], A]], tuple[()]]
667680
)
668681
assert (
669682
eval_typing(ft(B))
670-
== Callable[[Param[Literal["self"], B]], tuple[bool, str]]
683+
== Callable[Params[Param[Literal["self"], B]], tuple[bool, str]]
671684
)
672685

673686

@@ -701,8 +714,10 @@ class B(A):
701714

702715
ft = m.__args__[1].__args__[1]
703716
with _ensure_context():
704-
assert eval_typing(ft(A)) == classmethod[A, tuple[()], tuple[()]]
705-
assert eval_typing(ft(B)) == classmethod[B, tuple[()], tuple[bool, str]]
717+
assert eval_typing(ft(A)) == classmethod[A, Params[()], tuple[()]]
718+
assert (
719+
eval_typing(ft(B)) == classmethod[B, Params[()], tuple[bool, str]]
720+
)
706721

707722

708723
def test_getarg_never():
@@ -752,20 +767,15 @@ def test_eval_getarg_callable_old():
752767
def test_eval_getarg_callable_01():
753768
t = Callable[[int, str], str]
754769
args = eval_typing(GetArg[t, Callable, Literal[0]])
755-
assert (
756-
args
757-
== tuple[
758-
Param[Literal[None], int, Never], Param[Literal[None], str, Never]
759-
]
760-
)
770+
assert args == Params[Param[Literal[None], int], Param[Literal[None], str]]
761771

762772
t = Callable[int, str]
763773
args = eval_typing(GetArg[t, Callable, Literal[0]])
764-
assert args == tuple[Param[Literal[None], int, Never]]
774+
assert args == Params[Param[Literal[None], int]]
765775

766776
t = Callable[[], str]
767777
args = eval_typing(GetArg[t, Callable, Literal[0]])
768-
assert args == tuple[()]
778+
assert args == Params[()]
769779

770780
# XXX: Is this what we want? Or should it be *args, **kwargs
771781
t = Callable[..., str]
@@ -841,7 +851,7 @@ def f(self, x: int, /, y: int, *, z: int) -> int: ...
841851
t = eval_typing(GetArg[f, Callable, Literal[0]])
842852
assert (
843853
t
844-
== tuple[
854+
== Params[
845855
Param[Literal["self"], C, Literal["positional"]],
846856
Param[Literal["x"], int, Literal["positional"]],
847857
Param[Literal["y"], int],
@@ -855,7 +865,7 @@ def f(self, x: int, /, y: int, *, z: int) -> int: ...
855865
t = eval_typing(GetArg[f, Callable, Literal[0]])
856866
assert (
857867
t
858-
== tuple[
868+
== Params[
859869
Param[Literal["self"], Self, Literal["positional"]],
860870
Param[Literal["x"], int, Literal["positional"]],
861871
Param[Literal["y"], int],
@@ -878,7 +888,7 @@ def f(cls, x: int, /, y: int, *, z: int) -> int: ...
878888
t = eval_typing(GetArg[f, classmethod, Literal[1]])
879889
assert (
880890
t
881-
== tuple[
891+
== Params[
882892
Param[Literal["x"], int, Literal["positional"]],
883893
Param[Literal["y"], int],
884894
Param[Literal["z"], int, Literal["keyword"]],
@@ -892,7 +902,7 @@ def f(cls, x: int, /, y: int, *, z: int) -> int: ...
892902
t = eval_typing(GetArg[f, classmethod, Literal[1]])
893903
assert (
894904
t
895-
== tuple[
905+
== Params[
896906
Param[Literal["x"], int, Literal["positional"]],
897907
Param[Literal["y"], int],
898908
Param[Literal["z"], int, Literal["keyword"]],
@@ -912,7 +922,7 @@ def f(x: int, /, y: int, *, z: int) -> int: ...
912922
t = eval_typing(GetArg[f, staticmethod, Literal[0]])
913923
assert (
914924
t
915-
== tuple[
925+
== Params[
916926
Param[Literal["x"], int, Literal["positional"]],
917927
Param[Literal["y"], int],
918928
Param[Literal["z"], int, Literal["keyword"]],
@@ -925,7 +935,7 @@ def f(x: int, /, y: int, *, z: int) -> int: ...
925935
t = eval_typing(GetArg[f, staticmethod, Literal[0]])
926936
assert (
927937
t
928-
== tuple[
938+
== Params[
929939
Param[Literal["x"], int, Literal["positional"]],
930940
Param[Literal["y"], int],
931941
Param[Literal["z"], int, Literal["keyword"]],
@@ -942,7 +952,7 @@ class C:
942952

943953
f = eval_typing(GetMethodLike[IndirectProtocol[C], Literal["f"]])
944954
t = eval_typing(GetArg[f, Callable, Literal[0]])
945-
assert t == tuple[Param[Literal[None], int, Never],]
955+
assert t == Params[Param[Literal[None], int]]
946956
t = eval_typing(GetArg[f, Callable, Literal[1]])
947957
assert t is int
948958

@@ -1892,18 +1902,19 @@ def test_new_protocol_with_methods_02():
18921902
Member[
18931903
Literal["member_method"],
18941904
Callable[
1895-
[Param[Literal["self"], Self], Param[Literal["x"], int]], int
1905+
Params[Param[Literal["self"], Self], Param[Literal["x"], int]],
1906+
int,
18961907
],
18971908
Literal["ClassVar"],
18981909
],
18991910
Member[
19001911
Literal["class_method"],
1901-
classmethod[Self, tuple[Param[Literal["x"], int]], int],
1912+
classmethod[Self, Params[Param[Literal["x"], int]], int],
19021913
Literal["ClassVar"],
19031914
],
19041915
Member[
19051916
Literal["static_method"],
1906-
staticmethod[tuple[Param[Literal["x"], int]], int],
1917+
staticmethod[Params[Param[Literal["x"], int]], int],
19071918
Literal["ClassVar"],
19081919
],
19091920
]
@@ -1966,7 +1977,7 @@ def g(self) -> int: ... # omitted
19661977
Literal["__init_subclass__"],
19671978
classmethod[
19681979
A,
1969-
tuple[()],
1980+
Params[()],
19701981
UpdateClass[
19711982
Member[Literal["a2"], str],
19721983
Member[Literal["b1"], str],
@@ -1979,7 +1990,7 @@ def g(self) -> int: ... # omitted
19791990
],
19801991
Member[
19811992
Literal["f"],
1982-
Callable[[Param[Literal["self"], A]], int],
1993+
Callable[Params[Param[Literal["self"], A]], int],
19831994
Literal["ClassVar"],
19841995
object,
19851996
A,
@@ -2058,7 +2069,7 @@ def g(self) -> int: ... # omitted
20582069
Member[Literal["b2"], str, Never, Never, B],
20592070
Member[
20602071
Literal["f"],
2061-
Callable[[Param[Literal["self"], A]], int],
2072+
Callable[Params[Param[Literal["self"], A]], int],
20622073
Literal["ClassVar"],
20632074
object,
20642075
A,
@@ -2121,7 +2132,7 @@ def g(self) -> int: ... # omitted
21212132
Member[Literal["b"], set[str], Never, Never, B],
21222133
Member[
21232134
Literal["f"],
2124-
Callable[[Param[Literal["self"], A]], int],
2135+
Callable[Params[Param[Literal["self"], A]], int],
21252136
Literal["ClassVar"],
21262137
object,
21272138
A,
@@ -2178,7 +2189,7 @@ def g(self) -> int: ...
21782189
Literal["__init_subclass__"],
21792190
classmethod[
21802191
A,
2181-
tuple[()],
2192+
Params[()],
21822193
None,
21832194
],
21842195
Literal["ClassVar"],
@@ -2187,14 +2198,14 @@ def g(self) -> int: ...
21872198
],
21882199
Member[
21892200
Literal["f"],
2190-
Callable[[Param[Literal["self"], A]], int],
2201+
Callable[Params[Param[Literal["self"], A]], int],
21912202
Literal["ClassVar"],
21922203
object,
21932204
A,
21942205
],
21952206
Member[
21962207
Literal["g"],
2197-
Callable[[Param[Literal["self"], B]], int],
2208+
Callable[Params[Param[Literal["self"], B]], int],
21982209
Literal["ClassVar"],
21992210
object,
22002211
B,
@@ -2440,14 +2451,14 @@ def g(self) -> str: ...
24402451
Member[Literal["b"], str, Never, Never, B],
24412452
Member[
24422453
Literal["f"],
2443-
Callable[[Param[Literal["self"], Self]], int],
2454+
Callable[Params[Param[Literal["self"], Self]], int],
24442455
Literal["ClassVar"],
24452456
object,
24462457
B,
24472458
],
24482459
Member[
24492460
Literal["g"],
2450-
Callable[[Param[Literal["self"], Self]], str],
2461+
Callable[Params[Param[Literal["self"], Self]], str],
24512462
Literal["ClassVar"],
24522463
object,
24532464
B,

0 commit comments

Comments
 (0)