Closed
Description
This is a low-priority issue (or maybe should even be closed), just wanted to add it for the record in case others run into it.
When --strict-optional
is turned off, mypy in some cases is unable to infer the correct type inside ... if None else ...
type conditionals; this doesn't happen always, only if it's preceded with assert isinstance(x, (X, type(None)))
when x
was known to be of type Base
, which is the base class of X
:
from typing import *
class A:
a = 1
class B(A):
b = 2
def f(x: Union[A, None]) -> None:
assert isinstance(x, (B, type(None)))
y = x.b if x is not None else x # fails type check if --strict-optional is off, passes otherwise
The fix would be messy (mypy correctly analyzes the if
statement, the problem is that it ignores the isinstance
because it thinks that the assertion always succeeds anyway due to some problem with None
handling).