Skip to content

Commit 8feb7ab

Browse files
authored
gh-93464: [Enum] fix auto() failure during multiple assignment (GH-99148)
* fix auto() failure during multiple assignment i.e. `ONE = auto(), 'text'` will now have `ONE' with the value of `(1, 'text')`. Before it would have been `(<an auto instance>, 'text')`
1 parent 586b07e commit 8feb7ab

File tree

4 files changed

+82
-11
lines changed

4 files changed

+82
-11
lines changed

Doc/library/enum.rst

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ Data Types
242242

243243
Member values can be anything: :class:`int`, :class:`str`, etc.. If
244244
the exact value is unimportant you may use :class:`auto` instances and an
245-
appropriate value will be chosen for you. Care must be taken if you mix
246-
:class:`auto` with other values.
245+
appropriate value will be chosen for you. See :class:`auto` for the
246+
details.
247247

248248
.. attribute:: Enum._ignore_
249249

@@ -778,7 +778,16 @@ Utilities and Decorators
778778
For *Enum* and *IntEnum* that appropriate value will be the last value plus
779779
one; for *Flag* and *IntFlag* it will be the first power-of-two greater
780780
than the last value; for *StrEnum* it will be the lower-cased version of the
781-
member's name.
781+
member's name. Care must be taken if mixing *auto()* with manually specified
782+
values.
783+
784+
*auto* instances are only resolved when at the top level of an assignment:
785+
786+
* ``FIRST = auto()`` will work (auto() is replaced with ``1``);
787+
* ``SECOND = auto(), -2`` will work (auto is replaced with ``2``, so ``2, -2`` is
788+
used to create the ``SECOND`` enum member;
789+
* ``THREE = [auto(), -3]`` will *not* work (``<auto instance>, -3`` is used to
790+
create the ``THREE`` enum member)
782791

783792
``_generate_next_value_`` can be overridden to customize the values used by
784793
*auto*.

Lib/enum.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ class auto:
171171
"""
172172
Instances are replaced with an appropriate value in Enum class suites.
173173
"""
174-
value = _auto_null
174+
def __init__(self, value=_auto_null):
175+
self.value = value
175176

176177
def __repr__(self):
177178
return "auto(%r)" % self.value
@@ -427,15 +428,31 @@ def __setitem__(self, key, value):
427428
elif isinstance(value, member):
428429
# unwrap value here -- it will become a member
429430
value = value.value
431+
non_auto_store = True
432+
single = False
430433
if isinstance(value, auto):
431-
if value.value == _auto_null:
432-
value.value = self._generate_next_value(
433-
key, 1, len(self._member_names), self._last_values[:],
434-
)
435-
self._auto_called = True
436-
value = value.value
434+
single = True
435+
value = (value, )
436+
if isinstance(value, tuple):
437+
auto_valued = []
438+
for v in value:
439+
if isinstance(v, auto):
440+
non_auto_store = False
441+
if v.value == _auto_null:
442+
v.value = self._generate_next_value(
443+
key, 1, len(self._member_names), self._last_values[:],
444+
)
445+
self._auto_called = True
446+
v = v.value
447+
self._last_values.append(v)
448+
auto_valued.append(v)
449+
if single:
450+
value = auto_valued[0]
451+
else:
452+
value = tuple(auto_valued)
437453
self._member_names[key] = None
438-
self._last_values.append(value)
454+
if non_auto_store:
455+
self._last_values.append(value)
439456
super().__setitem__(key, value)
440457

441458
def update(self, members, **more_members):

Lib/test/test_enum.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4116,6 +4116,50 @@ class Dupes(Enum):
41164116
third = auto()
41174117
self.assertEqual([Dupes.first, Dupes.second, Dupes.third], list(Dupes))
41184118

4119+
def test_multiple_auto_on_line(self):
4120+
class Huh(Enum):
4121+
ONE = auto()
4122+
TWO = auto(), auto()
4123+
THREE = auto(), auto(), auto()
4124+
self.assertEqual(Huh.ONE.value, 1)
4125+
self.assertEqual(Huh.TWO.value, (2, 3))
4126+
self.assertEqual(Huh.THREE.value, (4, 5, 6))
4127+
#
4128+
class Hah(Enum):
4129+
def __new__(cls, value, abbr=None):
4130+
member = object.__new__(cls)
4131+
member._value_ = value
4132+
member.abbr = abbr or value[:3].lower()
4133+
return member
4134+
def _generate_next_value_(name, start, count, last):
4135+
return name
4136+
#
4137+
MONDAY = auto()
4138+
TUESDAY = auto()
4139+
WEDNESDAY = auto(), 'WED'
4140+
THURSDAY = auto(), 'Thu'
4141+
FRIDAY = auto()
4142+
self.assertEqual(Hah.MONDAY.value, 'MONDAY')
4143+
self.assertEqual(Hah.MONDAY.abbr, 'mon')
4144+
self.assertEqual(Hah.TUESDAY.value, 'TUESDAY')
4145+
self.assertEqual(Hah.TUESDAY.abbr, 'tue')
4146+
self.assertEqual(Hah.WEDNESDAY.value, 'WEDNESDAY')
4147+
self.assertEqual(Hah.WEDNESDAY.abbr, 'WED')
4148+
self.assertEqual(Hah.THURSDAY.value, 'THURSDAY')
4149+
self.assertEqual(Hah.THURSDAY.abbr, 'Thu')
4150+
self.assertEqual(Hah.FRIDAY.value, 'FRIDAY')
4151+
self.assertEqual(Hah.FRIDAY.abbr, 'fri')
4152+
#
4153+
class Huh(Enum):
4154+
def _generate_next_value_(name, start, count, last):
4155+
return count+1
4156+
ONE = auto()
4157+
TWO = auto(), auto()
4158+
THREE = auto(), auto(), auto()
4159+
self.assertEqual(Huh.ONE.value, 1)
4160+
self.assertEqual(Huh.TWO.value, (2, 2))
4161+
self.assertEqual(Huh.THREE.value, (3, 3, 3))
4162+
41194163
class TestEnumTypeSubclassing(unittest.TestCase):
41204164
pass
41214165

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``enum.auto()`` is now correctly activated when combined with other assignment values. E.g. ``ONE = auto(), 'some text'`` will now evaluate as ``(1, 'some text')``.

0 commit comments

Comments
 (0)