Skip to content

Commit fc6f8b3

Browse files
committed
Convert Callable ... to Params[*args: Any, **kwargs: Any]
Ellipsis in Callable[..., ret] and bare Callable now produce Params[Param[None, Any, "*"], Param[None, Any, "**"]] instead of SpecialFormEllipsis, so all Callable param access consistently returns Params.
1 parent 50da25b commit fc6f8b3

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

tests/test_type_eval.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -756,11 +756,23 @@ def test_eval_getarg_callable_old():
756756

757757
t = Callable[..., str]
758758
args = eval_typing(GetArg[t, Callable, 0])
759-
assert args == SpecialFormEllipsis
759+
assert (
760+
args
761+
== Params[
762+
Param[Literal[None], Any, Literal["*"]],
763+
Param[Literal[None], Any, Literal["**"]],
764+
]
765+
)
760766

761767
t = Callable
762768
args = eval_typing(GetArg[t, Callable, 0])
763-
assert args == SpecialFormEllipsis
769+
assert (
770+
args
771+
== Params[
772+
Param[Literal[None], Any, Literal["*"]],
773+
Param[Literal[None], Any, Literal["**"]],
774+
]
775+
)
764776

765777
t = Callable
766778
args = eval_typing(GetArg[t, Callable, 1])
@@ -780,14 +792,26 @@ def test_eval_getarg_callable_01():
780792
args = eval_typing(GetArg[t, Callable, Literal[0]])
781793
assert args == Params[()]
782794

783-
# XXX: Is this what we want? Or should it be *args, **kwargs
795+
# ... becomes *args: Any, **kwargs: Any
784796
t = Callable[..., str]
785797
args = eval_typing(GetArg[t, Callable, Literal[0]])
786-
assert args == SpecialFormEllipsis
798+
assert (
799+
args
800+
== Params[
801+
Param[Literal[None], Any, Literal["*"]],
802+
Param[Literal[None], Any, Literal["**"]],
803+
]
804+
)
787805

788806
t = Callable
789807
args = eval_typing(GetArg[t, Callable, Literal[0]])
790-
assert args == SpecialFormEllipsis
808+
assert (
809+
args
810+
== Params[
811+
Param[Literal[None], Any, Literal["*"]],
812+
Param[Literal[None], Any, Literal["**"]],
813+
]
814+
)
791815

792816
t = Callable
793817
args = eval_typing(GetArg[t, Callable, Literal[1]])

typemap/type_eval/_eval_operators.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,19 @@ def __repr__(self):
495495
def _unwrap_params(param_types) -> list:
496496
"""Unwrap params into a list of Param types.
497497
498-
Accepts Params[...] (extended format), or a list/tuple of plain
499-
types (standard format, converted to positional-only Params).
498+
Accepts Params[...] (extended format), Ellipsis (any params),
499+
or a list/tuple of plain types (standard format, converted to
500+
positional-only Params).
500501
"""
501502
if typing.get_origin(param_types) is Params:
502503
return list(typing.get_args(param_types))
503504

505+
if param_types is ...:
506+
return [
507+
Param[typing.Literal[None], typing.Any, typing.Literal["*"]],
508+
Param[typing.Literal[None], typing.Any, typing.Literal["**"]],
509+
]
510+
504511
if isinstance(param_types, (list, tuple)):
505512
items = list(param_types)
506513
else:
@@ -890,18 +897,7 @@ def _fix_callable_args(base, args):
890897
if idx >= len(args):
891898
return args
892899
args = list(args)
893-
special = _fix_type(args[idx])
894-
if typing.get_origin(special) is Params:
895-
args[idx] = Params[
896-
*[
897-
(
898-
t
899-
if typing.get_origin(t) is Param
900-
else Param[typing.Literal[None], t]
901-
)
902-
for t in typing.get_args(special)
903-
]
904-
]
900+
args[idx] = Params[*_unwrap_params(args[idx])]
905901
return tuple(args)
906902

907903

@@ -1039,7 +1035,13 @@ def _get_defaults(base_head):
10391035
# Callable and tuple need to produce a SpecialFormEllipsis for arg
10401036
# 0 and 1, respectively.
10411037
if base_head is collections.abc.Callable:
1042-
return (SpecialFormEllipsis, typing.Any)
1038+
return (
1039+
Params[
1040+
Param[typing.Literal[None], typing.Any, typing.Literal["*"]],
1041+
Param[typing.Literal[None], typing.Any, typing.Literal["**"]],
1042+
],
1043+
typing.Any,
1044+
)
10431045
elif base_head is tuple:
10441046
return (typing.Any, SpecialFormEllipsis)
10451047

0 commit comments

Comments
 (0)