Skip to content

Commit

Permalink
Fix propagated Any constraint (#12548)
Browse files Browse the repository at this point in the history
Fixes #12542.

This allows parameters to constrain to Any.
  • Loading branch information
A5rocks authored Apr 14, 2022
1 parent 0c6b290 commit 44993e6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]

0 comments on commit 44993e6

Please sign in to comment.