Skip to content

Commit

Permalink
Fix iterable rvalue and non-trivial inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
JukkaL committed Aug 18, 2013
1 parent 452cff3 commit affc8e0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
24 changes: 16 additions & 8 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,12 @@ def check_multi_assignment(self, lvalue_types: List[Type],
elif (is_subtype(rvalue_type,
self.named_generic_type('builtins.Iterable',
[AnyType()])) and
not isinstance(rvalue_type, NoneTyp)):
# Iterable rvalue.
item_type = cast(Instance, rvalue_type).args[0]
isinstance(rvalue_type, Instance)):
# Rvalue is iterable.
iterable = map_instance_to_supertype(
cast(Instance, rvalue_type),
self.lookup_typeinfo('builtins.Iterable'))
item_type = iterable.args[0]
for k in range(len(lvalue_types)):
self.check_single_assignment(lvalue_types[k],
index_lvalues[k],
Expand Down Expand Up @@ -1042,12 +1045,17 @@ def named_type_if_exists(self, name: str) -> Type:
return UnboundType(name)

def named_generic_type(self, name: str, args: List[Type]) -> Instance:
"""Return an instance with the given name and type
arguments. Assume that the number of arguments is correct.
"""Return an instance with the given name and type arguments.
Assume that the number of arguments is correct. Assume that
the name refers to a compatible generic type.
"""
# Assume that the name refers to a compatible generic type.
sym = self.lookup_qualified(name)
return Instance(cast(TypeInfo, sym.node), args)
return Instance(self.lookup_typeinfo(name), args)

def lookup_typeinfo(self, fullname: str) -> TypeInfo:
# Assume that the name refers to a class.
sym = self.lookup_qualified(fullname)
return cast(TypeInfo, sym.node)

def type_type(self) -> Instance:
"""Return instance type 'type'."""
Expand Down
16 changes: 16 additions & 0 deletions mypy/test/data/check-generic-subtyping.test
Original file line number Diff line number Diff line change
Expand Up @@ -596,3 +596,19 @@ ia = Undefined # type: I[A]

ia.f(b) # E: Argument 1 to "f" of "J" has incompatible type "B"
ia.f(a)


-- Misc
-- ----


[case testMultipleAssignmentAndGenericSubtyping]
from typing import Iterable, Undefined
n, s = Undefined(int), Undefined(str)
class Nums(Iterable[int]):
def __iter__(self): pass
def __next__(self): pass
n, n = Nums()
s, s = Nums() # E: Incompatible types in assignment
[builtins fixtures/for.py]
[out]

0 comments on commit affc8e0

Please sign in to comment.