basedmypy
basedmypy copied to clipboard
widen type at start of loop with assignment
a: int | None
a = None
for _ in range(2):
reveal_type(a) # None
a = 1
a should be widened to int | None at the start of the loop.
workaround
a: int | None
a = cast(int | None, None)
for _ in range(2):
reveal_type(a) # int | None
a = 1
- upstream https://github.com/python/mypy/issues/11612
this is more of an issue in basedmypy than upstream, since variables are narrowed on initial assignment so it's easier to run into
a: int | None = None
for _ in range(2):
reveal_type(a) # None
a = 1
p-1 I suppose?