You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix Any inference when unpacking iterators that don't directly inherit from typing.Iterator (#14821)
Fixes#14819.
Mypy currently silently infers an `Any` type when unpacking an iterator
that doesn't explicitly inherit from `typing.Iterator` (i.e., an
iterator that's a _structural_ subtype of `typing.Iterator`, but not a
_nominal_ subtype):
```python
from typing import TypeVar
T = TypeVar("T")
class Foo:
count: int
def __init__(self) -> None:
self.count = 0
def __iter__(self: T) -> T:
return self
def __next__(self) -> int:
self.count += 1
if self.count > 3:
raise StopIteration
return self.count
a, b, c = Foo()
reveal_type(a) # note: Revealed type is "Any"
```
However, we have enough information here to infer that the type of `a`
should really be `int`. This PR fixes that bug.
There's discussion on the issue thread about an alternative solution
that would involve changing some mypy behaviour that's been established
for around 10 years. For now, I haven't gone for that solution.
0 commit comments