Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
adriangb opened this issue Apr 17, 2023 · 5 comments
Labels
topic-typing type-bug An unexpected behavior, bug, or error

Comments

@adriangb
Copy link
Contributor

adriangb commented Apr 17, 2023

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

@adriangb adriangb added the type-bug An unexpected behavior, bug, or error label Apr 17, 2023
@JelleZijlstra
Copy link
Member

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

@adriangb
Copy link
Contributor Author

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

@adriangb
Copy link
Contributor Author

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?

@JelleZijlstra
Copy link
Member

Yes, that makes sense to do.

@sobolevn
Copy link
Member

sobolevn commented Apr 23, 2023

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 :)

@sobolevn sobolevn reopened this Apr 23, 2023
sobolevn added a commit to sobolevn/cpython that referenced this issue Apr 23, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Apr 23, 2023
…iteral`s (pythonGH-103706)

(cherry picked from commit 5041c2b)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
AlexWaygood pushed a commit that referenced this issue Apr 23, 2023
…Literal`s (GH-103706) (#103720)

gh-103592: Add tests of `Literal` with `Enum` and `Union` of `Literal`s (GH-103706)
(cherry picked from commit 5041c2b)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants