Open
Description
For the following code, mypy reports an incompatible type error on the function f
from typing import Optional
def f(x:Optional[str]) -> bool:
return bool(x) and g(x)
def f2(x:Optional[str]) -> bool:
if x:
return g(x)
return False
def g(x: str) -> bool:
return bool(x)
test.py:4: error: Argument 1 to "g" has incompatible type "Optional[str]"; expected "str"
however the code is equivalent to the one in f2
, for which no error is raised.
-
What are the versions of mypy and Python you are using?
Mypy: 0.620, Python: 3.6.5 -
What are the mypy flags you are using? (For example --strict-optional)
[mypy]
# This flag should be removed here and enabled per module when we have a
# considerable number of stubs for external libraries.
ignore_missing_imports = True
strict_optional = True
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True
check_untyped_defs = True
Activity
ilevkivskyi commentedon Aug 30, 2018
For mypy
bool
is just a function with type(object) -> bool
, so it doesn't know that, e.g., forNone
this function always returnsFalse
. We can of course special-case this (especially with literal types coming soon). But taking into account that there is an equivalent (and IMO better/more readable) pattern, I think this is low priority.sk- commentedon Aug 30, 2018
Thanks @ilevkivskyi for the explanation. "final types" would help in this case without having to special case
bool
, right? Do you know when are they planned to be implemented?"Regarding readability, without types one would just write:
but with types we have to write:
which is much more verbose.
Or the alternative in the OP using
bool
. In both cases we need to add extra code to make sure the types are as advertised.ilevkivskyi commentedon Aug 30, 2018
Note that
x and g(x)
will be a string ifx
is an empty string (try this in Python interpreter). So the two functions you wrote above are not equivalent, and this is why IMO the second is better.Soon.
stephen-akerson-rh commentedon Sep 11, 2019
I have a similar issue. With the following snippet:
mypy reports:
error: Argument 1 to "f" has incompatible type "Optional[str]"; expected "str"
.It therefore seems mypy is unaware of the check for the existence of
x
any timey
is passed tog
(see the conditional on L5).Note: this also seems related to: https://gist.github.com/reillysiemens/4f60233f321d84b5db911f00b17757a0