mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
[3.12] gh-114071: [Enum] update docs and code for tuples/subclasses (GH-114871) (GH-114993)
Update documentation with `__new__` and `__init__` entries.
Support use of `auto()` in tuple subclasses on member assignment lines. Previously, auto() was only supported on the member definition line either solo or as part of a tuple:
RED = auto()
BLUE = auto(), 'azul'
However, since Python itself supports using tuple subclasses where tuples are expected, e.g.:
from collections import namedtuple
T = namedtuple('T', 'first second third')
def test(one, two, three):
print(one, two, three)
test(*T(4, 5, 6))
GH- 4 5 6
it made sense to also support tuple subclasses in enum definitions.
(cherry picked from commit ff7588b729
)
Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
This commit is contained in:
parent
dc01c84ed0
commit
7d2f88edf0
4 changed files with 69 additions and 5 deletions
10
Lib/enum.py
10
Lib/enum.py
|
@ -457,10 +457,11 @@ class _EnumDict(dict):
|
|||
if isinstance(value, auto):
|
||||
single = True
|
||||
value = (value, )
|
||||
if type(value) is tuple and any(isinstance(v, auto) for v in value):
|
||||
if isinstance(value, tuple) and any(isinstance(v, auto) for v in value):
|
||||
# insist on an actual tuple, no subclasses, in keeping with only supporting
|
||||
# top-level auto() usage (not contained in any other data structure)
|
||||
auto_valued = []
|
||||
t = type(value)
|
||||
for v in value:
|
||||
if isinstance(v, auto):
|
||||
non_auto_store = False
|
||||
|
@ -475,7 +476,12 @@ class _EnumDict(dict):
|
|||
if single:
|
||||
value = auto_valued[0]
|
||||
else:
|
||||
value = tuple(auto_valued)
|
||||
try:
|
||||
# accepts iterable as multiple arguments?
|
||||
value = t(auto_valued)
|
||||
except TypeError:
|
||||
# then pass them in singlely
|
||||
value = t(*auto_valued)
|
||||
self._member_names[key] = None
|
||||
if non_auto_store:
|
||||
self._last_values.append(value)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue