Closed
Description
from typing import AnyStr, Generic, overload, Iterator
class Foo(Generic[AnyStr]):
x: AnyStr
@overload
def __iter__(self: Foo[str]) -> Iterator[bytes]: ...
@overload
def __iter__(self: Foo[bytes]) -> Iterator[str]: ...
def __iter__(self) -> Iterator[str | bytes]:
if isinstance(self.x, str):
yield from [b"a", b"b", b"c"]
yield from ["a", "b", "c"]
a, b, c = Foo[str]()
reveal_type(a) # note: Revealed type is "Any"
Mypy should have enough information here to infer that the type of a
is bytes
(and should probably ask for an explicit annotation if it can't infer that, rather than silently inferring Any
).
Mypy version tested on
- mypy 1.2.0+dev.c245e91e1ba4d23ff28e77d08f5e103c43733bf5 (compiled: no)
- Also reproducible on mypy playground with mypy v1.0.0: https://mypy-play.net/?mypy=latest&python=3.11&gist=dde121c6575944d735d3a9a4b2dc57e3