Binder loses narrowed type of a variable if variable may be uninitialized #18619
Labels
bug
mypy got something wrong
false-positive
mypy gave an error on correct code
priority-0-high
topic-type-narrowing
Conditional type narrowing / binder
Bug Report
Mypy sometimes loses track of the narrowed type of a variable, at least when the variable can be uninitialized in some code paths. This can happen when variables is both initialized and narrowed in a conditional block.
To Reproduce
Expected Behavior
Revealed type is
str
.Actual Behavior
Revealed type is
int | str
.Discussion
Here are some more realistic examples:
This issue interferes with inferring union types from assignments (#18568).
@ilevkivskyi Do you have an idea of how to fix this, since you did some work on the binder recently? If a variable hasn't been assigned in some conditional code path, maybe the type should be treated as
Never
when merging types from different code paths?The text was updated successfully, but these errors were encountered: