Skip to content

Using NamedTuple with custom Enum #114149

Closed
Closed

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
from dataclasses import dataclass
from enum import Enum

@dataclass
class CodeLabel:
    code: int
    label: str

class LabelledEnumMixin:

    labels = {}

    def __new__(cls, codelabel: CodeLabel):
        member = object.__new__(cls)
        member._value_ = codelabel.code
        member.label = codelabel.label
        cls.labels[codelabel.code] = codelabel.label
        return member

    @classmethod
    def list_labels(cls):
        return list(l for c, l in cls.labels.items())



class Test(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
from typing import NamedTuple
from enum import Enum

class CodeLabel(NamedTuple):
    code: int
    label: str

class LabelledEnumMixin:

    labels = {}

    def __new__(cls, codelabel: CodeLabel):
        member = object.__new__(cls)
        member._value_ = codelabel.code
        member.label = codelabel.label
        cls.labels[codelabel.code] = codelabel.label
        return member

    @classmethod
    def list_labels(cls):
        return list(l for c, l in cls.labels.items())



class Test(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

Activity

  1. self-assigned this
    on Jan 16, 2024
  2. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    3.11only security fixes
    3.12only security fixes
    3.13only security fixes
    and removed
    docsDocumentation in the Doc dir
    on Jan 16, 2024
  3. moved this from Todo to In Progress in enum issueson Jan 17, 2024
  4. added a commit that references this issue on Jan 17, 2024
  5. added a commit that references this issue on Jan 17, 2024
  6. 3 remaining items

  7. SultanOrazbayev commented on Jan 17, 2024

    @SultanOrazbayev
    Author

    It does seem like a correct behavior for a tuple, but it is a bit unexpected because tuples are treaded in a special way (re: the original issue). So perhaps this is resolvable by documenting the special treatment of tuples in __new__?

  8. ethanfurman commented on Jan 18, 2024

    @ethanfurman
    Member

    That is my thought.

  9. added a commit that references this issue on Jan 18, 2024
  10. added a commit that references this issue on Jan 18, 2024
  11. added a commit that references this issue on Jan 18, 2024
  12. moved this from In Progress to Done in enum issueson Jan 18, 2024
  13. added 2 commits that reference this issue on Jan 22, 2024
  14. added 2 commits that reference this issue on Feb 11, 2024
  15. added 2 commits that reference this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

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

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions