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 gives diagnostic on code Literal typing code accepted by pyright, pyrefly #19747
Mypy issues a diagnostic for some code where pyrefly and pyright correctly intuit the Literal type of an expression.
This affected me in my project (jepler/wwvbpy#155) though I worked around it; the example here is distilled from my project.
To Reproduce
fromtypingimportLiteralWWVBChannel=Literal["amplitude", "phase", "both"]
# OK in pyright, pyreflydeff1(s: str) ->WWVBChannel|None:
ifs=="amplitude"ors=="phase"ors=="both":
returnsreturnNone# OK in pyrefly only; produces diagnostic in pyrightdeff2(s: str) ->WWVBChannel|None:
ifsin {"amplitude", "phase", "both"}:
returnsreturnNone# OK in pyright, pyreflydeff3(s: str) ->WWVBChannel|None:
ifsin ("amplitude", "phase", "both"):
returnsreturnNone
Expected Behavior
mypy should be able to narrow the type of the s argument from str to WWVBChannel. There's at least one variant accepted by mypy that is accepted by these other two type checkers.
Actual Behavior
$ mypy --strict lit.py
/home/jepler/lit.py:8: error: Incompatible return value type (got "str", expected "Literal['amplitude', 'phase', 'both'] | None") [return-value]
[and so on for all the 'return s' statements]
Your Environment
Mypy version used: mypy 1.17.1 (compiled: yes)
Mypy command-line flags: --strict
Mypy configuration options from mypy.ini (and other config files): None
Python version used: Python 3.13.5 (debian trixie amd64)
Activity
jepler commentedon Aug 27, 2025
(you can
return typing.cast(WWVBChannel, s)but that triggers a redundant cast diagnostic in pyrefly)brianschubert commentedon Aug 27, 2025
Thanks for the report! Closing as duplicate of #12535
Literalusinginsyntax #12535jepler commentedon Aug 27, 2025
Thanks, my searching for the term "literal" didn't find that issue.