Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
Documentation
This issue is related to#114071 and@ethanfurman suggested creating an isolated issue:
The handling of NamedTuple with a custom Enum is unexpected. Here are two examples that seek to achieve the same thing, but one will fail when using NamedTuple.
Using dataclasses (works):
Details
fromdataclassesimportdataclassfromenumimportEnum@dataclassclassCodeLabel:code:intlabel:strclassLabelledEnumMixin:labels= {}def__new__(cls,codelabel:CodeLabel):member=object.__new__(cls)member._value_=codelabel.codemember.label=codelabel.labelcls.labels[codelabel.code]=codelabel.labelreturnmember@classmethoddeflist_labels(cls):returnlist(lforc,lincls.labels.items())classTest(LabelledEnumMixin,Enum):A=CodeLabel(1,"Label A")B=CodeLabel(2,"Custom B")C=CodeLabel(3,"Custom label for value C + another string")Test.list_labels()
Using NamedTuple (fails):
Details
fromtypingimportNamedTuplefromenumimportEnumclassCodeLabel(NamedTuple):code:intlabel:strclassLabelledEnumMixin:labels= {}def__new__(cls,codelabel:CodeLabel):member=object.__new__(cls)member._value_=codelabel.codemember.label=codelabel.labelcls.labels[codelabel.code]=codelabel.labelreturnmember@classmethoddeflist_labels(cls):returnlist(lforc,lincls.labels.items())classTest(LabelledEnumMixin,Enum):A=CodeLabel(1,"Label A")B=CodeLabel(2,"Custom B")C=CodeLabel(3,"Custom label for value C + another string")Test.list_labels()
Linked PRs
- gh-114149: fix tuple subclass handling when using custom __new__ #114160
- [3.12] gh-114149: [Enum] fix tuple subclass handling when using custom __new__ (GH-114160) #114196
- [3.11] gh-114149: [Enum] fix tuple subclass handling when using custom __new__ (GH-114160) #114197
- gh-114149: [Enum] revert #114160 and add more tuple-subclass tests #114215
- [3.12] gh-114149: [Enum] revert GH-114196 and add more tuple-subclass tests (GH-114215) #114218