Skip to content

Commit 6488838

Browse files
miedzinskigvanrossum
authored andcommitted
Fix constraint inference of CallableType and TypeType (#2895)
Together with #2894 this fixes #2892.
1 parent 1eb43de commit 6488838

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

mypy/constraints.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ def visit_callable_type(self, template: CallableType) -> List[Constraint]:
381381
return res
382382
elif isinstance(self.actual, Overloaded):
383383
return self.infer_against_overloaded(self.actual, template)
384+
elif isinstance(self.actual, TypeType):
385+
return infer_constraints(template.ret_type, self.actual.item, self.direction)
384386
else:
385387
return []
386388

@@ -442,9 +444,9 @@ def visit_overloaded(self, template: Overloaded) -> List[Constraint]:
442444
return res
443445

444446
def visit_type_type(self, template: TypeType) -> List[Constraint]:
445-
if isinstance(self.actual, CallableType) and self.actual.is_type_obj():
447+
if isinstance(self.actual, CallableType):
446448
return infer_constraints(template.item, self.actual.ret_type, self.direction)
447-
elif isinstance(self.actual, Overloaded) and self.actual.is_type_obj():
449+
elif isinstance(self.actual, Overloaded):
448450
return infer_constraints(template.item, self.actual.items()[0].ret_type,
449451
self.direction)
450452
elif isinstance(self.actual, TypeType):

test-data/unit/check-inference.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,14 @@ lb = [b]
664664
l = lb # E: Incompatible types in assignment (expression has type List[bool], variable has type List[A])
665665
[builtins fixtures/for.pyi]
666666

667+
[case testGenericFunctionWithTypeTypeAsCallable]
668+
from typing import Callable, Type, TypeVar
669+
T = TypeVar('T')
670+
def f(x: Callable[..., T]) -> T: return x()
671+
class A: pass
672+
x = None # type: Type[A]
673+
y = f(x)
674+
reveal_type(y) # E: Revealed type is '__main__.A*'
667675

668676
-- Generic function inference with unions
669677
-- --------------------------------------

0 commit comments

Comments
 (0)