Skip to content

Commit

Permalink
Fix inferring constrainta from param spec callable against Any (#11725)
Browse files Browse the repository at this point in the history
We didn't infer constraints from the return type.

Fixes #11704.
  • Loading branch information
JukkaL authored Dec 15, 2021
1 parent 61f0543 commit 22ab84a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
10 changes: 5 additions & 5 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,12 +581,12 @@ def visit_callable_type(self, template: CallableType) -> List[Constraint]:
if param_spec is None:
# FIX what if generic
res = self.infer_against_any(template.arg_types, self.actual)
res.extend(infer_constraints(template.ret_type, any_type, self.direction))
return res
else:
return [Constraint(param_spec.id,
SUBTYPE_OF,
callable_with_ellipsis(any_type, any_type, template.fallback))]
res = [Constraint(param_spec.id,
SUBTYPE_OF,
callable_with_ellipsis(any_type, any_type, template.fallback))]
res.extend(infer_constraints(template.ret_type, any_type, self.direction))
return res
elif isinstance(self.actual, Overloaded):
return self.infer_against_overloaded(self.actual, template)
elif isinstance(self.actual, TypeType):
Expand Down
28 changes: 28 additions & 0 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,31 @@ class C(Generic[P, P2]):
def m4(self, x: int) -> None:
pass
[builtins fixtures/dict.pyi]

[case testParamSpecOverUnannotatedDecorator]
from typing import Callable, Iterator, TypeVar, ContextManager, Any
from typing_extensions import ParamSpec

from nonexistent import deco2 # type: ignore

T = TypeVar("T")
P = ParamSpec("P")
T_co = TypeVar("T_co", covariant=True)

class CM(ContextManager[T_co]):
def __call__(self, func: T) -> T: ...

def deco1(
func: Callable[P, Iterator[T]]) -> Callable[P, CM[T]]: ...

@deco1
@deco2
def f():
pass

reveal_type(f) # N: Revealed type is "def (*Any, **Any) -> __main__.CM[Any]"

with f() as x:
pass
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]

0 comments on commit 22ab84a

Please sign in to comment.