@@ -4524,6 +4524,72 @@ WithMeta().a # E: "WithMeta" has no attribute "a"
4524
4524
t: Type[WithMeta]
4525
4525
t.unknown # OK
4526
4526
4527
+ [case testUnpackIterableClassWithOverloadedIter]
4528
+ from typing import Generic, overload, Iterator, TypeVar, Union
4529
+
4530
+ AnyNum = TypeVar('AnyNum', int, float)
4531
+
4532
+ class Foo(Generic[AnyNum]):
4533
+ @overload
4534
+ def __iter__(self: Foo[int]) -> Iterator[float]: ...
4535
+ @overload
4536
+ def __iter__(self: Foo[float]) -> Iterator[int]: ...
4537
+ def __iter__(self) -> Iterator[Union[float, int]]:
4538
+ ...
4539
+
4540
+ a, b, c = Foo[int]()
4541
+ reveal_type(a) # N: Revealed type is "builtins.float"
4542
+ reveal_type(b) # N: Revealed type is "builtins.float"
4543
+ reveal_type(c) # N: Revealed type is "builtins.float"
4544
+
4545
+ x, y = Foo[float]()
4546
+ reveal_type(x) # N: Revealed type is "builtins.int"
4547
+ reveal_type(y) # N: Revealed type is "builtins.int"
4548
+ [builtins fixtures/list.pyi]
4549
+
4550
+ [case testUnpackIterableClassWithOverloadedIter2]
4551
+ from typing import Union, TypeVar, Generic, overload, Iterator
4552
+
4553
+ X = TypeVar('X')
4554
+
4555
+ class Foo(Generic[X]):
4556
+ @overload
4557
+ def __iter__(self: Foo[str]) -> Iterator[int]: ... # type: ignore
4558
+ @overload
4559
+ def __iter__(self: Foo[X]) -> Iterator[str]: ...
4560
+ def __iter__(self) -> Iterator[Union[int, str]]:
4561
+ ...
4562
+
4563
+ a, b, c = Foo[str]()
4564
+ reveal_type(a) # N: Revealed type is "builtins.int"
4565
+ reveal_type(b) # N: Revealed type is "builtins.int"
4566
+ reveal_type(c) # N: Revealed type is "builtins.int"
4567
+
4568
+ x, y = Foo[float]()
4569
+ reveal_type(x) # N: Revealed type is "builtins.str"
4570
+ reveal_type(y) # N: Revealed type is "builtins.str"
4571
+ [builtins fixtures/list.pyi]
4572
+
4573
+ [case testUnpackIterableRegular]
4574
+ from typing import TypeVar, Generic, Iterator
4575
+
4576
+ X = TypeVar('X')
4577
+
4578
+ class Foo(Generic[X]):
4579
+ def __iter__(self) -> Iterator[X]:
4580
+ ...
4581
+
4582
+ a, b = Foo[int]()
4583
+ reveal_type(a) # N: Revealed type is "builtins.int"
4584
+ reveal_type(b) # N: Revealed type is "builtins.int"
4585
+ [builtins fixtures/list.pyi]
4586
+
4587
+ [case testUnpackNotIterableClass]
4588
+ class Foo: ...
4589
+
4590
+ a, b, c = Foo() # E: "Foo" object is not iterable
4591
+ [builtins fixtures/list.pyi]
4592
+
4527
4593
[case testMetaclassIterable]
4528
4594
from typing import Iterable, Iterator
4529
4595
0 commit comments