Type refinement does not work with None in [a, b, c]
, any()
, or all()
checks
#17149
Labels
None in [a, b, c]
, any()
, or all()
checks
#17149
When checking if multiple variables are not None using the syntax
None in [a, b, c]
, mypy fails to refine the types of these variables to non-Optional types after this check. This issue persists even when using any() or all() for similar checks, preventing mypy from correctly inferring that the variables cannot be None past these checks, despite the code logically ensuring that these variables are not None.Code to Reproduce:
And here is another example with the
all
function (withany
it is quite similar):Expected Behavior:
After the checks if None in [a, b, c]: or using all(), mypy should refine the types of a, b, and c to str, int, and int respectively, acknowledging that they cannot be None.
Actual Behavior:
mypy does not refine the types and continues to treat a, b, and c as Optional[str], Optional[int], and Optional[int] respectively. This results in type errors when attempting to use these variables in a context that does not allow None.
Additional Information:
mypy version: 1.9.0 compiled
Python version: 3.10
No flags or configurations are altering the default behavior of mypy in this instance.
This behavior suggests a limitation in mypy's type inference system when it comes to using collective None checks with list membership or aggregate functions like any() and all(). An enhancement to allow type refinement in such cases would significantly improve usability in scenarios involving initialization checks for multiple variables.
The text was updated successfully, but these errors were encountered: