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

Attribute resolution of an Enum member that is an instance of a subclass of the mixin class does not follow MRO #116487

Open
blhsing opened this issue Mar 8, 2024 · 2 comments
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@blhsing
Copy link
Contributor

blhsing commented Mar 8, 2024

Bug report

Bug description:

As reported in https://stackoverflow.com/questions/78120133/python-enum-of-classes-with-polymorphism .

Since the idea of a mixin class to an Enum is to make the members behave like instances of the mixin class, I think it's reasonable to expect that attribute resolution of a member whose value is an instance of a subclass of the mixin class follows MRO as well. Currently the attribute resolution appears to stick to the base mixin class.

Minimal reproducible example:

from enum import Enum

class Base:
    name = 'base'

class Child(Base):
    name = 'child'

class BaseEnum(Base, Enum):
    FOO = Child()

print(BaseEnum.FOO.name) # outputs 'base' when 'child' is expected
print(BaseEnum.FOO.value.name) # outputs 'child' as expected

CPython versions tested on:

3.12

Operating systems tested on:

Linux, Windows

@blhsing blhsing added the type-bug An unexpected behavior, bug, or error label Mar 8, 2024
@blhsing blhsing changed the title Method of an Enum member with a value of a subclass of the mixin class does not bind intuitively Mar 8, 2024
@blhsing blhsing changed the title Method of an Enum member that is an instance of a subclass of the mixin class does not bind intuitively Mar 8, 2024
@blhsing blhsing changed the title Method of an Enum member that is an instance of a subclass of the mixin class does not resolve according to MRO Mar 8, 2024
@ethanfurman
Copy link
Member

ethanfurman commented Mar 8, 2024

>>> isinstance(BaseEnum.FOO, Base)
True

>>> isinstance(BaseEnum.FOO, Child)
False

Child is not in BaseEnums mro.

@ethanfurman ethanfurman self-assigned this Mar 8, 2024
@blhsing
Copy link
Contributor Author

blhsing commented Mar 8, 2024

>>> isinstance(BaseEnum.FOO, Base)
True

>>> isinstance(BaseEnum.FOO, Child)
False

Child is not in BaseEnums mro.

Right. It isn't clear why the Activations class in the SO question needs to be an Enum at all in the first place, and I can't think of a good real-world use case myself. I simply concur with the OP of the SO question that the behavior is somewhat unexpected/counter-intuitive. That's all.

Just left a comment in the SO question requesting clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
2 participants