You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Type fails to get "refined" on branch when is None-check happens in variable assignment #9229
Are you reporting a bug, or opening a feature request? Bug
Please insert below the code you are checking with mypy:
fromtypingimportOptionaldefbar(var: str):
passdeffoo(var: Optional[str]=None):
var_not_None=not (varisNone)
ifvar_not_None:
# var can only be a `str` at this point,# but `mypy` complains that# Argument 1 to "bar" has incompatible type "Optional[str]"; expected "str"bar(var)
deffoo2(var: Optional[str]=None):
ifnot(varisNone):
# no complaints from mypybar(var)
What is the actual behavior/output?
Running mypy on this reproducer returns:
error: Argument 1 to "bar" has incompatible type "Optional[str]"; expected "str" Found 1 error in 1 file (checked 1 source file)
What is the behavior/output you expect?
No errors, both foo2 and foo are equivalent.
What are the versions of mypy and Python you are using?
mypy 0.782 and Python 3.6.8
Do you see the same issue after installing mypy from Git master?
Yup, unless I did something wrong 😅
Summary:
When you have an Optional[str]-typed variable var, one would expect that an not (var is None) check in an if-statement should tell mypy that all code within that branch has that the variable has type str. Indeed, mypy shows the expected behaviour for this, as shown in function foo2 above.
However, there are many instances where you want to check if a variable is None outside of the if-statement. For instance, if your if-statement were to check many boolean conditions at once. In this situation, one may save the result of the check into a variable. This is the case in the foo function above, where mypy complains.
I suspect this problem may not be solvable in the general case, but perhaps we can improve the status-quo nonetheless to catch this error.
If this is a beginner-friendly bug, I would love to help fix it! I've been meaning to get involved with the mypy project; just some guidance would do. Thanks!
This feature has been requested before, and we've closed the issues as too complicated to fix (or at least some of them). Since this is a somewhat common request, let's keep this open, but this looks low priority, since there may be no easy fix.
@Akuli I opened a PR #9288 that adds support for if (variable := condition) case as I was already trying to fix the behavior of walrus operator in conditional statements :)
Activity
Akuli commentedon Jul 29, 2020
using
:=operator doesn't help here eitherwhy is
var_not_None := blatreated differently than plainblahere?JukkaL commentedon Jul 31, 2020
This feature has been requested before, and we've closed the issues as too complicated to fix (or at least some of them). Since this is a somewhat common request, let's keep this open, but this looks low priority, since there may be no easy fix.
Akuli commentedon Jul 31, 2020
maybe support
if (variable := condition)for now? that should be just likeif condition, except that it also sets the variablekprzybyla commentedon Aug 11, 2020
@Akuli I opened a PR #9288 that adds support for
if (variable := condition)case as I was already trying to fix the behavior of walrus operator in conditional statements :)