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
reveal_type() shows that it correctly realises that 'foo' is no longer optional after the assignment in outer(), but for some reason it has become optional again in inner():
$ mypy foo.py
foo.py:11: error: Item "None" of "Optional[Foo]" has no attribute "bar"
I know it's possible to overcome this by doing e.g. assert foo is not None, but it seems this case should work as is.
The text was updated successfully, but these errors were encountered:
This has been discussed on the tracker before. I believe it's intended behavior; the reasoning is that you don't know in advance where in the outer function the inner function will be called, so you don't know what the restricted type of the variable is within the inner function.
In this specific case the inference doesn't seem like it should be too hard - foo is set to a non-None value before inner() is defined, and not assigned to again afterwards.
Replacing None with a default value early in a function is quite a common pattern, as it's not always possible or desirable to set the "real default" in the function signature.
Mypy (0.600, default options - i.e. strict optional enabled) seems to get confused about the optionality of 'foo' in the below code:
reveal_type() shows that it correctly realises that 'foo' is no longer optional after the assignment in outer(), but for some reason it has become optional again in inner():
I know it's possible to overcome this by doing e.g.
assert foo is not None
, but it seems this case should work as is.The text was updated successfully, but these errors were encountered: