Skip to content
Closed
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
18 changes: 10 additions & 8 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1883,12 +1883,11 @@ def are_trivial_parameters(param: Parameters | NormalizedCallableType) -> bool:
)


def is_trivial_suffix(param: Parameters | NormalizedCallableType) -> bool:
def has_trivial_varargs(param: Parameters | NormalizedCallableType) -> bool:
param_star = param.var_arg()
param_star2 = param.kw_arg()
return (
param.arg_kinds[-2:] == [ARG_STAR, ARG_STAR2]
and param_star is not None
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)
Expand Down Expand Up @@ -1917,7 +1916,10 @@ def are_parameters_compatible(
# Treat "def _(*a: Any, **kw: Any) -> X" similarly to "Callable[..., X]"
if are_trivial_parameters(right) and not is_proper_subtype:
return True
trivial_suffix = is_trivial_suffix(right) and not is_proper_subtype
# Any-typed *args and **kwargs behave like an ellipsis even when explicit
# keyword-only arguments appear between them. Those arguments are still
# checked below.
trivial_varargs = has_trivial_varargs(right) and not is_proper_subtype

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

if (
Expand Down Expand Up @@ -1990,9 +1992,9 @@ def _incompatible(left_arg: FormalArgument | None, right_arg: FormalArgument | N
):
return False

if trivial_suffix:
# For trivial right suffix we *only* check that every non-star right argument
# has a valid match on the left.
if trivial_varargs:
# For trivial right varargs we *only* check that every non-star right
# argument has a valid match on the left.
return True

# Phase 1c: Check var args. Right has an infinite series of optional positional
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -4589,6 +4589,21 @@ t = Test(Mock())
reveal_type(t) # N: Revealed type is "__main__.Test[Any]"
[builtins fixtures/dict.pyi]

[case testProtocolCallableWithAnyVarargsAndExplicitKeyword]
from typing import Any, Protocol

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

def cb(*, formatter: int, extra: int = 0) -> None: ...

def missing_formatter(*, extra: int = 0) -> None: ...

x: Registerable = cb
y: Registerable = missing_formatter # E: Incompatible types in assignment (expression has type "def missing_formatter(*, extra: int = ...) -> None", variable has type "Registerable") \
# N: "Registerable.__call__" has type "def __call__(self, *args: Any, formatter: int, **kwargs: Any) -> None"
[builtins fixtures/dict.pyi]

[case testProtocolClassObjectDescriptor]
from typing import Any, Protocol, overload

Expand Down
Loading