Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Using NamedTuple with custom Enum #114149

Closed
Closed
Assignees
ethanfurman
Labels
3.11only security fixes3.12only security fixes3.13bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
@SultanOrazbayev

Description

@SultanOrazbayev

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

Metadata

Metadata

Assignees

Labels

3.11only security fixes3.12only security fixes3.13bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp