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
$ mypy a.py
a.py:5: error: Incompatible types in assignment (expression has type "None", variable has type "str")
What is the behavior/output you expect?
Since there is no annotation on b, mypy should infer that the variable b has the broadest type that matches the possible assignments: Union[str, None] (aka Optional[str]).
I didn't expect that mypy would just infer the type of the variable based on the first assignment.
FWIW: PyCharm's type inference infers the type Optional[str]
If this is not possible, then perhaps mypy could give a more meaningful error and/or hint (e.g. "error: variable b is assigned a different type based on different branches" + "hint: provide a type annotation for b: Optional[str]").
What are the versions of mypy and Python you are using?
Are you reporting a bug, or opening a feature request?
It's a matter of opinion.
Please insert below the code you are checking with mypy.
Since there is no annotation on
b
, mypy should infer that the variableb
has the broadest type that matches the possible assignments:Union[str, None]
(akaOptional[str]
).I didn't expect that mypy would just infer the type of the variable based on the first assignment.
FWIW: PyCharm's type inference infers the type
Optional[str]
If this is not possible, then perhaps mypy could give a more meaningful error and/or hint (e.g. "error: variable
b
is assigned a different type based on different branches" + "hint: provide a type annotation forb
: Optional[str]").Python 3.7.4 / mypy 0.750+dev.3fc382321e4531450e91c55fddb6d4ed54611d5e
(git master)
No flags specified, but it also occurs with --strict-optional
Related to #4061
The text was updated successfully, but these errors were encountered: