Skip to content

Fix constraint inference for TypedDict and Iterable[T] #3611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ def visit_instance(self, template: Instance) -> List[Constraint]:
res = [] # type: List[Constraint]
if isinstance(actual, CallableType) and actual.fallback is not None:
actual = actual.fallback
if isinstance(actual, TypedDictType):
actual = actual.as_anonymous().fallback
if isinstance(actual, Instance):
instance = actual
if (self.direction == SUBTYPE_OF and
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,17 @@ reveal_type(f(g)) # E: Revealed type is '<nothing>'

-- Constraint Solver

[case testTypedDictConstraintsAgainstIterable]
from typing import TypeVar, Iterable
from mypy_extensions import TypedDict
T = TypeVar('T')
def f(x: Iterable[T]) -> T: pass
A = TypedDict('A', {'x': int})
a: A
reveal_type(f(a)) # E: Revealed type is 'builtins.str*'
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]

-- TODO: Figure out some way to trigger the ConstraintBuilderVisitor.visit_typeddict_type() path.


Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/fixtures/dict.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class object:

class type: pass

class dict(Iterable[KT], Mapping[KT, VT], Generic[KT, VT]):
class dict(Mapping[KT, VT], Iterable[KT], Generic[KT, VT]):
@overload
def __init__(self, **kwargs: VT) -> None: pass
@overload
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/fixtures/typing-full.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ class Sequence(Iterable[T], Generic[T]):
@abstractmethod
def __getitem__(self, n: Any) -> T: pass

class Mapping(Generic[T, U]):
class Mapping(Iterable[T], Generic[T, U]):
@overload
def get(self, k: T) -> Optional[U]: ...
@overload
def get(self, k: T, default: Union[U, V]) -> Union[U, V]: ...

class MutableMapping(Generic[T, U]): pass
class MutableMapping(Mapping[T, U]): pass

TYPE_CHECKING = 1