-
-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
Equality of typing.Literal depends on the order of arguments #86511
Comments
PEP-586 specifies that
is equivalent to
Since the equality of Unions doesn't take into account the order of arguments, Literals parametrized with multiple arguments should not be order dependent either. However they seem to: >>> Literal[1, 2] == Literal[2, 1]
False Compare with the equivalent form: >>> Union[Literal[1], Literal[2]] == Union[Literal[2], Literal[1]]
True In addition to that, the PEP specifies that nested Literals should be equivalent to the flattened version (https://www.python.org/dev/peps/pep-0586/#legal-parameters-for-literal-at-type-check-time). This section is titled "Legal parameters for Literal at type check time" but since the PEP doesn't specify runtime behavior differently, I think it makes sense to assume it is the same. It seems to be different though: >>> Literal[Literal[1, 2], 3]
typing.Literal[typing.Literal[1, 2], 3]
>>> Literal[Literal[1, 2], 3] == Literal[1, 2, 3]
False Also the flattening follows from the above definition |
Probably the implementation focused on static typing, not runtime checking. Can you come up with a PR for a fix? |
Yeah, I think it makes sense to de-dupe args for Literal. |
I recommend adding a whatsnew entry too. You can just add it to this issue. Interestingly you’ll probably need two separate ones, for 3.9 and 3.10. That would become two separate PRs for master, the 3.9 one to be backported. |
We need to fix this to make __hash__ match __eq__. |
Thanks everyone! Can I close this now? |
Looks like everything merged, we can close this issue |
Mainly to clarify, this is fixed in Python 3.9 and above, but won't be fixed in Python 3.8, right?
|
@alick yes. To be specific, 3.9.1 and above. 3.9.0 still has the old |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: