Skip to content

enums_expansion.py test3 misses cases of the union-of-all-member = enum equivalency #2323

Description

@tapetersen

The Enum Literal Expansion section states that a complete union of all literal members is equivalent to the enum type. test3 in [conformance/tests/enums_expansion.py](https://github.com/python/typing/blob/main/conformance/tests/enums_expansion.py) is meant to cover this, but doesn't catch some cases.

Both pyright-1.1.411 and zuban-0.9.0 reject the following.

class Answer(enum.Enum):
    Yes = 1
    No = 2

a: Answer = ...
x: Literal[Answer.Yes, Answer.No] = a   # both report an error here

pyright: Type "Answer" is not assignable to declared type "Literal[...]"; zuban: Incompatible types in assignment.

Suggested test

def test4(a: Answer) -> None:
    x: Literal[Answer.Yes, Answer.No] = a  # E?
    assert_type(a, Literal[Answer.Yes, Answer.No])

This came up from the discussion at https://discuss.python.org/t/treat-literal-of-all-elements-of-enum-equal-to-the-enum-in-type-annotations/108317

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions