From 44993e6a18f852a9296786267ecf26ea730b4f31 Mon Sep 17 00:00:00 2001 From: EXPLOSION Date: Thu, 14 Apr 2022 17:49:51 +0900 Subject: [PATCH] Fix propagated Any constraint (#12548) Fixes #12542. This allows parameters to constrain to Any. --- mypy/constraints.py | 4 ++++ test-data/unit/check-parameter-specification.test | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/mypy/constraints.py b/mypy/constraints.py index 0b2217b21ae0c..b7ed1492e5f3b 100644 --- a/mypy/constraints.py +++ b/mypy/constraints.py @@ -407,6 +407,10 @@ def visit_unpack_type(self, template: UnpackType) -> List[Constraint]: raise NotImplementedError def visit_parameters(self, template: Parameters) -> List[Constraint]: + # constraining Any against C[P] turns into infer_against_any([P], Any) + # ... which seems like the only case this can happen. Better to fail loudly. + if isinstance(self.actual, AnyType): + return self.infer_against_any(template.arg_types, self.actual) raise RuntimeError("Parameters cannot be constrained to") # Non-leaf types diff --git a/test-data/unit/check-parameter-specification.test b/test-data/unit/check-parameter-specification.test index be296b243019a..2242b79d4b643 100644 --- a/test-data/unit/check-parameter-specification.test +++ b/test-data/unit/check-parameter-specification.test @@ -1049,3 +1049,17 @@ P = ParamSpec("P") def x(f: Callable[Concatenate[int, Concatenate[int, P]], None]) -> None: ... # E: Nested Concatenates are invalid [builtins fixtures/tuple.pyi] + +[case testPropagatedAnyConstraintsAreOK] +from typing import Any, Callable, Generic, TypeVar +from typing_extensions import ParamSpec + +T = TypeVar("T") +P = ParamSpec("P") + +def callback(func: Callable[[Any], Any]) -> None: ... +class Job(Generic[P]): ... + +@callback +def run_job(job: Job[...]) -> T: ... +[builtins fixtures/tuple.pyi]