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
Let "Literal" accept another Literal type variable #10026
a.py:4: error: Parameter 1 of Literal[...] is invalid
a.py:4: error: Variable "a.s" is not valid as a type
a.py:4: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases
Found 2 errors in 1 file (checked 1 source file)
from now on I would like to avoid using directly the values 1 and 2 and instead refer to the constants MY_CONST_A and MY_CONST_B.
let's say I want to define a function that can return either of that consts, what the return type should be?
or similarly, I want to set the type of a value that can be set to one of the two values; what is it's type?
is not supported by mypy, and I need to set Literal[1,2] as return type to make the signature work
Reacted by Marat Reimers, DevilXD, Garrett Reynolds, GalNevo, Keith, Parsa Yadollahi, karolyjozsa, Adam Peacock, Reuben Morais, Christian Segercrantz and 1 more
This proposal would require an update to PEP 586, so this discussion should probably be moved to the typing-sig forum rather than mypy's issue tracker. (For full transparency, I'm one of the primary contributors to pyright, Microsoft's Python type checker.)
If we were to adopt a change like this, I think we'd need to place some additional constraints on the constants. Not only would they need to be marked Final, but they'd also need to be assigned an expression that is unambiguously a literal value — in other words, expressions that are allowed today within a Literal. That excludes more complex expressions.
For example, I would propose that none of these constants should be allowed in a Literal.
The problem with more complex expressions is that different type checkers can infer different types. If we constrain it to literal values only, there's no ambiguity or inconsistency between type checkers.
Reacted by Zac Hatfield-Dodds, DevilXD, Francisco Castro and Garrett Reynolds
This feature is again intended to help make using and reusing literal types more ergonomic.
That being said, I just ran into this issue while trying to specify a variable type as Literal[logging.NOTSET, logging.DEBUG], caused most likely by those being inferred by MyPy as int rather than Literal[0] and Literal[10] respectively. Regarding the type being int instead of Literal, I've proposed a typeshed PR: python/typeshed#6610
Having that PR accepted, MyPy would still be expected to accept Literal type aliases like so:
NOTSET: Literal[0]
DEBUG: Literal[10]
# currently validlogging_level: Literal[Literal[0], Literal[10]]
# currently invalidlogging_level: Literal[NOTSET, DEBUG]
Reacted by Francisco Castro, Garrett Reynolds, karolyjozsa and Ben Mares
Activity
Zac-HD commentedon Sep 28, 2021
I'm not sure this is a bug: the PEP notes that "", and
mypyis perfectly happy if you omit theFinal:And in
Literal[s],sis a name, not a literal value.ZeeD commentedon Sep 28, 2021
while this may be not a bug, I thik this design decision limits the usefulness of
Literals - andFinalslet's say I define a pair of constants
from now on I would like to avoid using directly the values
1and2and instead refer to the constantsMY_CONST_AandMY_CONST_B.let's say I want to define a function that can return either of that consts, what the return type should be?
or similarly, I want to set the type of a value that can be set to one of the two values; what is it's type?
is not supported by mypy, and I need to set
Literal[1,2]as return type to make the signature workerictraut commentedon Sep 28, 2021
This proposal would require an update to PEP 586, so this discussion should probably be moved to the typing-sig forum rather than mypy's issue tracker. (For full transparency, I'm one of the primary contributors to pyright, Microsoft's Python type checker.)
If we were to adopt a change like this, I think we'd need to place some additional constraints on the constants. Not only would they need to be marked
Final, but they'd also need to be assigned an expression that is unambiguously a literal value — in other words, expressions that are allowed today within aLiteral. That excludes more complex expressions.For example, I would propose that none of these constants should be allowed in a
Literal.The problem with more complex expressions is that different type checkers can infer different types. If we constrain it to literal values only, there's no ambiguity or inconsistency between type checkers.
DevilXD commentedon Dec 16, 2021
I'm not sure if it's helpful or not, but PEP 586 already mentions this:
That being said, I just ran into this issue while trying to specify a variable type as
Literal[logging.NOTSET, logging.DEBUG], caused most likely by those being inferred by MyPy asintrather thanLiteral[0]andLiteral[10]respectively. Regarding the type beingintinstead ofLiteral, I've proposed a typeshed PR: python/typeshed#6610Having that PR accepted, MyPy would still be expected to accept
Literaltype aliases like so:karolyjozsa commentedon Jun 4, 2023
What if we take a different approach, and the Type parameterization is extended with Literals? I.e. if we could get this working?
christiansegercrantz commentedon Jan 31, 2025
This still seems like a topic. I have the following scenario:
Any update on the matter?