Skip to content

Commit 0b4fde6

Browse files
JukkaLgvanrossum
authored andcommitted
Fix constraint inference for TypedDict and Iterable[T] (#3611)
Fixes #3610.
1 parent 1ad9aef commit 0b4fde6

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

mypy/constraints.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ def visit_instance(self, template: Instance) -> List[Constraint]:
312312
res = [] # type: List[Constraint]
313313
if isinstance(actual, CallableType) and actual.fallback is not None:
314314
actual = actual.fallback
315+
if isinstance(actual, TypedDictType):
316+
actual = actual.as_anonymous().fallback
315317
if isinstance(actual, Instance):
316318
instance = actual
317319
if (self.direction == SUBTYPE_OF and

test-data/unit/check-typeddict.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,17 @@ reveal_type(f(g)) # E: Revealed type is '<nothing>'
557557

558558
-- Constraint Solver
559559

560+
[case testTypedDictConstraintsAgainstIterable]
561+
from typing import TypeVar, Iterable
562+
from mypy_extensions import TypedDict
563+
T = TypeVar('T')
564+
def f(x: Iterable[T]) -> T: pass
565+
A = TypedDict('A', {'x': int})
566+
a: A
567+
reveal_type(f(a)) # E: Revealed type is 'builtins.str*'
568+
[builtins fixtures/dict.pyi]
569+
[typing fixtures/typing-full.pyi]
570+
560571
-- TODO: Figure out some way to trigger the ConstraintBuilderVisitor.visit_typeddict_type() path.
561572

562573

test-data/unit/fixtures/dict.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class object:
1111

1212
class type: pass
1313

14-
class dict(Iterable[KT], Mapping[KT, VT], Generic[KT, VT]):
14+
class dict(Mapping[KT, VT], Iterable[KT], Generic[KT, VT]):
1515
@overload
1616
def __init__(self, **kwargs: VT) -> None: pass
1717
@overload

test-data/unit/fixtures/typing-full.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ class Sequence(Iterable[T], Generic[T]):
103103
@abstractmethod
104104
def __getitem__(self, n: Any) -> T: pass
105105

106-
class Mapping(Generic[T, U]):
106+
class Mapping(Iterable[T], Generic[T, U]):
107107
@overload
108108
def get(self, k: T) -> Optional[U]: ...
109109
@overload
110110
def get(self, k: T, default: Union[U, V]) -> Union[U, V]: ...
111111

112-
class MutableMapping(Generic[T, U]): pass
112+
class MutableMapping(Mapping[T, U]): pass
113113

114114
TYPE_CHECKING = 1

0 commit comments

Comments
 (0)