This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Dominik V.
Recipients Dominik V.
Date 2020-11-13.14:11:55
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1605276715.65.0.853257843244.issue42345@roundup.psfhosted.org>
In-reply-to
Content
[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.
History
Date User Action Args
2020-11-13 14:11:55Dominik V.setrecipients: + Dominik V.
2020-11-13 14:11:55Dominik V.setmessageid: <1605276715.65.0.853257843244.issue42345@roundup.psfhosted.org>
2020-11-13 14:11:55Dominik V.linkissue42345 messages
2020-11-13 14:11:55Dominik V.create