Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
22 changes: 19 additions & 3 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1883,6 +1883,17 @@ def are_trivial_parameters(param: Parameters | NormalizedCallableType) -> bool:
)


def is_trivial_star_params(param: Parameters | NormalizedCallableType) -> bool:
param_star = param.var_arg()
param_star2 = param.kw_arg()
return (
param_star is not None
and isinstance(get_proper_type(param_star.typ), AnyType)
and param_star2 is not None
and isinstance(get_proper_type(param_star2.typ), AnyType)
)


def is_trivial_suffix(param: Parameters | NormalizedCallableType) -> bool:
param_star = param.var_arg()
param_star2 = param.kw_arg()
Expand Down Expand Up @@ -1918,6 +1929,11 @@ def are_parameters_compatible(
if are_trivial_parameters(right) and not is_proper_subtype:
return True
trivial_suffix = is_trivial_suffix(right) and not is_proper_subtype
# Per the typing spec, a (*args: Any, **kwargs: Any) pair in the input signature
# is equivalent to "...", even if other parameters are declared between them.
# The star parameters then impose no constraints on the left callable, while
# any retained (non-star) parameters are still checked below.
trivial_star_params = is_trivial_star_params(right) and not is_proper_subtype

trivial_vararg_suffix = False
if (
Expand Down Expand Up @@ -1964,7 +1980,7 @@ def _incompatible(left_arg: FormalArgument | None, right_arg: FormalArgument | N
return not allow_partial_overlap and not trivial_suffix
return not is_compat(right_arg.typ, left_arg.typ)

if (
if not trivial_star_params and (
_incompatible(left_star, right_star)
and not trivial_vararg_suffix
or _incompatible(left_star2, right_star2)
Expand Down Expand Up @@ -1999,7 +2015,7 @@ def _incompatible(left_arg: FormalArgument | None, right_arg: FormalArgument | N
# arguments. Get all further positional args of left, and make sure
# they're more general than the corresponding member in right.
# TODO: handle suffix in UnpackType (i.e. *args: *Tuple[Ts, X, Y]).
if right_star is not None and not trivial_vararg_suffix:
if right_star is not None and not trivial_vararg_suffix and not trivial_star_params:
# Synthesize an anonymous formal argument for the right
right_by_position = right.try_synthesizing_arg_from_vararg(None)
assert right_by_position is not None
Expand All @@ -2026,7 +2042,7 @@ def _incompatible(left_arg: FormalArgument | None, right_arg: FormalArgument | N
# Phase 1d: Check kw args. Right has an infinite series of optional named
# arguments. Get all further named args of left, and make sure
# they're more general than the corresponding member in right.
if right_star2 is not None:
if right_star2 is not None and not trivial_star_params:
right_names = {name for name in right.arg_names if name is not None}
left_only_names = set()
for name, kind in zip(left.arg_names, left.arg_kinds):
Expand Down
40 changes: 40 additions & 0 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -4865,3 +4865,43 @@ from typing import Callable, TypeVar
S = TypeVar("S")
class C:
def __init_subclass__(cls: type[S], item: Callable[[S], None]) -> None: ...

[case testTrivialStarParamsInCallbackProtocol]
from typing import Any, Protocol

class P(Protocol):
def __call__(self, *args: Any, k: int, **kwargs: Any) -> None: ...

def cb1(*, k: int, extra: int = 0) -> None: ...
def cb2(x: int, *, k: int) -> None: ...
def cb3(*args: Any, k: int, **kwargs: Any) -> None: ...

p1: P = cb1
p2: P = cb2
p3: P = cb3
[builtins fixtures/primitives.pyi]
[case testTrivialStarParamsRetainedParamsChecked]
from typing import Any, Protocol

class P(Protocol):
def __call__(self, *args: Any, k: int, **kwargs: Any) -> None: ...

def missing_k(*, other: int = 0) -> None: ...
def wrong_k_type(*, k: str) -> None: ...

p1: P = missing_k # E: Incompatible types in assignment (expression has type "def missing_k(*, other: int = ...) -> None", variable has type "P") \
# N: "P.__call__" has type "def __call__(self, *args: Any, k: int, **kwargs: Any) -> None"
p2: P = wrong_k_type # E: Incompatible types in assignment (expression has type "def wrong_k_type(*, k: str) -> None", variable has type "P") \
# N: "P.__call__" has type "def __call__(self, *args: Any, k: int, **kwargs: Any) -> None"
[builtins fixtures/primitives.pyi]
[case testNonTrivialStarParamsStillChecked]
from typing import Any, Protocol

class P(Protocol):
def __call__(self, *args: str, k: int, **kwargs: Any) -> None: ...

def no_varargs(*, k: int) -> None: ...

p1: P = no_varargs # E: Incompatible types in assignment (expression has type "def no_varargs(*, k: int) -> None", variable has type "P") \
# N: "P.__call__" has type "def __call__(self, *args: str, k: int, **kwargs: Any) -> None"
[builtins fixtures/primitives.pyi]
Loading