KotlinIsland / basedmypy

Based Python static type checker with baseline, sane default settings and based typing features
https://kotlinisland.github.io/basedmypy/
Other
146 stars 4 forks source link

widen type at start of loop with assignment #568

Open KotlinIsland opened 1 year ago

KotlinIsland commented 1 year ago
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
KotlinIsland commented 10 months ago
DetachHead commented 10 months ago

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
KotlinIsland commented 10 months ago

p-1 I suppose?