>>> from enum import Enum
>>> from collections import namedtuple
>>> TTuple = namedtuple('TTuple', 'id a blist')
>>> class NTEnum(Enum):
... NONE = TTuple(0, 0, [])
... A = TTuple(1, 2, [4])
... B = TTuple(2, 4, [0, 1, 2])
...
...
>>> NTEnum.NONE
<NTEnum.NONE: (0, 0, [])>
>>> NTEnum.NONE.value
(0, 0, [])
>>> [x.value for x in NTEnum]
[(0, 0, []), (1, 2, [4]), (2, 4, [0, 1, 2])]
>>> import copy
>>> x = TTuple(0, 1, [7])
>>> x
TTuple(id=0, a=1, blist=[7])
>>> copy.copy(x)
TTuple(id=0, a=1, blist=[7])
>>> copy.deepcopy(x)
TTuple(id=0, a=1, blist=[7])
>>> NTEnum.NONE.value.blist
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
NTEnum.NONE.value.blist
AttributeError: 'tuple' object has no attribute 'blist'
Bug report
Between 3.11.0 and 3.11.1, Enums whose values are namedtuple objects have their values converted to tuple, which drops the field names we expect to be able to use, causing AttributeErrors. Test cases below create a namedtuple and an enum whose values are instances of that tuple. In the 3.11.1 case, referencing the enum value like
NTEnum.NONE.valueproduces a tuple and not a namedtuple. In both cases,copy.copypreserves the namedtuple type.It is not clear whether any item in the changelog or release notes references this change, nor could I quickly tell whether this was related to changes to address #93910.
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
Python 3.11.1 (tags/v3.11.1:a7a450f, Dec 6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] on win32
Your environment
Linked PRs