-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
False narrowing with repeated assignment inside if resulting in bad false negative #18492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks! Pretty bad bug. Looks like it's just some issue if there's a double assignment (and specifically with None and without an explicit annotation, so likely related to partial types):
|
I can reproduce the minified example with the same toolchain as above. |
@hauntsaninja While trying to reproduce this I encountered an issue with mypy where it fails to correctly identify the type of variable and ultimately raises an error at an undesired place
|
I've tried reproducing the original example and in the latest nightly (301c3b6) and it no longer reproduces. In the function
The problem in the simplified example by @Prabhat-Thapa45 is still reproducible, though (mypy doesn't take into assignment a reachable assignment in the false branch). However, with the experimental options some_condition: bool = "123".isnumeric()
list_of_dicts = [{"some": 1, "columns": 2, "will": 3, "go": 4, "here": 5}]
if some_condition > 0:
output_df = pd.DataFrame(list_of_dicts)
output_df = output_df[["some", "columns", "here"]]
else:
output_df = None # warning (false positive?) without --allow-redefinition-new --local-partial-type
def fails_as_expected_1(some_condition: bool) -> pd.DataFrame:
list_of_dicts = [{"some": 1, "columns": 2, "will": 3, "go": 4, "here": 5}]
output_df = pd.DataFrame(list_of_dicts)
output_df = output_df[["some", "columns", "here"]]
output_df = None # warning here (false positive?) without --allow-redefinition-new --local-partial-type
other_df: pd.DataFrame = output_df # warning here with --allow-redefinition-new
return other_df |
Looks like this got fixed by #18538 |
Bug Report
In a function,
mypy
fails to detect an incompatible assignment in a function, if that is after a__get_item__
access inside a branch. Weirdly,mypy
correctly deduces the types, as shown withreveal_type
but does not complain about the incorrect assignment anyways.To Reproduce
I would have liked to give a playground link, but the MWE involves a
pandas.DataFrame
which seems not to work on the playground.Expected Behavior
Each block and function should contain a
mypy
error regarding an incompatible assignment.Actual Behavior
For the last function, no such error is produced:
Your Environment
pyproject.toml
:The text was updated successfully, but these errors were encountered: