Skip to content

Fix attrs.frozen with enum.Enum subclasses#1574

Open
C1-BA-B1-F3 wants to merge 1 commit into
python-attrs:mainfrom
C1-BA-B1-F3:fix-enum-frozen-1287
Open

Fix attrs.frozen with enum.Enum subclasses#1574
C1-BA-B1-F3 wants to merge 1 commit into
python-attrs:mainfrom
C1-BA-B1-F3:fix-enum-frozen-1287

Conversation

@C1-BA-B1-F3

Copy link
Copy Markdown

Description

This PR fixes issue #1287 where attrs.frozen classes don't work with enum.Enum subclasses.

Problem

When creating an Enum subclass of a frozen attrs class, the frozen __setattr__ would prevent Enum from creating members, raising a FrozenInstanceError:

@attrs.frozen
class AttrsFrozen:
    a: int
    b: float

# This would raise FrozenInstanceError
class AttrsFrozenEnum(AttrsFrozen, Enum):
    X = (1, 2.0)
    Y = (3, 4.0)

Solution

Modified _frozen_setattrs to allow Enum instances to bypass the frozen check. Enum needs to set attributes like _name_, _value_, etc. during member creation, so we allow this by using object.__setattr__ for Enum instances.

Changes

  1. src/attr/_make.py: Added check in _frozen_setattrs to allow Enum instances to set attributes
  2. tests/test_functional.py: Added test cases for Enum + attrs interaction
  3. changelog.d/1287.change.md: Added changelog entry

Testing

  • Added tests for both attrs.define and attrs.frozen with Enum subclasses
  • Verified that frozen Enum subclasses are hashable
  • All existing tests pass

When creating an Enum subclass of a frozen attrs class, the frozen
__setattr__ would prevent Enum from creating members. Now, Enum
instances can bypass the frozen check to allow proper member creation.

Fixes python-attrs#1287
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant