Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
2 changes: 1 addition & 1 deletion mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -7464,7 +7464,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]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 9 additions & 9 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
16 changes: 8 additions & 8 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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: ...
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
12 changes: 6 additions & 6 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-python311.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]"

Expand Down
24 changes: 12 additions & 12 deletions test-data/unit/check-typevar-tuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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"
Expand All @@ -2194,15 +2194,15 @@ 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,
*args: Unpack[Keywords], # E: "Keywords" cannot be unpacked (must be tuple or TypeVarTuple)
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]

Expand Down Expand Up @@ -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]

Expand All @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/check-varargs.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
2 changes: 1 addition & 1 deletion test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/parse.test
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/semanal-types.test
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ MypyFile:1(
OverloadedFuncDef:2(
FuncDef:7(
f
def (*args: Any)
def (*Any)
VarArg(
Var(args))
Block:7(
Expand Down Expand Up @@ -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(
Expand Down