Skip to content
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

Allow iterable class objects to be unpacked #14803

Closed
wants to merge 11 commits into from
8 changes: 8 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3399,6 +3399,14 @@ def check_multi_assignment(
if len(relevant_items) == 1:
rvalue_type = get_proper_type(relevant_items[0])

if isinstance(rvalue_type, FunctionLike) and rvalue_type.is_type_obj():
ret_type = get_proper_type(rvalue_type.items[0].ret_type)
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(ret_type, Instance):
rvalue_type = ret_type.type.metaclass_type or rvalue_type

if isinstance(rvalue_type, TypeType) and isinstance(rvalue_type.item, Instance):
rvalue_type = rvalue_type.item.type.metaclass_type or rvalue_type

if isinstance(rvalue_type, AnyType):
for lv in lvalues:
if isinstance(lv, StarExpr):
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,22 @@ def f() -> None:
class A: pass
[out]

[case testInferringLvarTypesUnpackedFromIterableClassObject]
from typing import Iterator, Type
class Meta(type):
def __iter__(cls) -> Iterator[int]:
yield from [1, 2, 3]

class Foo(metaclass=Meta): ...

a, b, c = Foo
reveal_type(a) # N: Revealed type is "builtins.int"
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved

F: Type[Foo] = Foo
d, e, f = F
reveal_type(d) # N: Revealed type is "builtins.int"
[out]

[case testInferringLvarTypesInMultiDefWithInvalidTuple]
from typing import Tuple
t = None # type: Tuple[object, object, object]
Expand Down