-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Let "Literal" accept another Literal type variable #10026
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm not sure this is a bug: the PEP notes that "", and from typing import Literal
var = Literal[Literal[1]] = 1 And in |
while this may be not a bug, I thik this design decision limits the usefulness of from typing import Final, Literal
MY_CONST_A: Final = 1
MY_CONST_B: Final = 2 from now on I would like to avoid using directly the values let's say I want to define a function that can return either of that consts, what the return type should be? def foo() -> Literal[MY_CONST_A, MY_CONST_B]:
return MY_CONST_A if ... else MY_CONST_B is not supported by mypy, and I need to set |
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 For example, I would propose that none of these constants should be allowed in a MY_CONST_C: Final = my_func()
MY_CONST_D: Final = 1 + 2
MY_CONST_E: Final = 1 if a else 2
MY_CONST_F: Final = (1, 2, 3)[0]
MY_CONST_G: Final = ... 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. |
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 Having that PR accepted, MyPy would still be expected to accept NOTSET: Literal[0]
DEBUG: Literal[10]
# currently valid
logging_level: Literal[Literal[0], Literal[10]]
# currently invalid
logging_level: Literal[NOTSET, DEBUG] |
What if we take a different approach, and the Type parameterization is extended with Literals? I.e. if we could get this working?
|
This still seems like a topic. I have the following scenario: from typing import Final, Literal
TOTAL: Final = "total"
Lit = Literal["sub_sum_1", "sub_sum_2", TOTAL]
val: Lit = "sub_sum_1" # error: Parameter 3 of Literal[...] is invalid [valid-type]
val_list: list[Lit] = ["sub_sum_1", "sub_sum_2", TOTAL] # error: Parameter 3 of Literal[...] is invalid [valid-type] Any update on the matter? |
Feature
Currently
Literal
doesn't accept anotherLiteral
orFinal[Literal]
. For example:outputs:
Pitch
This is particularly useful when there are some defined constants and people refer to those constants rather than the actual value. For example, some of these socket constants: https://docs.python.org/3/library/socket.html#socket.BDADDR_ANY
The text was updated successfully, but these errors were encountered: