Skip to content

When selecting an overload item for constraint template matching, treat any ParamSpec in the template as free #19487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,7 @@ def find_matching_overload_item(overloaded: Overloaded, template: CallableType)
is_compat=mypy.subtypes.is_subtype,
is_proper_subtype=False,
ignore_return=True,
map_template_paramspec=True,
):
return item
# Fall back to the first item if we can't find a match. This is totally arbitrary --
Expand Down
9 changes: 6 additions & 3 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,7 @@ def is_callable_compatible(
check_args_covariantly: bool = False,
allow_partial_overlap: bool = False,
strict_concatenate: bool = False,
map_template_paramspec: bool = False,
) -> bool:
"""Is the left compatible with the right, using the provided compatibility check?

Expand Down Expand Up @@ -1717,6 +1718,7 @@ def g(x: int) -> int: ...
ignore_pos_arg_names=ignore_pos_arg_names,
allow_partial_overlap=allow_partial_overlap,
strict_concatenate_check=strict_concatenate_check,
template_has_paramspec=map_template_paramspec and right.param_spec() is not None,
)


Expand Down Expand Up @@ -1753,6 +1755,7 @@ def are_parameters_compatible(
ignore_pos_arg_names: bool = False,
allow_partial_overlap: bool = False,
strict_concatenate_check: bool = False,
template_has_paramspec: bool = False,
) -> bool:
"""Helper function for is_callable_compatible, used for Parameter compatibility"""
if right.is_ellipsis_args and not is_proper_subtype:
Expand Down Expand Up @@ -1817,7 +1820,7 @@ def _incompatible(left_arg: FormalArgument | None, right_arg: FormalArgument | N
_incompatible(left_star, right_star)
and not trivial_vararg_suffix
or _incompatible(left_star2, right_star2)
):
) and not template_has_paramspec:
return False

# Phase 1b: Check non-star args: for every arg right can accept, left must
Expand Down Expand Up @@ -1848,7 +1851,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 template_has_paramspec:
# 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 @@ -1875,7 +1878,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 template_has_paramspec:
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
23 changes: 23 additions & 0 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -2603,3 +2603,26 @@ def run3(predicate: Callable[Concatenate[int, str, _P], None], *args: _P.args, *
# E: Argument 1 has incompatible type "*tuple[Union[int, str], ...]"; expected "str" \
# E: Argument 1 has incompatible type "*tuple[Union[int, str], ...]"; expected "_P.args"
[builtins fixtures/paramspec.pyi]

[case testParamSpecOverloadProtocol]
from typing import ParamSpec, Protocol, TypeVar, overload

_A_contra = TypeVar("_A_contra", bound=str, contravariant=True)
_P = ParamSpec("_P")

class Callback(Protocol[_A_contra, _P]):
def method(self, a: _A_contra, *args: _P.args, **kwargs: _P.kwargs) -> None: ...

class Impl:
@overload
def method(self, a: int, b: str) -> None: ...
@overload
def method(self, a: str, b: int) -> None: ...
def method(self, a, b) -> None: ...

def accepts_callback(cb: Callback[str, _P], *args: _P.args, **kwargs: _P.kwargs) -> int:
return 1

a = accepts_callback(Impl(), 1)
reveal_type(a) # N: Revealed type is "builtins.int"
[builtins fixtures/paramspec.pyi]