From 8a4cd574e712be45a2c794e5c58ac8c4b23daaee Mon Sep 17 00:00:00 2001 From: A5rocks Date: Sun, 4 May 2025 00:55:01 +0900 Subject: [PATCH 1/4] Mark varargs as pos-only --- mypy/fastparse.py | 2 +- test-data/unit/check-classes.test | 2 +- test-data/unit/check-inference.test | 2 +- test-data/unit/check-modules.test | 6 ++--- test-data/unit/check-overloading.test | 18 +++++++------- .../unit/check-parameter-specification.test | 16 ++++++------- test-data/unit/check-protocols.test | 12 +++++----- test-data/unit/check-python311.test | 2 +- test-data/unit/check-typevar-tuple.test | 24 +++++++++---------- test-data/unit/check-varargs.test | 6 +++++ test-data/unit/fine-grained.test | 2 +- test-data/unit/parse.test | 6 ++--- test-data/unit/semanal-types.test | 4 ++-- 13 files changed, 54 insertions(+), 48 deletions(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index aed04c6f2eb9..dbe5996790c5 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -1101,7 +1101,7 @@ def transform_args( # *arg if args.vararg is not None: - new_args.append(self.make_argument(args.vararg, None, ARG_STAR, no_type_check)) + new_args.append(self.make_argument(args.vararg, None, ARG_STAR, no_type_check, True)) names.append(args.vararg) # keyword-only arguments with defaults diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test index 65a6a0c9c0a8..3df40d08f410 100644 --- a/test-data/unit/check-classes.test +++ b/test-data/unit/check-classes.test @@ -7420,7 +7420,7 @@ class A: class B(A): pass -reveal_type(A.__init_subclass__) # N: Revealed type is "def (*args: Any, **kwargs: Any) -> Any" +reveal_type(A.__init_subclass__) # N: Revealed type is "def (*Any, **kwargs: Any) -> Any" [builtins fixtures/object_with_init_subclass.pyi] [case testInitSubclassUnannotatedMulti] diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 42b5a05ab39a..72b79f510105 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -3847,7 +3847,7 @@ def Negate(count: int, /, metric: Metric[float]) -> float: ... def Combine(count: int, m1: Metric[T], m2: Metric[T], /, *more: Metric[T]) -> T: ... reveal_type(Negate) # N: Revealed type is "def (metric: __main__.Metric[builtins.float]) -> builtins.float" -reveal_type(Combine) # N: Revealed type is "def [T] (def () -> T`5, def () -> T`5, *more: def () -> T`5) -> T`5" +reveal_type(Combine) # N: Revealed type is "def [T] (def () -> T`5, def () -> T`5, *def () -> T`5) -> T`5" def m1() -> float: ... def m2() -> float: ... diff --git a/test-data/unit/check-modules.test b/test-data/unit/check-modules.test index 000dae86131d..da461f4d7813 100644 --- a/test-data/unit/check-modules.test +++ b/test-data/unit/check-modules.test @@ -3208,12 +3208,12 @@ class Bar(Foo): [out1] tmp/b.py:3: error: Signature of "frobnicate" incompatible with supertype "Foo" tmp/b.py:3: note: Superclass: -tmp/b.py:3: note: def frobnicate(self, x: str, *args: Any, **kwargs: Any) -> Any +tmp/b.py:3: note: def frobnicate(self, x: str, *Any, **kwargs: Any) -> Any tmp/b.py:3: note: Subclass: tmp/b.py:3: note: def frobnicate(self) -> None [out2] tmp/b.py:3: error: Signature of "frobnicate" incompatible with supertype "Foo" tmp/b.py:3: note: Superclass: -tmp/b.py:3: note: def frobnicate(self, x: str, *args: Any, **kwargs: Any) -> Any +tmp/b.py:3: note: def frobnicate(self, x: str, *Any, **kwargs: Any) -> Any tmp/b.py:3: note: Subclass: -tmp/b.py:3: note: def frobnicate(self, *args: int) -> None +tmp/b.py:3: note: def frobnicate(self, *int) -> None diff --git a/test-data/unit/check-overloading.test b/test-data/unit/check-overloading.test index 243568c54253..251ff4275e08 100644 --- a/test-data/unit/check-overloading.test +++ b/test-data/unit/check-overloading.test @@ -724,8 +724,8 @@ f(B(), B) f(B(), B, B) f(object()) # E: No overload variant of "f" matches argument type "object" \ # N: Possible overload variants: \ - # N: def f(x: A, *more: Any) -> A \ - # N: def f(x: B, *more: Any) -> A + # N: def f(x: A, *Any) -> A \ + # N: def f(x: B, *Any) -> A class A: pass class B: pass [builtins fixtures/list.pyi] @@ -742,12 +742,12 @@ f(A(), B()) f(A(), B(), B()) f(A(), A(), B()) # E: No overload variant of "f" matches argument types "A", "A", "B" \ # N: Possible overload variants: \ - # N: def f(x: A, *more: B) -> A \ - # N: def f(x: B, *more: A) -> A + # N: def f(x: A, *B) -> A \ + # N: def f(x: B, *A) -> A f(A(), B(), A()) # E: No overload variant of "f" matches argument types "A", "B", "A" \ # N: Possible overload variants: \ - # N: def f(x: A, *more: B) -> A \ - # N: def f(x: B, *more: A) -> A + # N: def f(x: A, *B) -> A \ + # N: def f(x: B, *A) -> A class A: pass class B: pass [builtins fixtures/list.pyi] @@ -1219,13 +1219,13 @@ def f(*x: str) -> str: pass f(*(1,))() # E: No overload variant of "f" matches argument type "Tuple[int]" \ # N: Possible overload variants: \ # N: def f(x: int, y: str) -> int \ - # N: def f(*x: str) -> str + # N: def f(*str) -> str f(*('',))() # E: "str" not callable f(*(1, ''))() # E: "int" not callable f(*(1, '', 1))() # E: No overload variant of "f" matches argument type "Tuple[int, str, int]" \ # N: Possible overload variants: \ # N: def f(x: int, y: str) -> int \ - # N: def f(*x: str) -> str + # N: def f(*str) -> str [builtins fixtures/tuple.pyi] [case testPreferExactSignatureMatchInOverload] @@ -2543,7 +2543,7 @@ foo(*y) # E: No overload variant of "foo" matches argument type "List[str]" \ # N: Possible overload variants: \ # N: def foo(x: int) -> A \ # N: def foo(x: int, y: int) -> B \ - # N: def foo(x: int, y: int, z: int, *args: int) -> C + # N: def foo(x: int, y: int, z: int, *int) -> C [builtins fixtures/list.pyi] [case testOverloadMultipleVarargDefinition] diff --git a/test-data/unit/check-parameter-specification.test b/test-data/unit/check-parameter-specification.test index 6f01b15e11f6..5b16e5359fdd 100644 --- a/test-data/unit/check-parameter-specification.test +++ b/test-data/unit/check-parameter-specification.test @@ -414,7 +414,7 @@ class C(Generic[P]): 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" +reveal_type(c.m) # N: Revealed type is "def (*Any, **kwargs: Any) -> builtins.int" c.m(4, 6, y='x') c = c @@ -603,18 +603,18 @@ def f5(x: X[int, int]) -> str: ... # E: Can only replace ParamSpec with a param 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" +reveal_type(add(bar)) # N: Revealed type is "def (builtins.str, x: builtins.int, *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" +reveal_type(remove(bar)) # N: Revealed type is "def (*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" +reveal_type(transform(bar)) # N: Revealed type is "def (builtins.str, *builtins.bool) -> builtins.bool" # CASE 4 def expects_int_first(x: Callable[Concatenate[int, P], int]) -> None: ... @@ -1776,9 +1776,9 @@ def bar(b: A[P]) -> A[Concatenate[int, P]]: return b # E: Incompatible return value type (got "A[P]", expected "A[[int, **P]]") \ # N: Following member(s) of "A[P]" have conflicts: \ # N: Expected: \ - # N: def foo(self, int, /, *args: P.args, **kwargs: P.kwargs) -> Any \ + # N: def foo(self, int, /, *P.args, **kwargs: P.kwargs) -> Any \ # N: Got: \ - # N: def foo(self, *args: P.args, **kwargs: P.kwargs) -> Any + # N: def foo(self, *P.args, **kwargs: P.kwargs) -> Any [builtins fixtures/paramspec.pyi] [case testParamSpecPrefixSubtypingValidNonStrict] @@ -1818,9 +1818,9 @@ def bar(b: B[P]) -> A[Concatenate[int, P]]: return b # E: Incompatible return value type (got "B[P]", expected "A[[int, **P]]") \ # N: Following member(s) of "B[P]" have conflicts: \ # N: Expected: \ - # N: def foo(self, a: int, int, /, *args: P.args, **kwargs: P.kwargs) -> Any \ + # N: def foo(self, a: int, int, /, *P.args, **kwargs: P.kwargs) -> Any \ # N: Got: \ - # N: def foo(self, a: int, b: int, *args: P.args, **kwargs: P.kwargs) -> Any + # N: def foo(self, a: int, b: int, *P.args, **kwargs: P.kwargs) -> Any [builtins fixtures/paramspec.pyi] [case testParamSpecDecoratorOverload] diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test index 34e3f3e88080..ab37b55643b1 100644 --- a/test-data/unit/check-protocols.test +++ b/test-data/unit/check-protocols.test @@ -2287,15 +2287,15 @@ func2(x) main:14: error: Argument 1 to "func1" has incompatible type "B"; expected "A" main:14: note: Following member(s) of "B" have conflicts: main:14: note: Expected: -main:14: note: def execute(self, statement: Any, *args: Any, **kwargs: Any) -> None +main:14: note: def execute(self, statement: Any, *Any, **kwargs: Any) -> None main:14: note: Got: -main:14: note: def execute(self, stmt: Any, *args: Any, **kwargs: Any) -> None +main:14: note: def execute(self, stmt: Any, *Any, **kwargs: Any) -> None main:15: error: Argument 1 to "func2" has incompatible type "B"; expected "Optional[A]" main:15: note: Following member(s) of "B" have conflicts: main:15: note: Expected: -main:15: note: def execute(self, statement: Any, *args: Any, **kwargs: Any) -> None +main:15: note: def execute(self, statement: Any, *Any, **kwargs: Any) -> None main:15: note: Got: -main:15: note: def execute(self, stmt: Any, *args: Any, **kwargs: Any) -> None +main:15: note: def execute(self, stmt: Any, *Any, **kwargs: Any) -> None [case testDontShowNotesForTupleAndIterableProtocol] from typing import Iterable, Sequence, Protocol, NamedTuple @@ -2375,11 +2375,11 @@ x: P = C() main:10: error: Incompatible types in assignment (expression has type "C", variable has type "P") main:10: note: Following member(s) of "C" have conflicts: main:10: note: Expected: -main:10: note: def meth(self, x: int, *args: str) -> None +main:10: note: def meth(self, x: int, *str) -> None main:10: note: Got: main:10: note: def meth(self) -> int main:10: note: Expected: -main:10: note: def other(self, *args: Any, hint: Optional[str] = ..., **kwargs: str) -> None +main:10: note: def other(self, *Any, hint: Optional[str] = ..., **kwargs: str) -> None main:10: note: Got: main:10: note: def other(self) -> int diff --git a/test-data/unit/check-python311.test b/test-data/unit/check-python311.test index c6d42660403e..41e1561cf2e5 100644 --- a/test-data/unit/check-python311.test +++ b/test-data/unit/check-python311.test @@ -101,7 +101,7 @@ class C(Generic[T, *Ts]): ci: C[*tuple[int, ...]] reveal_type(ci) # N: Revealed type is "__main__.C[Unpack[builtins.tuple[builtins.int, ...]]]" -reveal_type(ci.meth) # N: Revealed type is "def (*args: builtins.int) -> builtins.int" +reveal_type(ci.meth) # N: Revealed type is "def (*builtins.int) -> builtins.int" c3: C[str, str, str] reveal_type(c3) # N: Revealed type is "__main__.C[builtins.str, builtins.str, builtins.str]" diff --git a/test-data/unit/check-typevar-tuple.test b/test-data/unit/check-typevar-tuple.test index d364439f22e9..7bc698b01b86 100644 --- a/test-data/unit/check-typevar-tuple.test +++ b/test-data/unit/check-typevar-tuple.test @@ -448,11 +448,11 @@ def foo2(*args: Unpack[Tuple[bool, Unpack[Tuple[int, str]], bool]]) -> None: # It is hard to normalize callable types in definition, because there is deep relation between `FuncDef.type` # and `FuncDef.arguments`, therefore various typeops need to be sure to normalize Callable types before using them. -reveal_type(foo2) # N: Revealed type is "def (*args: Unpack[Tuple[builtins.bool, builtins.int, builtins.str, builtins.bool]])" +reveal_type(foo2) # N: Revealed type is "def (*Unpack[Tuple[builtins.bool, builtins.int, builtins.str, builtins.bool]])" class C: def foo2(self, *args: Unpack[Tuple[bool, Unpack[Tuple[int, str]], bool]]) -> None: ... -reveal_type(C().foo2) # N: Revealed type is "def (*args: Unpack[Tuple[builtins.bool, builtins.int, builtins.str, builtins.bool]])" +reveal_type(C().foo2) # N: Revealed type is "def (*Unpack[Tuple[builtins.bool, builtins.int, builtins.str, builtins.bool]])" [builtins fixtures/tuple.pyi] [case testTypeVarTuplePep646TypeVarStarArgsVariableLengthTuple] @@ -2015,7 +2015,7 @@ from typing_extensions import TypeVarTuple, Unpack Ts = TypeVarTuple("Ts") class B(Generic[Unpack[Ts]]): def __init__(self, x: Tuple[Unpack[Ts]], *args: Unpack[Ts]) -> None: ... -reveal_type(B) # N: Revealed type is "def [Ts] (x: Tuple[Unpack[Ts`1]], *args: Unpack[Ts`1]) -> __main__.B[Unpack[Ts`1]]" +reveal_type(B) # N: Revealed type is "def [Ts] (x: Tuple[Unpack[Ts`1]], *Unpack[Ts`1]) -> __main__.B[Unpack[Ts`1]]" T = TypeVar("T") S = TypeVar("S") @@ -2140,7 +2140,7 @@ get_items(b) # E: Argument 1 to "get_items" has incompatible type "Bad"; expect match(b) # E: Argument 1 to "match" has incompatible type "Bad"; expected "PC[Unpack[Tuple[Never, ...]]]" \ # N: Following member(s) of "Bad" have conflicts: \ # N: Expected: \ - # N: def meth(self, *args: Never) -> None \ + # N: def meth(self, *Never) -> None \ # N: Got: \ # N: def meth(self, *, named: int) -> None [builtins fixtures/tuple.pyi] @@ -2178,14 +2178,14 @@ class Keywords(TypedDict): Ints = Tuple[int, ...] def f(*args: Unpack[Ints], other: str = "no", **kwargs: Unpack[Keywords]) -> None: ... -reveal_type(f) # N: Revealed type is "def (*args: builtins.int, other: builtins.str =, **kwargs: Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])" +reveal_type(f) # N: Revealed type is "def (*builtins.int, other: builtins.str =, **kwargs: Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])" f(1, 2, a="a", b="b") # OK f(1, 2, 3) # E: Missing named argument "a" for "f" \ # E: Missing named argument "b" for "f" Ts = TypeVarTuple("Ts") def g(*args: Unpack[Ts], other: str = "no", **kwargs: Unpack[Keywords]) -> None: ... -reveal_type(g) # N: Revealed type is "def [Ts] (*args: Unpack[Ts`-1], other: builtins.str =, **kwargs: Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])" +reveal_type(g) # N: Revealed type is "def [Ts] (*Unpack[Ts`-1], other: builtins.str =, **kwargs: Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])" g(1, 2, a="a", b="b") # OK g(1, 2, 3) # E: Missing named argument "a" for "g" \ # E: Missing named argument "b" for "g" @@ -2194,7 +2194,7 @@ def bad( *args: Unpack[Keywords], # E: "Keywords" cannot be unpacked (must be tuple or TypeVarTuple) **kwargs: Unpack[Ints], # E: Unpack item in ** argument must be a TypedDict ) -> None: ... -reveal_type(bad) # N: Revealed type is "def (*args: Any, **kwargs: Any)" +reveal_type(bad) # N: Revealed type is "def (*Any, **kwargs: Any)" def bad2( one: int, @@ -2202,7 +2202,7 @@ def bad2( other: str = "no", **kwargs: Unpack[Ints], # E: Unpack item in ** argument must be a TypedDict ) -> None: ... -reveal_type(bad2) # N: Revealed type is "def (one: builtins.int, *args: Any, other: builtins.str =, **kwargs: Any)" +reveal_type(bad2) # N: Revealed type is "def (one: builtins.int, *Any, other: builtins.str =, **kwargs: Any)" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -2429,7 +2429,7 @@ Ts = TypeVarTuple("Ts") @cm def test(*args: Unpack[Ts]) -> Tuple[Unpack[Ts]]: ... -reveal_type(test) # N: Revealed type is "def [Ts] (*args: Unpack[Ts`-1]) -> __main__.CM[Tuple[Unpack[Ts`-1]]]" +reveal_type(test) # N: Revealed type is "def [Ts] (*Unpack[Ts`-1]) -> __main__.CM[Tuple[Unpack[Ts`-1]]]" reveal_type(test(1, 2, 3)) # N: Revealed type is "__main__.CM[Tuple[Literal[1]?, Literal[2]?, Literal[3]?]]" [builtins fixtures/tuple.pyi] @@ -2447,7 +2447,7 @@ Ts = TypeVarTuple("Ts") @cm # E: Argument 1 to "cm" has incompatible type "Callable[[VarArg(Unpack[Ts])], Tuple[Unpack[Ts]]]"; expected "Callable[[VarArg(Never)], List[Never]]" def test(*args: Unpack[Ts]) -> Tuple[Unpack[Ts]]: ... -reveal_type(test) # N: Revealed type is "def (*args: Never) -> __main__.CM[Never]" +reveal_type(test) # N: Revealed type is "def (*Never) -> __main__.CM[Never]" [builtins fixtures/tuple.pyi] [case testTypeVarTupleAgainstParamSpecActualPrefix] @@ -2465,7 +2465,7 @@ Ts = TypeVarTuple("Ts") @cm def test(x: T, *args: Unpack[Ts]) -> Tuple[T, Unpack[Ts]]: ... -reveal_type(test) # N: Revealed type is "def [T, Ts] (builtins.list[T`2], *args: Unpack[Ts`-2]) -> __main__.CM[Tuple[T`2, Unpack[Ts`-2]]]" +reveal_type(test) # N: Revealed type is "def [T, Ts] (builtins.list[T`2], *Unpack[Ts`-2]) -> __main__.CM[Tuple[T`2, Unpack[Ts`-2]]]" [builtins fixtures/tuple.pyi] [case testMixingTypeVarTupleAndParamSpec] @@ -2585,7 +2585,7 @@ class C(Generic[Unpack[Ts]]): def foo(self, *args: Unpack[Ts]) -> None: ... c: C[Unpack[tuple[int, ...]]] -reveal_type(c.foo) # N: Revealed type is "def (*args: builtins.int)" +reveal_type(c.foo) # N: Revealed type is "def (*builtins.int)" [builtins fixtures/tuple.pyi] [case testTypeVarTupleJoinInstanceTypeVar] diff --git a/test-data/unit/check-varargs.test b/test-data/unit/check-varargs.test index 65bbd8456d78..3debf1e7d68a 100644 --- a/test-data/unit/check-varargs.test +++ b/test-data/unit/check-varargs.test @@ -1114,3 +1114,9 @@ foo(key="yes", value="ok") bad: Callable[[*TD], None] # E: "TD" cannot be unpacked (must be tuple or TypeVarTuple) [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] + +[case testVarargsHasNoName] +def foo(*args: str) -> None: ... # N: "foo" defined here + +foo(args="hello") # E: Unexpected keyword argument "args" for "foo" +[builtins fixtures/dict.pyi] diff --git a/test-data/unit/fine-grained.test b/test-data/unit/fine-grained.test index df244b3135e9..f3c7b5504b88 100644 --- a/test-data/unit/fine-grained.test +++ b/test-data/unit/fine-grained.test @@ -10471,7 +10471,7 @@ reveal_type(x) [builtins fixtures/tuple.pyi] [out] == -a.py:3: note: Revealed type is "Union[def (x: builtins.int) -> builtins.int, def (*x: builtins.int) -> builtins.int]" +a.py:3: note: Revealed type is "Union[def (x: builtins.int) -> builtins.int, def (*builtins.int) -> builtins.int]" [case testErrorInReAddedModule] # flags: --disallow-untyped-defs --follow-imports=error diff --git a/test-data/unit/parse.test b/test-data/unit/parse.test index 943ca49081f1..2537a414d558 100644 --- a/test-data/unit/parse.test +++ b/test-data/unit/parse.test @@ -1922,7 +1922,7 @@ MypyFile:1( f Args( Var(x)) - def (x: str?, *a: int?) -> Any + def (x: str?, *int?) -> Any VarArg( Var(a)) Block:1( @@ -2766,7 +2766,7 @@ MypyFile:1( f Args( Var(x)) - def (x: X?, *y: Y?) -> Z? + def (x: X?, *Y?) -> Z? VarArg( Var(y)) Block:2( @@ -2781,7 +2781,7 @@ MypyFile:1( f Args( Var(x)) - def (x: X?, *y: Y?, **z: Z?) -> A? + def (x: X?, *Y?, **z: Z?) -> A? VarArg( Var(y)) DictVarArg( diff --git a/test-data/unit/semanal-types.test b/test-data/unit/semanal-types.test index 83c44738f055..7c8020adbb76 100644 --- a/test-data/unit/semanal-types.test +++ b/test-data/unit/semanal-types.test @@ -797,7 +797,7 @@ MypyFile:1( OverloadedFuncDef:2( FuncDef:7( f - def (*args: Any) + def (*Any) VarArg( Var(args)) Block:7( @@ -1043,7 +1043,7 @@ MypyFile:1( default( Var(y) StrExpr())) - def (*x: builtins.int, y: builtins.str =) -> Any + def (*builtins.int, y: builtins.str =) -> Any VarArg( Var(x)) Block:1( From d98e716820d23f30e68ca1717c5bec573912acdd Mon Sep 17 00:00:00 2001 From: A5rocks Date: Sun, 4 May 2025 07:40:48 +0900 Subject: [PATCH 2/4] Fixes for tests Given that this changes stubgen, it may make sense to have a more targeted fix. --- mypy/stubgen.py | 2 +- test-data/unit/pythoneval.test | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mypy/stubgen.py b/mypy/stubgen.py index ba0a3f9dade6..6255cfcb936e 100755 --- a/mypy/stubgen.py +++ b/mypy/stubgen.py @@ -556,7 +556,7 @@ def _get_func_args(self, o: FuncDef, ctx: FunctionContext) -> list[ArgSig]: if not isinstance(get_proper_type(annotated_type), AnyType): typename = self.print_annotation(annotated_type) - if actually_pos_only_args and arg_.pos_only: + if actually_pos_only_args and (arg_.pos_only and kind != ARG_STAR): pos_only_marker_position += 1 if kind.is_named() and not any(arg.name.startswith("*") for arg in args): diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index 0e0e2b1f344d..e5bfcd9618ed 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -1027,7 +1027,7 @@ with f('') as s: reveal_type(s) [out] _program.py:13: note: Revealed type is "def (x: builtins.int) -> contextlib._GeneratorContextManager[builtins.str, None, None]" -_program.py:14: note: Revealed type is "def (*x: builtins.str) -> contextlib._GeneratorContextManager[builtins.int, None, None]" +_program.py:14: note: Revealed type is "def (*builtins.str) -> contextlib._GeneratorContextManager[builtins.int, None, None]" _program.py:16: error: Argument 1 to "f" has incompatible type "str"; expected "int" _program.py:17: note: Revealed type is "builtins.str" From ed3550711443196bd8176cb6da65b366e5f649d4 Mon Sep 17 00:00:00 2001 From: A5rocks Date: Mon, 5 May 2025 11:16:07 +0900 Subject: [PATCH 3/4] Revert "Mark varargs as pos-only" This reverts commit 8a4cd574e712be45a2c794e5c58ac8c4b23daaee. --- mypy/fastparse.py | 2 +- mypy/stubgen.py | 2 +- test-data/unit/check-classes.test | 2 +- test-data/unit/check-inference.test | 2 +- test-data/unit/check-modules.test | 6 ++--- test-data/unit/check-overloading.test | 18 +++++++------- .../unit/check-parameter-specification.test | 16 ++++++------- test-data/unit/check-protocols.test | 12 +++++----- test-data/unit/check-python311.test | 2 +- test-data/unit/check-typevar-tuple.test | 24 +++++++++---------- test-data/unit/check-varargs.test | 6 ----- test-data/unit/fine-grained.test | 2 +- test-data/unit/parse.test | 6 ++--- test-data/unit/pythoneval.test | 2 +- test-data/unit/semanal-types.test | 4 ++-- 15 files changed, 50 insertions(+), 56 deletions(-) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index dbe5996790c5..aed04c6f2eb9 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -1101,7 +1101,7 @@ def transform_args( # *arg if args.vararg is not None: - new_args.append(self.make_argument(args.vararg, None, ARG_STAR, no_type_check, True)) + new_args.append(self.make_argument(args.vararg, None, ARG_STAR, no_type_check)) names.append(args.vararg) # keyword-only arguments with defaults diff --git a/mypy/stubgen.py b/mypy/stubgen.py index 6255cfcb936e..ba0a3f9dade6 100755 --- a/mypy/stubgen.py +++ b/mypy/stubgen.py @@ -556,7 +556,7 @@ def _get_func_args(self, o: FuncDef, ctx: FunctionContext) -> list[ArgSig]: if not isinstance(get_proper_type(annotated_type), AnyType): typename = self.print_annotation(annotated_type) - if actually_pos_only_args and (arg_.pos_only and kind != ARG_STAR): + if actually_pos_only_args and arg_.pos_only: pos_only_marker_position += 1 if kind.is_named() and not any(arg.name.startswith("*") for arg in args): diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test index 55658f3f0d8b..e0ea00aee361 100644 --- a/test-data/unit/check-classes.test +++ b/test-data/unit/check-classes.test @@ -7464,7 +7464,7 @@ class A: class B(A): pass -reveal_type(A.__init_subclass__) # N: Revealed type is "def (*Any, **kwargs: Any) -> Any" +reveal_type(A.__init_subclass__) # N: Revealed type is "def (*args: Any, **kwargs: Any) -> Any" [builtins fixtures/object_with_init_subclass.pyi] [case testInitSubclassUnannotatedMulti] diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 72b79f510105..42b5a05ab39a 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -3847,7 +3847,7 @@ def Negate(count: int, /, metric: Metric[float]) -> float: ... def Combine(count: int, m1: Metric[T], m2: Metric[T], /, *more: Metric[T]) -> T: ... reveal_type(Negate) # N: Revealed type is "def (metric: __main__.Metric[builtins.float]) -> builtins.float" -reveal_type(Combine) # N: Revealed type is "def [T] (def () -> T`5, def () -> T`5, *def () -> T`5) -> T`5" +reveal_type(Combine) # N: Revealed type is "def [T] (def () -> T`5, def () -> T`5, *more: def () -> T`5) -> T`5" def m1() -> float: ... def m2() -> float: ... diff --git a/test-data/unit/check-modules.test b/test-data/unit/check-modules.test index da461f4d7813..000dae86131d 100644 --- a/test-data/unit/check-modules.test +++ b/test-data/unit/check-modules.test @@ -3208,12 +3208,12 @@ class Bar(Foo): [out1] tmp/b.py:3: error: Signature of "frobnicate" incompatible with supertype "Foo" tmp/b.py:3: note: Superclass: -tmp/b.py:3: note: def frobnicate(self, x: str, *Any, **kwargs: Any) -> Any +tmp/b.py:3: note: def frobnicate(self, x: str, *args: Any, **kwargs: Any) -> Any tmp/b.py:3: note: Subclass: tmp/b.py:3: note: def frobnicate(self) -> None [out2] tmp/b.py:3: error: Signature of "frobnicate" incompatible with supertype "Foo" tmp/b.py:3: note: Superclass: -tmp/b.py:3: note: def frobnicate(self, x: str, *Any, **kwargs: Any) -> Any +tmp/b.py:3: note: def frobnicate(self, x: str, *args: Any, **kwargs: Any) -> Any tmp/b.py:3: note: Subclass: -tmp/b.py:3: note: def frobnicate(self, *int) -> None +tmp/b.py:3: note: def frobnicate(self, *args: int) -> None diff --git a/test-data/unit/check-overloading.test b/test-data/unit/check-overloading.test index 251ff4275e08..243568c54253 100644 --- a/test-data/unit/check-overloading.test +++ b/test-data/unit/check-overloading.test @@ -724,8 +724,8 @@ f(B(), B) f(B(), B, B) f(object()) # E: No overload variant of "f" matches argument type "object" \ # N: Possible overload variants: \ - # N: def f(x: A, *Any) -> A \ - # N: def f(x: B, *Any) -> A + # N: def f(x: A, *more: Any) -> A \ + # N: def f(x: B, *more: Any) -> A class A: pass class B: pass [builtins fixtures/list.pyi] @@ -742,12 +742,12 @@ f(A(), B()) f(A(), B(), B()) f(A(), A(), B()) # E: No overload variant of "f" matches argument types "A", "A", "B" \ # N: Possible overload variants: \ - # N: def f(x: A, *B) -> A \ - # N: def f(x: B, *A) -> A + # N: def f(x: A, *more: B) -> A \ + # N: def f(x: B, *more: A) -> A f(A(), B(), A()) # E: No overload variant of "f" matches argument types "A", "B", "A" \ # N: Possible overload variants: \ - # N: def f(x: A, *B) -> A \ - # N: def f(x: B, *A) -> A + # N: def f(x: A, *more: B) -> A \ + # N: def f(x: B, *more: A) -> A class A: pass class B: pass [builtins fixtures/list.pyi] @@ -1219,13 +1219,13 @@ def f(*x: str) -> str: pass f(*(1,))() # E: No overload variant of "f" matches argument type "Tuple[int]" \ # N: Possible overload variants: \ # N: def f(x: int, y: str) -> int \ - # N: def f(*str) -> str + # N: def f(*x: str) -> str f(*('',))() # E: "str" not callable f(*(1, ''))() # E: "int" not callable f(*(1, '', 1))() # E: No overload variant of "f" matches argument type "Tuple[int, str, int]" \ # N: Possible overload variants: \ # N: def f(x: int, y: str) -> int \ - # N: def f(*str) -> str + # N: def f(*x: str) -> str [builtins fixtures/tuple.pyi] [case testPreferExactSignatureMatchInOverload] @@ -2543,7 +2543,7 @@ foo(*y) # E: No overload variant of "foo" matches argument type "List[str]" \ # N: Possible overload variants: \ # N: def foo(x: int) -> A \ # N: def foo(x: int, y: int) -> B \ - # N: def foo(x: int, y: int, z: int, *int) -> C + # N: def foo(x: int, y: int, z: int, *args: int) -> C [builtins fixtures/list.pyi] [case testOverloadMultipleVarargDefinition] diff --git a/test-data/unit/check-parameter-specification.test b/test-data/unit/check-parameter-specification.test index 5b16e5359fdd..6f01b15e11f6 100644 --- a/test-data/unit/check-parameter-specification.test +++ b/test-data/unit/check-parameter-specification.test @@ -414,7 +414,7 @@ class C(Generic[P]): c: C[Any] reveal_type(c) # N: Revealed type is "__main__.C[Any]" -reveal_type(c.m) # N: Revealed type is "def (*Any, **kwargs: Any) -> builtins.int" +reveal_type(c.m) # N: Revealed type is "def (*args: Any, **kwargs: Any) -> builtins.int" c.m(4, 6, y='x') c = c @@ -603,18 +603,18 @@ def f5(x: X[int, int]) -> str: ... # E: Can only replace ParamSpec with a param 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, *builtins.bool) -> builtins.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 (*builtins.bool) -> builtins.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, *builtins.bool) -> builtins.bool" +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: ... @@ -1776,9 +1776,9 @@ def bar(b: A[P]) -> A[Concatenate[int, P]]: return b # E: Incompatible return value type (got "A[P]", expected "A[[int, **P]]") \ # N: Following member(s) of "A[P]" have conflicts: \ # N: Expected: \ - # N: def foo(self, int, /, *P.args, **kwargs: P.kwargs) -> Any \ + # N: def foo(self, int, /, *args: P.args, **kwargs: P.kwargs) -> Any \ # N: Got: \ - # N: def foo(self, *P.args, **kwargs: P.kwargs) -> Any + # N: def foo(self, *args: P.args, **kwargs: P.kwargs) -> Any [builtins fixtures/paramspec.pyi] [case testParamSpecPrefixSubtypingValidNonStrict] @@ -1818,9 +1818,9 @@ def bar(b: B[P]) -> A[Concatenate[int, P]]: return b # E: Incompatible return value type (got "B[P]", expected "A[[int, **P]]") \ # N: Following member(s) of "B[P]" have conflicts: \ # N: Expected: \ - # N: def foo(self, a: int, int, /, *P.args, **kwargs: P.kwargs) -> Any \ + # N: def foo(self, a: int, int, /, *args: P.args, **kwargs: P.kwargs) -> Any \ # N: Got: \ - # N: def foo(self, a: int, b: int, *P.args, **kwargs: P.kwargs) -> Any + # N: def foo(self, a: int, b: int, *args: P.args, **kwargs: P.kwargs) -> Any [builtins fixtures/paramspec.pyi] [case testParamSpecDecoratorOverload] diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test index ab37b55643b1..34e3f3e88080 100644 --- a/test-data/unit/check-protocols.test +++ b/test-data/unit/check-protocols.test @@ -2287,15 +2287,15 @@ func2(x) main:14: error: Argument 1 to "func1" has incompatible type "B"; expected "A" main:14: note: Following member(s) of "B" have conflicts: main:14: note: Expected: -main:14: note: def execute(self, statement: Any, *Any, **kwargs: Any) -> None +main:14: note: def execute(self, statement: Any, *args: Any, **kwargs: Any) -> None main:14: note: Got: -main:14: note: def execute(self, stmt: Any, *Any, **kwargs: Any) -> None +main:14: note: def execute(self, stmt: Any, *args: Any, **kwargs: Any) -> None main:15: error: Argument 1 to "func2" has incompatible type "B"; expected "Optional[A]" main:15: note: Following member(s) of "B" have conflicts: main:15: note: Expected: -main:15: note: def execute(self, statement: Any, *Any, **kwargs: Any) -> None +main:15: note: def execute(self, statement: Any, *args: Any, **kwargs: Any) -> None main:15: note: Got: -main:15: note: def execute(self, stmt: Any, *Any, **kwargs: Any) -> None +main:15: note: def execute(self, stmt: Any, *args: Any, **kwargs: Any) -> None [case testDontShowNotesForTupleAndIterableProtocol] from typing import Iterable, Sequence, Protocol, NamedTuple @@ -2375,11 +2375,11 @@ x: P = C() main:10: error: Incompatible types in assignment (expression has type "C", variable has type "P") main:10: note: Following member(s) of "C" have conflicts: main:10: note: Expected: -main:10: note: def meth(self, x: int, *str) -> None +main:10: note: def meth(self, x: int, *args: str) -> None main:10: note: Got: main:10: note: def meth(self) -> int main:10: note: Expected: -main:10: note: def other(self, *Any, hint: Optional[str] = ..., **kwargs: str) -> None +main:10: note: def other(self, *args: Any, hint: Optional[str] = ..., **kwargs: str) -> None main:10: note: Got: main:10: note: def other(self) -> int diff --git a/test-data/unit/check-python311.test b/test-data/unit/check-python311.test index 41e1561cf2e5..c6d42660403e 100644 --- a/test-data/unit/check-python311.test +++ b/test-data/unit/check-python311.test @@ -101,7 +101,7 @@ class C(Generic[T, *Ts]): ci: C[*tuple[int, ...]] reveal_type(ci) # N: Revealed type is "__main__.C[Unpack[builtins.tuple[builtins.int, ...]]]" -reveal_type(ci.meth) # N: Revealed type is "def (*builtins.int) -> builtins.int" +reveal_type(ci.meth) # N: Revealed type is "def (*args: builtins.int) -> builtins.int" c3: C[str, str, str] reveal_type(c3) # N: Revealed type is "__main__.C[builtins.str, builtins.str, builtins.str]" diff --git a/test-data/unit/check-typevar-tuple.test b/test-data/unit/check-typevar-tuple.test index 7bc698b01b86..d364439f22e9 100644 --- a/test-data/unit/check-typevar-tuple.test +++ b/test-data/unit/check-typevar-tuple.test @@ -448,11 +448,11 @@ def foo2(*args: Unpack[Tuple[bool, Unpack[Tuple[int, str]], bool]]) -> None: # It is hard to normalize callable types in definition, because there is deep relation between `FuncDef.type` # and `FuncDef.arguments`, therefore various typeops need to be sure to normalize Callable types before using them. -reveal_type(foo2) # N: Revealed type is "def (*Unpack[Tuple[builtins.bool, builtins.int, builtins.str, builtins.bool]])" +reveal_type(foo2) # N: Revealed type is "def (*args: Unpack[Tuple[builtins.bool, builtins.int, builtins.str, builtins.bool]])" class C: def foo2(self, *args: Unpack[Tuple[bool, Unpack[Tuple[int, str]], bool]]) -> None: ... -reveal_type(C().foo2) # N: Revealed type is "def (*Unpack[Tuple[builtins.bool, builtins.int, builtins.str, builtins.bool]])" +reveal_type(C().foo2) # N: Revealed type is "def (*args: Unpack[Tuple[builtins.bool, builtins.int, builtins.str, builtins.bool]])" [builtins fixtures/tuple.pyi] [case testTypeVarTuplePep646TypeVarStarArgsVariableLengthTuple] @@ -2015,7 +2015,7 @@ from typing_extensions import TypeVarTuple, Unpack Ts = TypeVarTuple("Ts") class B(Generic[Unpack[Ts]]): def __init__(self, x: Tuple[Unpack[Ts]], *args: Unpack[Ts]) -> None: ... -reveal_type(B) # N: Revealed type is "def [Ts] (x: Tuple[Unpack[Ts`1]], *Unpack[Ts`1]) -> __main__.B[Unpack[Ts`1]]" +reveal_type(B) # N: Revealed type is "def [Ts] (x: Tuple[Unpack[Ts`1]], *args: Unpack[Ts`1]) -> __main__.B[Unpack[Ts`1]]" T = TypeVar("T") S = TypeVar("S") @@ -2140,7 +2140,7 @@ get_items(b) # E: Argument 1 to "get_items" has incompatible type "Bad"; expect match(b) # E: Argument 1 to "match" has incompatible type "Bad"; expected "PC[Unpack[Tuple[Never, ...]]]" \ # N: Following member(s) of "Bad" have conflicts: \ # N: Expected: \ - # N: def meth(self, *Never) -> None \ + # N: def meth(self, *args: Never) -> None \ # N: Got: \ # N: def meth(self, *, named: int) -> None [builtins fixtures/tuple.pyi] @@ -2178,14 +2178,14 @@ class Keywords(TypedDict): Ints = Tuple[int, ...] def f(*args: Unpack[Ints], other: str = "no", **kwargs: Unpack[Keywords]) -> None: ... -reveal_type(f) # N: Revealed type is "def (*builtins.int, other: builtins.str =, **kwargs: Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])" +reveal_type(f) # N: Revealed type is "def (*args: builtins.int, other: builtins.str =, **kwargs: Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])" f(1, 2, a="a", b="b") # OK f(1, 2, 3) # E: Missing named argument "a" for "f" \ # E: Missing named argument "b" for "f" Ts = TypeVarTuple("Ts") def g(*args: Unpack[Ts], other: str = "no", **kwargs: Unpack[Keywords]) -> None: ... -reveal_type(g) # N: Revealed type is "def [Ts] (*Unpack[Ts`-1], other: builtins.str =, **kwargs: Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])" +reveal_type(g) # N: Revealed type is "def [Ts] (*args: Unpack[Ts`-1], other: builtins.str =, **kwargs: Unpack[TypedDict('__main__.Keywords', {'a': builtins.str, 'b': builtins.str})])" g(1, 2, a="a", b="b") # OK g(1, 2, 3) # E: Missing named argument "a" for "g" \ # E: Missing named argument "b" for "g" @@ -2194,7 +2194,7 @@ def bad( *args: Unpack[Keywords], # E: "Keywords" cannot be unpacked (must be tuple or TypeVarTuple) **kwargs: Unpack[Ints], # E: Unpack item in ** argument must be a TypedDict ) -> None: ... -reveal_type(bad) # N: Revealed type is "def (*Any, **kwargs: Any)" +reveal_type(bad) # N: Revealed type is "def (*args: Any, **kwargs: Any)" def bad2( one: int, @@ -2202,7 +2202,7 @@ def bad2( other: str = "no", **kwargs: Unpack[Ints], # E: Unpack item in ** argument must be a TypedDict ) -> None: ... -reveal_type(bad2) # N: Revealed type is "def (one: builtins.int, *Any, other: builtins.str =, **kwargs: Any)" +reveal_type(bad2) # N: Revealed type is "def (one: builtins.int, *args: Any, other: builtins.str =, **kwargs: Any)" [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] @@ -2429,7 +2429,7 @@ Ts = TypeVarTuple("Ts") @cm def test(*args: Unpack[Ts]) -> Tuple[Unpack[Ts]]: ... -reveal_type(test) # N: Revealed type is "def [Ts] (*Unpack[Ts`-1]) -> __main__.CM[Tuple[Unpack[Ts`-1]]]" +reveal_type(test) # N: Revealed type is "def [Ts] (*args: Unpack[Ts`-1]) -> __main__.CM[Tuple[Unpack[Ts`-1]]]" reveal_type(test(1, 2, 3)) # N: Revealed type is "__main__.CM[Tuple[Literal[1]?, Literal[2]?, Literal[3]?]]" [builtins fixtures/tuple.pyi] @@ -2447,7 +2447,7 @@ Ts = TypeVarTuple("Ts") @cm # E: Argument 1 to "cm" has incompatible type "Callable[[VarArg(Unpack[Ts])], Tuple[Unpack[Ts]]]"; expected "Callable[[VarArg(Never)], List[Never]]" def test(*args: Unpack[Ts]) -> Tuple[Unpack[Ts]]: ... -reveal_type(test) # N: Revealed type is "def (*Never) -> __main__.CM[Never]" +reveal_type(test) # N: Revealed type is "def (*args: Never) -> __main__.CM[Never]" [builtins fixtures/tuple.pyi] [case testTypeVarTupleAgainstParamSpecActualPrefix] @@ -2465,7 +2465,7 @@ Ts = TypeVarTuple("Ts") @cm def test(x: T, *args: Unpack[Ts]) -> Tuple[T, Unpack[Ts]]: ... -reveal_type(test) # N: Revealed type is "def [T, Ts] (builtins.list[T`2], *Unpack[Ts`-2]) -> __main__.CM[Tuple[T`2, Unpack[Ts`-2]]]" +reveal_type(test) # N: Revealed type is "def [T, Ts] (builtins.list[T`2], *args: Unpack[Ts`-2]) -> __main__.CM[Tuple[T`2, Unpack[Ts`-2]]]" [builtins fixtures/tuple.pyi] [case testMixingTypeVarTupleAndParamSpec] @@ -2585,7 +2585,7 @@ class C(Generic[Unpack[Ts]]): def foo(self, *args: Unpack[Ts]) -> None: ... c: C[Unpack[tuple[int, ...]]] -reveal_type(c.foo) # N: Revealed type is "def (*builtins.int)" +reveal_type(c.foo) # N: Revealed type is "def (*args: builtins.int)" [builtins fixtures/tuple.pyi] [case testTypeVarTupleJoinInstanceTypeVar] diff --git a/test-data/unit/check-varargs.test b/test-data/unit/check-varargs.test index 3debf1e7d68a..65bbd8456d78 100644 --- a/test-data/unit/check-varargs.test +++ b/test-data/unit/check-varargs.test @@ -1114,9 +1114,3 @@ foo(key="yes", value="ok") bad: Callable[[*TD], None] # E: "TD" cannot be unpacked (must be tuple or TypeVarTuple) [builtins fixtures/dict.pyi] [typing fixtures/typing-typeddict.pyi] - -[case testVarargsHasNoName] -def foo(*args: str) -> None: ... # N: "foo" defined here - -foo(args="hello") # E: Unexpected keyword argument "args" for "foo" -[builtins fixtures/dict.pyi] diff --git a/test-data/unit/fine-grained.test b/test-data/unit/fine-grained.test index f3c7b5504b88..df244b3135e9 100644 --- a/test-data/unit/fine-grained.test +++ b/test-data/unit/fine-grained.test @@ -10471,7 +10471,7 @@ reveal_type(x) [builtins fixtures/tuple.pyi] [out] == -a.py:3: note: Revealed type is "Union[def (x: builtins.int) -> builtins.int, def (*builtins.int) -> builtins.int]" +a.py:3: note: Revealed type is "Union[def (x: builtins.int) -> builtins.int, def (*x: builtins.int) -> builtins.int]" [case testErrorInReAddedModule] # flags: --disallow-untyped-defs --follow-imports=error diff --git a/test-data/unit/parse.test b/test-data/unit/parse.test index 2537a414d558..943ca49081f1 100644 --- a/test-data/unit/parse.test +++ b/test-data/unit/parse.test @@ -1922,7 +1922,7 @@ MypyFile:1( f Args( Var(x)) - def (x: str?, *int?) -> Any + def (x: str?, *a: int?) -> Any VarArg( Var(a)) Block:1( @@ -2766,7 +2766,7 @@ MypyFile:1( f Args( Var(x)) - def (x: X?, *Y?) -> Z? + def (x: X?, *y: Y?) -> Z? VarArg( Var(y)) Block:2( @@ -2781,7 +2781,7 @@ MypyFile:1( f Args( Var(x)) - def (x: X?, *Y?, **z: Z?) -> A? + def (x: X?, *y: Y?, **z: Z?) -> A? VarArg( Var(y)) DictVarArg( diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index e5bfcd9618ed..0e0e2b1f344d 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -1027,7 +1027,7 @@ with f('') as s: reveal_type(s) [out] _program.py:13: note: Revealed type is "def (x: builtins.int) -> contextlib._GeneratorContextManager[builtins.str, None, None]" -_program.py:14: note: Revealed type is "def (*builtins.str) -> contextlib._GeneratorContextManager[builtins.int, None, None]" +_program.py:14: note: Revealed type is "def (*x: builtins.str) -> contextlib._GeneratorContextManager[builtins.int, None, None]" _program.py:16: error: Argument 1 to "f" has incompatible type "str"; expected "int" _program.py:17: note: Revealed type is "builtins.str" diff --git a/test-data/unit/semanal-types.test b/test-data/unit/semanal-types.test index 7c8020adbb76..83c44738f055 100644 --- a/test-data/unit/semanal-types.test +++ b/test-data/unit/semanal-types.test @@ -797,7 +797,7 @@ MypyFile:1( OverloadedFuncDef:2( FuncDef:7( f - def (*Any) + def (*args: Any) VarArg( Var(args)) Block:7( @@ -1043,7 +1043,7 @@ MypyFile:1( default( Var(y) StrExpr())) - def (*builtins.int, y: builtins.str =) -> Any + def (*x: builtins.int, y: builtins.str =) -> Any VarArg( Var(x)) Block:1( From 5452fd2e11faf7ff949ef8fefa95f5ccbfd2d362 Mon Sep 17 00:00:00 2001 From: A5rocks Date: Mon, 5 May 2025 11:20:38 +0900 Subject: [PATCH 4/4] Use a different approach Co-authored-by: Anthony Sottile --- mypy/argmap.py | 2 +- test-data/unit/check-varargs.test | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/mypy/argmap.py b/mypy/argmap.py index a1c4ef72ea40..28fad1f093dd 100644 --- a/mypy/argmap.py +++ b/mypy/argmap.py @@ -78,7 +78,7 @@ def map_actuals_to_formals( elif actual_kind.is_named(): assert actual_names is not None, "Internal error: named kinds without names given" name = actual_names[ai] - if name in formal_names: + if name in formal_names and formal_kinds[formal_names.index(name)] != nodes.ARG_STAR: formal_to_actual[formal_names.index(name)].append(ai) elif nodes.ARG_STAR2 in formal_kinds: formal_to_actual[formal_kinds.index(nodes.ARG_STAR2)].append(ai) diff --git a/test-data/unit/check-varargs.test b/test-data/unit/check-varargs.test index 65bbd8456d78..c59f07e92a4e 100644 --- a/test-data/unit/check-varargs.test +++ b/test-data/unit/check-varargs.test @@ -145,6 +145,14 @@ f(*it1, 1, *it2, 2) # E: Argument 3 to "f" has incompatible type "*Tuple[str]"; f(*it1, '') # E: Argument 2 to "f" has incompatible type "str"; expected "int" [builtins fixtures/for.pyi] +[case testCallVarArgsWithMatchingNamedArgument] +def foo(*args: int) -> None: ... # N: "foo" defined here +foo(args=1) # E: Unexpected keyword argument "args" for "foo" + +def bar(*args: int, **kwargs: str) -> None: ... +bar(args=1) # E: Argument "args" to "bar" has incompatible type "int"; expected "str" +[builtins fixtures/for.pyi] + -- Calling varargs function + type inference -- -----------------------------------------