Skip to content

Commit db752bb

Browse files
A5rocksasottile
andauthored
Mark varargs as pos-only (#19022)
Fixes #19019. This PR marks `*args` as positional only, as you cannot pass the argument by its name. --------- Co-authored-by: Anthony Sottile <[email protected]>
1 parent e7405c9 commit db752bb

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

mypy/argmap.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def map_actuals_to_formals(
7878
elif actual_kind.is_named():
7979
assert actual_names is not None, "Internal error: named kinds without names given"
8080
name = actual_names[ai]
81-
if name in formal_names:
81+
if name in formal_names and formal_kinds[formal_names.index(name)] != nodes.ARG_STAR:
8282
formal_to_actual[formal_names.index(name)].append(ai)
8383
elif nodes.ARG_STAR2 in formal_kinds:
8484
formal_to_actual[formal_kinds.index(nodes.ARG_STAR2)].append(ai)

test-data/unit/check-varargs.test

+8
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ f(*it1, 1, *it2, 2) # E: Argument 3 to "f" has incompatible type "*Tuple[str]";
145145
f(*it1, '') # E: Argument 2 to "f" has incompatible type "str"; expected "int"
146146
[builtins fixtures/for.pyi]
147147

148+
[case testCallVarArgsWithMatchingNamedArgument]
149+
def foo(*args: int) -> None: ... # N: "foo" defined here
150+
foo(args=1) # E: Unexpected keyword argument "args" for "foo"
151+
152+
def bar(*args: int, **kwargs: str) -> None: ...
153+
bar(args=1) # E: Argument "args" to "bar" has incompatible type "int"; expected "str"
154+
[builtins fixtures/for.pyi]
155+
148156

149157
-- Calling varargs function + type inference
150158
-- -----------------------------------------

0 commit comments

Comments
 (0)