Skip to content

Commit ae04f8e

Browse files
committed
require dataclass's attributes to be encapsulated in a tuple
1 parent 9852ee4 commit ae04f8e

File tree

6 files changed

+29
-19
lines changed

6 files changed

+29
-19
lines changed

doc/source/changelog.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ v2.4.0 (2025-09-18)
77

88
* Implemented `Support Python 3.14, drop 3.8 support <https://github.com/bckohan/enum-properties/issues/97>`_
99

10+
.. warning::
11+
12+
If you are using a dataclass enum with additional properties, you will now need to encapsulate
13+
the dataclass attributes in a tuple as the first element of the enum member tuple. This syntax
14+
worked before but it was also lenient and took the first N elements of the member tuple that
15+
were not properties. This change makes the syntax more explicit and less error prone and was
16+
necessitated to support Python 3.14 because properties are determined after the enum member
17+
values are set on the class due to lazy evaluation of annotations.
18+
19+
1020
v2.3.0 (2025-03-29)
1121
===================
1222

doc/source/howto.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,13 @@ enumeration for the following reasons:**
277277

278278
EnumProperties also integrates with Enum's dataclass support!
279279
For example we can add a symmetric property to the Creature
280-
enumeration like so:
280+
enumeration like so (**note the tuple encapsulating the dataclass fields**):
281281

282282
.. literalinclude:: ../../tests/examples/howto_dataclass_integration.py
283283

284284

285285
.. _howto_members_and_aliases:
286+
286287
Get members and aliases
287288
-----------------------
288289

src/enum_properties/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,8 @@ def __setitem__(self, key, value):
538538
for values in self._ep_properties_.values():
539539
values.append(value[idx])
540540
idx += 1
541-
if num_vals == 1:
542-
value = value[0]
543-
else:
544-
value = value[0:num_vals]
541+
542+
value = value[0]
545543

546544
except TypeError as type_err:
547545
raise ValueError(

tests/annotations/test_flags.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,9 @@ def label(self):
409409

410410
def test_enum_dataclass_support(self):
411411
"""
412-
In 3.12, Enum added support for dataclass inheritance which offers similar functionality
413-
to enum-properties. This tests evaluates how these step on each other's toes.
412+
In 3.12, Enum added support for dataclass inheritance which offers similar
413+
functionality to enum-properties. This tests evaluates how these step on
414+
each other's toes.
414415
415416
From the std lib docs example:
416417
"""
@@ -431,7 +432,7 @@ class CreatureDataHashableMixin:
431432
class CreatureHybrid(CreatureDataMixin, EnumProperties):
432433
kingdom: Annotated[str, Symmetric()]
433434

434-
BEETLE = "small", 6, False, "insect"
435+
BEETLE = ("small", 6, False), "insect"
435436
DOG = (
436437
(
437438
"medium",
@@ -456,7 +457,7 @@ class CreatureHybrid(CreatureDataMixin, EnumProperties):
456457
class CreatureHybridSpecialized(CreatureDataMixin, EnumProperties):
457458
kingdom: Annotated[str, Symmetric()]
458459

459-
BEETLE = "small", 6, "insect"
460+
BEETLE = ("small", 6), "insect"
460461
DOG = ("medium", 4, False), "mammal"
461462

462463
@specialize(BEETLE)
@@ -492,7 +493,7 @@ def function(self):
492493
class CreatureHybridSpecialized(CreatureDataHashableMixin, EnumProperties):
493494
kingdom: Annotated[str, Symmetric()]
494495

495-
BEETLE = "small", 6, "insect"
496+
BEETLE = ("small", 6), "insect"
496497
DOG = (
497498
(
498499
"medium",

tests/examples/howto_dataclass_integration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class Creature(CreatureDataMixin, EnumProperties):
1414

1515
kingdom: t.Annotated[str, Symmetric()]
1616

17-
BEETLE = 'small', 6, False, 'insect'
18-
DOG = 'medium', 4, 'animal'
17+
BEETLE = ('small', 6, False), 'insect'
18+
DOG = ('medium', 4), 'animal'
1919

2020

2121
# you can now access the dataclass fields on the enumeration values

tests/legacy/test_flags.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ class CreatureDataHashableMixin:
400400
tail: bool = field(repr=False, default=True)
401401

402402
class Creature(CreatureDataMixin, EnumProperties):
403-
BEETLE = "small", 6
404-
DOG = "medium", 4
403+
BEETLE = ("small", 6)
404+
DOG = ("medium", 4)
405405

406406
self.assertEqual(Creature.BEETLE.size, "small")
407407
self.assertEqual(Creature.BEETLE.legs, 6)
@@ -412,8 +412,8 @@ class Creature(CreatureDataMixin, EnumProperties):
412412
self.assertEqual(Creature.DOG.tail, True)
413413

414414
class CreatureEP(CreatureDataMixin, EnumProperties):
415-
BEETLE = "small", 6
416-
DOG = "medium", 4, False
415+
BEETLE = ("small", 6)
416+
DOG = ("medium", 4, False)
417417

418418
self.assertEqual(CreatureEP.BEETLE.size, "small")
419419
self.assertEqual(CreatureEP.BEETLE.legs, 6)
@@ -424,7 +424,7 @@ class CreatureEP(CreatureDataMixin, EnumProperties):
424424
self.assertEqual(CreatureEP.DOG.tail, False)
425425

426426
class CreatureHybrid(CreatureDataMixin, EnumProperties, s("kingdom")):
427-
BEETLE = "small", 6, False, "insect"
427+
BEETLE = ("small", 6, False), "insect"
428428
DOG = (
429429
(
430430
"medium",
@@ -449,7 +449,7 @@ class CreatureHybrid(CreatureDataMixin, EnumProperties, s("kingdom")):
449449
class CreatureHybridSpecialized(
450450
CreatureDataMixin, EnumProperties, s("kingdom")
451451
):
452-
BEETLE = "small", 6, "insect"
452+
BEETLE = ("small", 6), "insect"
453453
DOG = ("medium", 4, False), "mammal"
454454

455455
@specialize(BEETLE)
@@ -485,7 +485,7 @@ def function(self):
485485
class CreatureHybridSpecialized(
486486
CreatureDataHashableMixin, EnumProperties, s("kingdom")
487487
):
488-
BEETLE = "small", 6, "insect"
488+
BEETLE = ("small", 6), "insect"
489489
DOG = (
490490
(
491491
"medium",

0 commit comments

Comments
 (0)