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) *  |
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) *  |
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) *  |
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) *  |
Date: 2020-11-18 23:22 |
We need to fix this to make __hash__ match __eq__. |
|
|
msg381388 - (view) |
Author: Guido van Rossum (gvanrossum) *  |
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) *  |
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 |
|
|
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) *  |
Date: 2021-10-13 10:26 |
@Alick yes. To be specific, 3.9.1 and above. 3.9.0 still has the old behavior. |
|
|