[issue42345] Equality of typing.Literal depends on the order of arguments

Dominik V. report at bugs.python.org
Fri Nov 13 09:11:55 EST 2020


New submission from Dominik V. <dominik.vilsmeier1123 at gmail.com>:

[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.

----------
messages: 380888
nosy: Dominik V.
priority: normal
severity: normal
status: open
title: Equality of typing.Literal depends on the order of arguments
type: behavior
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42345>
_______________________________________


More information about the Python-bugs-list mailing list