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
When a function has a type signature like def foo(bar: Literal["a", "b"]) -> None: ... mypy throws an error if the argument passed in can be either a or b as it implies a general string.
I assume this issue of different implicit Literal / str types inside branches has been discussed before but I couldn't find it searching through the GitHub issues.
More generally, even if mypy were to infer literals for types, when merging the two branches mypy will use join rather than a union, which would still cause this. (I forget the rules for literal inference so that may already work, I don't know).
I don't know much about the logic but maybe it's possible to do a Union and put it in last_known_value for Instance? (but I'm not sure) nevermind that gets erased unless the variable is maked Final :/
Bug Report
When a function has a type signature like
def foo(bar: Literal["a", "b"]) -> None: ...
mypy throws an error if the argument passed in can be eithera
orb
as it implies a general string.To Reproduce
Expected Behavior
It would be nice if mypy recognized that
bar
here could only beLiteral["a"]
orLiteral["b"]
, i.eLiteral["a", "b"]
.Actual Behavior
Your Environment
mypy.ini
(and other config files): DefaultContext
This came up because of a recent change in typeshed python/typeshed#12181 which causes this pip code to trigger a mypy error: https://github.com/pypa/pip/blob/24.3.1/src/pip/_internal/utils/unpacking.py#L179
I assume this issue of different implicit
Literal
/str
types inside branches has been discussed before but I couldn't find it searching through the GitHub issues.x-ref: pypa/pip#13148
The text was updated successfully, but these errors were encountered: