classification
Title: Equality of typing.Literal depends on the order of arguments
Type: behavior Stage: resolved
Components: Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Dominik V., gvanrossum, kj, levkivskyi, miss-islington, uriyyo
Priority: normal Keywords: patch

Created on 2020-11-13 14:11 by Dominik V., last changed 2020-11-20 17:11 by gvanrossum. 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 (15)
msg380888 - (view) Author: Dominik V. (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) * 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) * Date: 2020-11-20 09:09
Looks like everything merged, we can close this issue
History
Date User Action Args
2020-11-20 17:11:24gvanrossumsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-11-20 09:09:44uriyyosetmessages: + msg381469
2020-11-19 17:59:20miss-islingtonsetmessages: + msg381448
2020-11-19 17:38:02gvanrossumsetmessages: + msg381446
2020-11-19 17:37:42miss-islingtonsetpull_requests: + pull_request22304
2020-11-19 17:37:33miss-islingtonsetmessages: + msg381445
2020-11-19 16:51:09miss-islingtonsetmessages: + msg381441
2020-11-19 16:17:55miss-islingtonsetpull_requests: + pull_request22301
2020-11-19 16:17:46miss-islingtonsetmessages: + msg381439
2020-11-19 05:09:43kjsetpull_requests: + pull_request22279
2020-11-19 04:44:36gvanrossumsetmessages: + msg381388
2020-11-19 03:18:25kjsetpull_requests: + pull_request22278
2020-11-18 23:51:12uriyyosetstage: needs patch -> patch review
pull_requests: + pull_request22275
2020-11-18 23:22:17gvanrossumsetstatus: closed -> open
resolution: fixed -> (no value)
messages: + msg381380

stage: resolved -> needs patch
2020-11-17 15:28:24gvanrossumsetstatus: open -> closed
resolution: fixed
messages: + msg381249

stage: patch review -> resolved
2020-11-17 15:23:43miss-islingtonsetmessages: + msg381247
2020-11-17 07:52:12uriyyosetpull_requests: + pull_request22224
2020-11-17 02:23:32miss-islingtonsetnosy: + miss-islington
messages: + msg381204
2020-11-16 01:03:28gvanrossumsetmessages: + msg381050
2020-11-15 16:01:46kjsetnosy: + kj
messages: + msg381018
2020-11-15 13:25:31uriyyosetkeywords: + patch
nosy: + uriyyo

pull_requests: + pull_request22185
stage: needs patch -> patch review
2020-11-13 15:46:53gvanrossumsetstage: needs patch
messages: + msg380904
versions: + Python 3.8, Python 3.10
2020-11-13 14:26:46serhiy.storchakasetnosy: + gvanrossum, levkivskyi
2020-11-13 14:11:55Dominik V.create