Skip to content

Commit 22ab84a

Browse files
authored
Fix inferring constrainta from param spec callable against Any (#11725)
We didn't infer constraints from the return type. Fixes #11704.
1 parent 61f0543 commit 22ab84a

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

mypy/constraints.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -581,12 +581,12 @@ def visit_callable_type(self, template: CallableType) -> List[Constraint]:
581581
if param_spec is None:
582582
# FIX what if generic
583583
res = self.infer_against_any(template.arg_types, self.actual)
584-
res.extend(infer_constraints(template.ret_type, any_type, self.direction))
585-
return res
586584
else:
587-
return [Constraint(param_spec.id,
588-
SUBTYPE_OF,
589-
callable_with_ellipsis(any_type, any_type, template.fallback))]
585+
res = [Constraint(param_spec.id,
586+
SUBTYPE_OF,
587+
callable_with_ellipsis(any_type, any_type, template.fallback))]
588+
res.extend(infer_constraints(template.ret_type, any_type, self.direction))
589+
return res
590590
elif isinstance(self.actual, Overloaded):
591591
return self.infer_against_overloaded(self.actual, template)
592592
elif isinstance(self.actual, TypeType):

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

+28
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,31 @@ class C(Generic[P, P2]):
378378
def m4(self, x: int) -> None:
379379
pass
380380
[builtins fixtures/dict.pyi]
381+
382+
[case testParamSpecOverUnannotatedDecorator]
383+
from typing import Callable, Iterator, TypeVar, ContextManager, Any
384+
from typing_extensions import ParamSpec
385+
386+
from nonexistent import deco2 # type: ignore
387+
388+
T = TypeVar("T")
389+
P = ParamSpec("P")
390+
T_co = TypeVar("T_co", covariant=True)
391+
392+
class CM(ContextManager[T_co]):
393+
def __call__(self, func: T) -> T: ...
394+
395+
def deco1(
396+
func: Callable[P, Iterator[T]]) -> Callable[P, CM[T]]: ...
397+
398+
@deco1
399+
@deco2
400+
def f():
401+
pass
402+
403+
reveal_type(f) # N: Revealed type is "def (*Any, **Any) -> __main__.CM[Any]"
404+
405+
with f() as x:
406+
pass
407+
[builtins fixtures/dict.pyi]
408+
[typing fixtures/typing-full.pyi]

0 commit comments

Comments
 (0)