GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Incorrect deduplication of Enum values in `Union[Literal[1], Literal[SomeEnum.one]]` · Issue #103592 · python/cpython

Incorrect deduplication of Enum values in Union[Literal[1], Literal[SomeEnum.one]] #103592

New issue
Copy link
New issue
Copy link
Closed
Closed
Incorrect deduplication of Enum values in Union[Literal[1], Literal[SomeEnum.one]]#103592
Copy link
Labels
topic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@adriangb
adriangb
opened on Apr 17, 2023
Issue body actions

This was fixed on 3.9 (by #23294 or followups). Here's a test / example:

from enum import Enum
from typing import Literal, Union, get_args


class MyEnum(int, Enum):
    foo = 1

tp = Union[Literal[MyEnum.foo], Literal[1]]
assert len(get_args(tp)) == 2, get_args(tp)

tp = Union[Literal[1], Literal[MyEnum.foo]]
assert len(get_args(tp)) == 2, get_args(tp)

These assertions fail on 3.8 since Union deduplicates both Literals into the same thing. The reason you need Literal here is because enum values are values not types so they can only appear in a Union if they're also within a Literal.

As per #23294 (comment) maybe the answer here is to not do anything. And in fact I looked into back porting and it was too big of a change to be called a back port. But I didn't see this specific issue w/ Enum and Union reported, so I at least wanted to report it and confirm that it is a bug.

Linked PRs

  • gh-103592: Add tests of Literal with Enum and Union of Literals #103706
  • [3.11] gh-103592: Add tests of Literal with Enum and Union of Literals (GH-103706) #103720

Activity

  1. adriangb
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Apr 17, 2023
  2. JelleZijlstra commented on Apr 17, 2023

    @JelleZijlstra
    JelleZijlstra
    on Apr 17, 2023
    Member
    More actions

    3.8 is in security fix-only mode, so it's too late to fix this.

    Reacted by Adrian Garcia Badaracco
  3. adriangb commented on Apr 17, 2023

    @adriangb
    adriangb
    on Apr 17, 2023
    ContributorAuthor
    More actions

    Makes sense. I'll just close the issue then.

  4. adriangb
    closed this as completedon Apr 17, 2023
  5. adriangb commented on Apr 17, 2023

    @adriangb
    adriangb
    on Apr 17, 2023
    ContributorAuthor
    More actions

    Do you think we can change https://github.com/python/typing_extensions/blob/501a00e4701185ec93220155a0caa3da71d327e8/src/typing_extensions.py#L264-L268 to use the typing-extensions implementation for 3.8 as well?

  6. JelleZijlstra commented on Apr 17, 2023

    @JelleZijlstra
    JelleZijlstra
    on Apr 17, 2023
    Member
    More actions

    Yes, that makes sense to do.

  7. sobolevn commented on Apr 23, 2023

    @sobolevn
    sobolevn
    on Apr 23, 2023
    Member
    More actions

    test_typing.py police is here 🚨

    This is not tested right now, in fact, Literal with enum is never tested at all.
    Moreover, we don't have a single Union[Literal, Literal, ...] test as well.

    Thanks a lot @adriangb for raising this.
    PR with the fix is ready :)

  8. sobolevn
    reopened this on Apr 23, 2023
  9. sobolevn
    added a commit that references this issue on Apr 23, 2023

    pythongh-103592: Add tests of `Literal` with `Enum` and `Union` of `L…

    e9ccdcc
  10. AlexWaygood
    added
    topic-typing
    on Apr 23, 2023
  11. AlexWaygood
    added a commit that references this issue on Apr 23, 2023

    gh-103592: Add tests of `Literal` with `Enum` and `Union` of `Literal…

    5041c2b
  12. miss-islington
    added a commit that references this issue on Apr 23, 2023

    pythongh-103592: Add tests of `Literal` with `Enum` and `Union` of `L…

    14db874
  13. AlexWaygood
    added a commit that references this issue on Apr 23, 2023

    [3.11] gh-103592: Add tests of `Literal` with `Enum` and `Union` of `…

    4e9635e
  14. AlexWaygood
    closed this as completedon Apr 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.