Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better support for variadic calls and indexing #16131

Merged
merged 6 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Prohibit dangerous case
  • Loading branch information
ilevkivskyi committed Sep 16, 2023
commit 28008649a08feb1b5842d9b550921a604790918d
21 changes: 21 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,27 @@ def check_callable_call(
callee.type_object().name, abstract_attributes, context
)

var_arg = callee.var_arg()
if var_arg and isinstance(var_arg.typ, UnpackType):
# It is hard to support multiple variadic unpacks (except for old-style *args: int),
# fail gracefully to avoid crashes later.
seen_unpack = False
for arg, arg_kind in zip(args, arg_kinds):
if arg_kind != ARG_STAR:
continue
arg_type = get_proper_type(self.accept(arg))
if not isinstance(arg_type, TupleType) or any(
isinstance(t, UnpackType) for t in arg_type.items
):
if seen_unpack:
self.msg.fail(
"Passing multiple variadic unpacks in a call is not supported",
context,
code=codes.CALL_ARG,
)
return AnyType(TypeOfAny.from_error), callee
seen_unpack = True

formal_to_actual = map_actuals_to_formals(
arg_kinds,
arg_names,
Expand Down
4 changes: 4 additions & 0 deletions test-data/unit/check-typevar-tuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ def args_to_tuple(*args: Unpack[Ts]) -> Tuple[Unpack[Ts]]:
reveal_type(args_to_tuple(1, *args)) # N: Revealed type is "Tuple[Literal[1]?, Unpack[Ts`-1]]"
reveal_type(args_to_tuple(*args, 'a')) # N: Revealed type is "Tuple[Unpack[Ts`-1], Literal['a']?]"
reveal_type(args_to_tuple(1, *args, 'a')) # N: Revealed type is "Tuple[Literal[1]?, Unpack[Ts`-1], Literal['a']?]"
args_to_tuple(*args, *args) # E: Passing multiple variadic unpacks in a call is not supported
ok = (1, 'a')
reveal_type(args_to_tuple(*ok, *ok)) # N: Revealed type is "Tuple[builtins.int, builtins.str, builtins.int, builtins.str]"
if int():
return args
else:
Expand All @@ -381,6 +384,7 @@ vt: Tuple[int, ...]
reveal_type(args_to_tuple(1, *vt)) # N: Revealed type is "Tuple[Literal[1]?, Unpack[builtins.tuple[builtins.int, ...]]]"
reveal_type(args_to_tuple(*vt, 'a')) # N: Revealed type is "Tuple[Unpack[builtins.tuple[builtins.int, ...]], Literal['a']?]"
reveal_type(args_to_tuple(1, *vt, 'a')) # N: Revealed type is "Tuple[Literal[1]?, Unpack[builtins.tuple[builtins.int, ...]], Literal['a']?]"
args_to_tuple(*vt, *vt) # E: Passing multiple variadic unpacks in a call is not supported
[builtins fixtures/tuple.pyi]

[case testTypeVarTuplePep646TypeVarStarArgs]
Expand Down