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
False positives with --strict-optional turned off #2999
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).
I can't reproduce the error on 0.620+ (I've tried installing earlier versions locally, but wasn't easily able to).
--strict-optional has been the default for a while now, and I think the people enabling the old behaviour with --no-strict-optional are becoming rarer and rarer.
There's been no activity on the issue since its creation five years ago.
The issue was low-priority when it was first created five years ago, and is even lower priority now.
Activity
Replace isinstance with cast to deal with python#2999; rename test
Replace isinstance with cast to deal with python#2999; rename test
AlexWaygood commentedon Mar 29, 2022
I'm closing this issue:
--strict-optionalhas been the default for a while now, and I think the people enabling the old behaviour with--no-strict-optionalare becoming rarer and rarer.