Issue 42345: Equality of typing.Literal depends on the order of arguments (original) (raw)

Created on 2020-11-13 14:11 by Dominik V., last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 23294 merged uriyyo,2020-11-15 13:25
PR 23335 merged uriyyo,2020-11-17 07:52
PR 23383 merged uriyyo,2020-11-18 23:51
PR 23385 merged kj,2020-11-19 03:18
PR 23386 merged kj,2020-11-19 05:09
PR 23408 merged miss-islington,2020-11-19 16:17
PR 23411 merged miss-islington,2020-11-19 17:37
Messages (17)
msg380888 - (view) Author: Dominik Vilsmeier (Dominik V.) * Date: 2020-11-13 14:11
[PEP 586](https://www.python.org/dev/peps/pep-0586/#shortening-unions-of-literals) specifies that Literal[v1, v2, v3] is equivalent to Union[Literal[v1], Literal[v2], Literal[v3]] 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 `Literal[v1, v2, v3] == Union[Literal[v1], Literal[v2], Literal[v3]]` and the fact that Unions are flattened.
msg380904 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-11-13 15:46
Probably the implementation focused on static typing, not runtime checking. Can you come up with a PR for a fix?
msg381018 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2020-11-15 16:01
@Guido and @Ivan, Should Literals de-duplicate values in its __args__ similar to Union? PEP 586 states that: > Literal[v1, v2, v3] is equivalent to Union[Literal[v1], Literal[v2], Literal[v3]] Naturally, a Union de-duplicates values, so I was wondering if Literal should follow.
msg381050 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-11-16 01:03
Yeah, I think it makes sense to de-dupe args for Literal.
msg381204 - (view) Author: miss-islington (miss-islington) Date: 2020-11-17 02:23
New changeset f03d318ca42578e45405717aedd4ac26ea52aaed by Yurii Karabas in branch 'master': bpo-42345: Fix three issues with typing.Literal parameters (GH-23294) https://github.com/python/cpython/commit/f03d318ca42578e45405717aedd4ac26ea52aaed
msg381247 - (view) Author: miss-islington (miss-islington) Date: 2020-11-17 15:23
New changeset ac472b316cbb22ab8b750a474e991b46d1e92e15 by Yurii Karabas in branch '3.9': [3.9] bpo-42345: Fix three issues with typing.Literal parameters (GH-23294) (GH-23335) https://github.com/python/cpython/commit/ac472b316cbb22ab8b750a474e991b46d1e92e15
msg381249 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-11-17 15:28
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.
msg381380 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-11-18 23:22
We need to fix this to make __hash__ match __eq__.
msg381388 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-11-19 04:44
New changeset 4687338d0ed46e1f5f5060536becf8a96496bae7 by kj in branch 'master': bpo-42345: Add whatsnew for typing.Literal in 3.10 (GH-23385) https://github.com/python/cpython/commit/4687338d0ed46e1f5f5060536becf8a96496bae7
msg381439 - (view) Author: miss-islington (miss-islington) Date: 2020-11-19 16:17
New changeset 1b54077ff6f5c1379e097e9f8e8648da9826d6ec by Yurii Karabas in branch 'master': bpo-42345: Fix hash implementation of typing.Literal (GH-23383) https://github.com/python/cpython/commit/1b54077ff6f5c1379e097e9f8e8648da9826d6ec
msg381441 - (view) Author: miss-islington (miss-islington) Date: 2020-11-19 16:51
New changeset 2acd9d0c6ccf0b4360e3b63beddb97996bcb9bb1 by Miss Islington (bot) in branch '3.9': bpo-42345: Fix hash implementation of typing.Literal (GH-23383) https://github.com/python/cpython/commit/2acd9d0c6ccf0b4360e3b63beddb97996bcb9bb1
msg381445 - (view) Author: miss-islington (miss-islington) Date: 2020-11-19 17:37
New changeset e1dc0db8c7cb8c4d7343e051ba85146b375bb8e0 by kj in branch 'master': bpo-42345: Add whatsnew and versionchanged for typing.Literal in 3.9 (GH-23386) https://github.com/python/cpython/commit/e1dc0db8c7cb8c4d7343e051ba85146b375bb8e0
msg381446 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-11-19 17:38
Thanks everyone! Can I close this now?
msg381448 - (view) Author: miss-islington (miss-islington) Date: 2020-11-19 17:59
New changeset 1051ca4d974ebb6448f58b661aa28f8aff325ed3 by Miss Islington (bot) in branch '3.9': bpo-42345: Add whatsnew and versionchanged for typing.Literal in 3.9 (GH-23386) https://github.com/python/cpython/commit/1051ca4d974ebb6448f58b661aa28f8aff325ed3
msg381469 - (view) Author: Yurii Karabas (uriyyo) * (Python triager) Date: 2020-11-20 09:09
Looks like everything merged, we can close this issue
msg403752 - (view) Author: Alick Zhao (alick) Date: 2021-10-12 18:54
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 >>> ```
msg403818 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2021-10-13 10:26
@Alick yes. To be specific, 3.9.1 and above. 3.9.0 still has the old behavior.
History
Date User Action Args
2022-04-11 14:59:38 admin set github: 86511
2021-10-13 10:26:49 kj set messages: +
2021-10-12 18:54:48 alick set nosy: + alickmessages: +
2020-11-20 17:11:24 gvanrossum set status: open -> closedresolution: fixedstage: patch review -> resolved
2020-11-20 09:09:44 uriyyo set messages: +
2020-11-19 17:59:20 miss-islington set messages: +
2020-11-19 17:38:02 gvanrossum set messages: +
2020-11-19 17:37:42 miss-islington set pull_requests: + <pull%5Frequest22304>
2020-11-19 17:37:33 miss-islington set messages: +
2020-11-19 16:51:09 miss-islington set messages: +
2020-11-19 16:17:55 miss-islington set pull_requests: + <pull%5Frequest22301>
2020-11-19 16:17:46 miss-islington set messages: +
2020-11-19 05:09:43 kj set pull_requests: + <pull%5Frequest22279>
2020-11-19 04:44:36 gvanrossum set messages: +
2020-11-19 03🔞25 kj set pull_requests: + <pull%5Frequest22278>
2020-11-18 23:51:12 uriyyo set stage: needs patch -> patch reviewpull_requests: + <pull%5Frequest22275>
2020-11-18 23:22:17 gvanrossum set status: closed -> openresolution: fixed -> (no value)messages: + stage: resolved -> needs patch
2020-11-17 15:28:24 gvanrossum set status: open -> closedresolution: fixedmessages: + stage: patch review -> resolved
2020-11-17 15:23:43 miss-islington set messages: +
2020-11-17 07:52:12 uriyyo set pull_requests: + <pull%5Frequest22224>
2020-11-17 02:23:32 miss-islington set nosy: + miss-islingtonmessages: +
2020-11-16 01:03:28 gvanrossum set messages: +
2020-11-15 16:01:46 kj set nosy: + kjmessages: +
2020-11-15 13:25:31 uriyyo set keywords: + patchnosy: + uriyyopull_requests: + <pull%5Frequest22185>stage: needs patch -> patch review
2020-11-13 15:46:53 gvanrossum set stage: needs patchmessages: + versions: + Python 3.8, Python 3.10
2020-11-13 14:26:46 serhiy.storchaka set nosy: + gvanrossum, levkivskyi
2020-11-13 14:11:55 Dominik V. create