mirror of
https://github.com/python/cpython.git
synced 2025-08-22 17:55:18 +00:00
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')`
This commit is contained in:
parent
586b07e1f9
commit
8feb7ab77c
4 changed files with 82 additions and 11 deletions
33
Lib/enum.py
33
Lib/enum.py
|
@ -171,7 +171,8 @@ class auto:
|
|||
"""
|
||||
Instances are replaced with an appropriate value in Enum class suites.
|
||||
"""
|
||||
value = _auto_null
|
||||
def __init__(self, value=_auto_null):
|
||||
self.value = value
|
||||
|
||||
def __repr__(self):
|
||||
return "auto(%r)" % self.value
|
||||
|
@ -427,15 +428,31 @@ class _EnumDict(dict):
|
|||
elif isinstance(value, member):
|
||||
# unwrap value here -- it will become a member
|
||||
value = value.value
|
||||
non_auto_store = True
|
||||
single = False
|
||||
if isinstance(value, auto):
|
||||
if value.value == _auto_null:
|
||||
value.value = self._generate_next_value(
|
||||
key, 1, len(self._member_names), self._last_values[:],
|
||||
)
|
||||
self._auto_called = True
|
||||
value = value.value
|
||||
single = True
|
||||
value = (value, )
|
||||
if isinstance(value, tuple):
|
||||
auto_valued = []
|
||||
for v in value:
|
||||
if isinstance(v, auto):
|
||||
non_auto_store = False
|
||||
if v.value == _auto_null:
|
||||
v.value = self._generate_next_value(
|
||||
key, 1, len(self._member_names), self._last_values[:],
|
||||
)
|
||||
self._auto_called = True
|
||||
v = v.value
|
||||
self._last_values.append(v)
|
||||
auto_valued.append(v)
|
||||
if single:
|
||||
value = auto_valued[0]
|
||||
else:
|
||||
value = tuple(auto_valued)
|
||||
self._member_names[key] = None
|
||||
self._last_values.append(value)
|
||||
if non_auto_store:
|
||||
self._last_values.append(value)
|
||||
super().__setitem__(key, value)
|
||||
|
||||
def update(self, members, **more_members):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue