Skip to content
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

Reconsider constraints involving parameter specifications #15272

Merged
merged 5 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
70 changes: 53 additions & 17 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,25 +685,33 @@ def visit_instance(self, template: Instance) -> list[Constraint]:
)
elif isinstance(tvar, ParamSpecType) and isinstance(mapped_arg, ParamSpecType):
suffix = get_proper_type(instance_arg)
prefix = mapped_arg.prefix
length = len(prefix.arg_types)

if isinstance(suffix, CallableType):
prefix = mapped_arg.prefix
from_concat = bool(prefix.arg_types) or suffix.from_concatenate
suffix = suffix.copy_modified(from_concatenate=from_concat)

if isinstance(suffix, (Parameters, CallableType)):
# no such thing as variance for ParamSpecs
# TODO: is there a case I am missing?
# TODO: constraints between prefixes
prefix = mapped_arg.prefix
suffix = suffix.copy_modified(
suffix.arg_types[len(prefix.arg_types) :],
suffix.arg_kinds[len(prefix.arg_kinds) :],
suffix.arg_names[len(prefix.arg_names) :],

constrained_to = suffix.copy_modified(
suffix.arg_types[length:],
suffix.arg_kinds[length:],
suffix.arg_names[length:],
)
res.append(Constraint(mapped_arg, SUPERTYPE_OF, suffix))
res.append(Constraint(mapped_arg, SUPERTYPE_OF, constrained_to))
elif isinstance(suffix, ParamSpecType):
res.append(Constraint(mapped_arg, SUPERTYPE_OF, suffix))
suffix_prefix = suffix.prefix
constrained = suffix.copy_modified(
prefix=suffix_prefix.copy_modified(
suffix_prefix.arg_types[length:],
suffix_prefix.arg_kinds[length:],
suffix_prefix.arg_names[length:],
)
)
res.append(Constraint(mapped_arg, SUPERTYPE_OF, constrained))
else:
# This case should have been handled above.
assert not isinstance(tvar, TypeVarTupleType)
Expand Down Expand Up @@ -755,6 +763,8 @@ def visit_instance(self, template: Instance) -> list[Constraint]:
template_arg, ParamSpecType
):
suffix = get_proper_type(mapped_arg)
prefix = template_arg.prefix
length = len(prefix.arg_types)

if isinstance(suffix, CallableType):
prefix = template_arg.prefix
Expand All @@ -764,16 +774,22 @@ def visit_instance(self, template: Instance) -> list[Constraint]:
if isinstance(suffix, (Parameters, CallableType)):
# no such thing as variance for ParamSpecs
# TODO: is there a case I am missing?
# TODO: constraints between prefixes
prefix = template_arg.prefix

suffix = suffix.copy_modified(
suffix.arg_types[len(prefix.arg_types) :],
suffix.arg_kinds[len(prefix.arg_kinds) :],
suffix.arg_names[len(prefix.arg_names) :],
suffix.arg_types[length:],
suffix.arg_kinds[length:],
suffix.arg_names[length:],
)
res.append(Constraint(template_arg, SUPERTYPE_OF, suffix))
elif isinstance(suffix, ParamSpecType):
suffix_prefix = suffix.prefix
constrained = suffix.copy_modified(
A5rocks marked this conversation as resolved.
Show resolved Hide resolved
prefix=suffix_prefix.copy_modified(
suffix_prefix.arg_types[length:],
suffix_prefix.arg_kinds[length:],
suffix_prefix.arg_names[length:],
)
)
res.append(Constraint(template_arg, SUPERTYPE_OF, suffix))
else:
# This case should have been handled above.
Expand Down Expand Up @@ -942,9 +958,19 @@ def visit_callable_type(self, template: CallableType) -> list[Constraint]:
prefix_len = len(prefix.arg_types)
cactual_ps = cactual.param_spec()

cactual_prefix: Parameters | CallableType
if cactual_ps:
cactual_prefix = cactual_ps.prefix
else:
cactual_prefix = cactual

max_prefix_len = len(
[k for k in cactual_prefix.arg_kinds if k in (ARG_POS, ARG_OPT)]
)
prefix_len = min(prefix_len, max_prefix_len)

# we could check the prefixes match here, but that should be caught elsewhere.
if not cactual_ps:
max_prefix_len = len([k for k in cactual.arg_kinds if k in (ARG_POS, ARG_OPT)])
prefix_len = min(prefix_len, max_prefix_len)
res.append(
Constraint(
param_spec,
Expand All @@ -958,7 +984,17 @@ def visit_callable_type(self, template: CallableType) -> list[Constraint]:
)
)
else:
res.append(Constraint(param_spec, SUBTYPE_OF, cactual_ps))
# earlier, cactual_prefix = cactual_ps.prefix. thus, this is guaranteed
assert isinstance(cactual_prefix, Parameters)

constrained_by = cactual_ps.copy_modified(
prefix=cactual_prefix.copy_modified(
cactual_prefix.arg_types[prefix_len:],
cactual_prefix.arg_kinds[prefix_len:],
cactual_prefix.arg_names[prefix_len:],
)
)
res.append(Constraint(param_spec, SUBTYPE_OF, constrained_by))

# compare prefixes
cactual_prefix = cactual.copy_modified(
Expand Down
30 changes: 30 additions & 0 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,36 @@ def identity(func: Callable[P, None]) -> Callable[P, None]: ...
def f(f: Callable[P, None], *args: P.args, **kwargs: P.kwargs) -> None: ...
[builtins fixtures/paramspec.pyi]

[case testComplicatedParamSpecReturnType]
# regression test for https://github.com/python/mypy/issues/15073
from typing import TypeVar, Callable
from typing_extensions import ParamSpec, Concatenate

R = TypeVar("R")
P = ParamSpec("P")

def f(
) -> Callable[[Callable[Concatenate[Callable[P, R], P], R]], Callable[P, R]]:
def r(fn: Callable[Concatenate[Callable[P, R], P], R]) -> Callable[P, R]: ...
return r
[builtins fixtures/paramspec.pyi]

[case testParamSpecToParamSpecAssignment]
# minimized from https://github.com/python/mypy/issues/15037
# ~ the same as https://github.com/python/mypy/issues/15065
from typing import Callable
from typing_extensions import Concatenate, ParamSpec

P = ParamSpec("P")

def f(f: Callable[Concatenate[int, P], None]) -> Callable[P, None]: ...

x: Callable[
[Callable[Concatenate[int, P], None]],
Callable[P, None],
] = f
[builtins fixtures/paramspec.pyi]

[case testParamSpecDecoratorAppliedToGeneric]
# flags: --new-type-inference
from typing import Callable, List, TypeVar
Expand Down