Skip to content

Commit 44993e6

Browse files
authored
Fix propagated Any constraint (#12548)
Fixes #12542. This allows parameters to constrain to Any.
1 parent 0c6b290 commit 44993e6

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

mypy/constraints.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,10 @@ def visit_unpack_type(self, template: UnpackType) -> List[Constraint]:
407407
raise NotImplementedError
408408

409409
def visit_parameters(self, template: Parameters) -> List[Constraint]:
410+
# constraining Any against C[P] turns into infer_against_any([P], Any)
411+
# ... which seems like the only case this can happen. Better to fail loudly.
412+
if isinstance(self.actual, AnyType):
413+
return self.infer_against_any(template.arg_types, self.actual)
410414
raise RuntimeError("Parameters cannot be constrained to")
411415

412416
# Non-leaf types

test-data/unit/check-parameter-specification.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,3 +1049,17 @@ P = ParamSpec("P")
10491049

10501050
def x(f: Callable[Concatenate[int, Concatenate[int, P]], None]) -> None: ... # E: Nested Concatenates are invalid
10511051
[builtins fixtures/tuple.pyi]
1052+
1053+
[case testPropagatedAnyConstraintsAreOK]
1054+
from typing import Any, Callable, Generic, TypeVar
1055+
from typing_extensions import ParamSpec
1056+
1057+
T = TypeVar("T")
1058+
P = ParamSpec("P")
1059+
1060+
def callback(func: Callable[[Any], Any]) -> None: ...
1061+
class Job(Generic[P]): ...
1062+
1063+
@callback
1064+
def run_job(job: Job[...]) -> T: ...
1065+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)