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
Mypy performs wrong narrowing with == None and in (None,) conditions #15759
This is a pretty ridiculous bug, I'm surprised it has not shown up until now. 😆 It goes back all the way to mypy 0.800. I will open a PR with a fix shortly.
Thankfully this isn't a problem in practice, since None comparisons are usually done with is None instead of == None.
deffunc(val: str|None) ->None:
ifval==None:
reveal_type(val) # ❌ Revealed type is "builtins.str"ifvalin (None,):
reveal_type(val) # ❌ Revealed type is "builtins.str"
This is because type narrowing logic internally uses is_optional() to check whether a value can be None. is_optional() only considers UnionType, whereas NoneType value may also have value None.
Activity
intgr commentedon Jul 26, 2023
This should have label
topic-type-narrowing