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
$ mypy x.py x.py:2: note: Revealed type is "builtins.str"x.py:4: note: Revealed type is "builtins.str"x.py:7: note: Revealed type is "builtins.int"x.py:9: note: Revealed type is "builtins.int"
If mypy were to support type narrowing using chained or statements and in statements, the output would look like:
$ mypy x.py x.py:4: note: Revealed type is "builtins.str"x.py:6: note: Revealed type is "Union[Literal['a'], Literal['b'], Literal['c']]"x.py:9: note: Revealed type is "builtins.int"x.py:11: note: Revealed type is "Union[Literal[10], Literal[20], Literal[50]]"
I think doing simple narrowing here would be relatively easy and very useful.
This issue seems to address narrowing both for in but also for ==. Should these two features be tracked independently (if their implementations would be rather decoupled) or is tracking them together preferred (if implementing one would likely solve the other)?
I couldn't find an issue specifically for narrowing literals in == equality comparisons, and I would have expected this to be implemented first, because it even feels more basic than the in operator:
Activity
tmke8 commentedon Apr 7, 2022
Related to (but maybe not quite a duplicate of) #9718.
tusharsadhwani commentedon Apr 7, 2022
It's a superset of #9718. I'll be looking into implementing this then, starting with the literal narrowing case.
AlexWaygood commentedon Apr 7, 2022
A superset of #9718, and a subset of #10977 :)
bluenote10 commentedon Aug 14, 2023
This issue seems to address narrowing both for
inbut also for==. Should these two features be tracked independently (if their implementations would be rather decoupled) or is tracking them together preferred (if implementing one would likely solve the other)?I couldn't find an issue specifically for narrowing literals in
==equality comparisons, and I would have expected this to be implemented first, because it even feels more basic than theinoperator:Pyright seems to understand this kind of narrowing well.
asottile-sentry commentedon Jun 6, 2024
I opted for a clunky workaround using
TypeIsthough it would be nice if this were automaticJelleZijlstra commentedon Jun 7, 2024
This is similar to #3229 though that one doesn't ask for narrowing to Literal, possibly because the issue predates literal types.
Fix mypy issues after 1.14 upgrade
Fix mypy issues after 1.14 upgrade