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
Equality of typing.Literal depends on the order of arguments #86511
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:
assignee=Noneclosed_at=<Date2020-11-20.17:11:24.442>created_at=<Date2020-11-13.14:11:55.639>labels= ['type-bug', '3.8', '3.9', '3.10']
title='Equality of typing.Literal depends on the order of arguments'updated_at=<Date2021-10-13.10:26:49.678>user='https://github.com/Dominik1123'
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:
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:
Also the flattening follows from the above definition Literal[v1, v2, v3] == Union[Literal[v1], Literal[v2], Literal[v3]] and the fact that Unions are flattened.
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.
Mainly to clarify, this is fixed in Python 3.9 and above, but won't be fixed in Python 3.8, right?
~$ python3.8
Python 3.8.11 (default, Jul 24 2021, 13:24:12)
[Clang 12.0.5 (clang-1205.0.22.11)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Could not open PYTHONSTARTUP
FileNotFoundError: [Errno 2] No such file or directory: '/Users/taoz/.pythonstartup'
>>> from typing import Literal
>>> Literal[1,2]==Literal[2,1]
False
>>>
Activity
Dominik1123 commentedon Nov 13, 2020
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] FalseCompare with the equivalent form:
>>> Union[Literal[1], Literal[2]] == Union[Literal[2], Literal[1]] TrueIn 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] FalseAlso the flattening follows from the above definition
Literal[v1, v2, v3] == Union[Literal[v1], Literal[v2], Literal[v3]]and the fact that Unions are flattened.gvanrossum commentedon Nov 13, 2020
Probably the implementation focused on static typing, not runtime checking.
Can you come up with a PR for a fix?
Fidget-Spinner commentedon Nov 15, 2020
@guido and @ivan,
Should Literals de-duplicate values in its __args__ similar to Union? PEP-586 states that:
Naturally, a Union de-duplicates values, so I was wondering if Literal should follow.
gvanrossum commentedon Nov 16, 2020
Yeah, I think it makes sense to de-dupe args for Literal.
miss-islington commentedon Nov 17, 2020
New changeset f03d318 by Yurii Karabas in branch 'master':
bpo-42345: Fix three issues with typing.Literal parameters (GH-23294)
f03d318
miss-islington commentedon Nov 17, 2020
New changeset ac472b3 by Yurii Karabas in branch '3.9':
[3.9] bpo-42345: Fix three issues with typing.Literal parameters (GH-23294) (GH-23335)
ac472b3
gvanrossum commentedon Nov 17, 2020
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.
1 remaining item
gvanrossum commentedon Nov 18, 2020
We need to fix this to make __hash__ match __eq__.
gvanrossum commentedon Nov 19, 2020
New changeset 4687338 by kj in branch 'master':
bpo-42345: Add whatsnew for typing.Literal in 3.10 (GH-23385)
4687338
miss-islington commentedon Nov 19, 2020
New changeset 1b54077 by Yurii Karabas in branch 'master':
bpo-42345: Fix hash implementation of typing.Literal (GH-23383)
1b54077
miss-islington commentedon Nov 19, 2020
New changeset 2acd9d0 by Miss Islington (bot) in branch '3.9':
bpo-42345: Fix hash implementation of typing.Literal (GH-23383)
2acd9d0
miss-islington commentedon Nov 19, 2020
New changeset e1dc0db by kj in branch 'master':
bpo-42345: Add whatsnew and versionchanged for typing.Literal in 3.9 (GH-23386)
e1dc0db
gvanrossum commentedon Nov 19, 2020
Thanks everyone! Can I close this now?
miss-islington commentedon Nov 19, 2020
New changeset 1051ca4 by Miss Islington (bot) in branch '3.9':
bpo-42345: Add whatsnew and versionchanged for typing.Literal in 3.9 (GH-23386)
1051ca4
uriyyo commentedon Nov 20, 2020
Looks like everything merged, we can close this issue
alick commentedon Oct 12, 2021
Mainly to clarify, this is fixed in Python 3.9 and above, but won't be fixed in Python 3.8, right?
Fidget-Spinner commentedon Oct 13, 2021
@alick yes. To be specific, 3.9.1 and above. 3.9.0 still has the old
behavior.