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

Match on Enum with dataclass decorator #114803

Closed
bedapisl opened this issue Jan 31, 2024 · 7 comments
Closed

Match on Enum with dataclass decorator #114803

bedapisl opened this issue Jan 31, 2024 · 7 comments
Labels
stdlib Python modules in the Lib dir

Comments

@bedapisl
Copy link

bedapisl commented Jan 31, 2024

Bug report

Bug description:

Hello,
when Enum with @DataClass decorator is used in a match statement, the results seem to be wrong. Consider this example:

from enum import Enum, auto
from dataclasses import dataclass

@dataclass
class Color(str, Enum):
    Red = auto()
    Green = auto()

mode = Color.Green

match mode:
    case Color.Red:
        print(f"Matched RED, actual {mode}")
    case Color.Green:
        print(f"Matched GREEN, actual {mode}")
    case _:
        assert False

I think it should print:

Matched GREEN, actual Color.Green

but it prints:

Matched RED, actual Color.Green

I would suggest this either prints the expected result, or at least produces some error instead of the current behaviour.

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Linked PRs

@bedapisl bedapisl added the type-bug An unexpected behavior, bug, or error label Jan 31, 2024
@sobolevn
Copy link
Member

Can you please explain your use-case a little bit more? It is uncommon to use @dataclass on Enum

@ericvsmith
Copy link
Member

ericvsmith commented Jan 31, 2024

Agreed with @sobolevn: it's not clear why you'd want to do this. But if it is important, we'd probably need to add a parameter to @dataclass to not set __match_args__, which I assume is what causes the problem.

@AlexWaygood AlexWaygood added the stdlib Python modules in the Lib dir label Jan 31, 2024
@ethanfurman
Copy link
Member

@ericvsmith, I tried deleting __match_args__ from Color, and got the same results. Also, turning an enum into a dataclass breaks basic behavior:

>>> Color.Red == Color.Green
True

Perhaps the better solution is to reject enums as dataclasses? I am unclear on what benefit a dataclass-enum would offer, since all members are already defined by the time the decorator gets control.

@brandtbucher
Copy link
Member

brandtbucher commented Jan 31, 2024

I don't think __match_args__ is a problem here, since there are no class patterns (we're matching against attributes of the Color class, not instances of it).

Honestly, this isn't an issue with match... the issue is that none of us have any idea what an @dataclass-decorated Enum is supposed to mean. 🙃

@bedapisl
Copy link
Author

bedapisl commented Feb 1, 2024

Hello,
I don't have a direct use case for this.

Perhaps the better solution is to reject enums as dataclasses?

That makes sense to me.

@sobolevn
Copy link
Member

sobolevn commented Feb 1, 2024

Perhaps the better solution is to reject enums as dataclasses?

I can clearly see that this is reasonable. However, this will come with several downsides:

  1. We have to check all mro for Enum and slow things down for all use-cases
  2. This error seems to be rather rare to enforce it for everyone
  3. There might be other custom magic / metaclass-heavy classes that won't work with @dataclass that we still won't detect, so why make special cases?

So, maybe just close the issue for now? If this problem prooves to be common, we can get back to it.

@ethanfurman
Copy link
Member

Perhaps a doc update stating that the proper way to use dataclass and Enum together is with an existing dataclass as a mixin to an enum.

The link in the enum HowTo document is .. _enum-dataclass-support:.

sobolevn added a commit to sobolevn/cpython that referenced this issue Feb 2, 2024
ethanfurman added a commit that referenced this issue Feb 3, 2024
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
@ethanfurman ethanfurman removed the type-bug An unexpected behavior, bug, or error label Feb 12, 2024
fsc-eriker pushed a commit to fsc-eriker/cpython that referenced this issue Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir
Projects
None yet
Development

No branches or pull requests

6 participants