Skip to content

Add missing _add_value_alias_ method for Python 3.13 enum #14411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions stdlib/enum.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ class Enum(metaclass=EnumMeta):
if sys.version_info >= (3, 12) and sys.version_info < (3, 14):
@classmethod
def __signature__(cls) -> str: ...
if sys.version_info >= (3, 13):
# Value may be any type, even in special enums. Enabling Enum parsing from
# multiple value types
def _add_value_alias_(self, value: Any) -> None: ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We nowadays require explanatory comments for all non-obvious Anys. In this case, maybe the following comment makes sense?

Suggested change
def _add_value_alias_(self, value: Any) -> None: ...
# value must have the same type as other enum members
def _add_value_alias_(self, value: Any) -> None: ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... Looking at your suggestion... is this true? I have implemented a str enum with int and other aliases...

from enum import Enum, StrEnum


class EOTFType(str, Enum):
    RESERVED = ("RESERVED", 0)
    SDR = ("SDR", 1)
    PQ = ("PQ", 2)
    HLG = ("HLG", 3)

    def __new__(cls, value: str, int_val: int):
        self = str.__new__(cls, value)
        self._value_ = value
        self._add_value_alias_(int_val)  # type: ignore Added in python 3.13
        return self

    def __init__(
        self,
        _: str,
        int_value: int,
    ):
        self.int_value = int_value

    def __str__(self) -> str:
        """
        Return a formatted string representation of the EOTF type.

        Returns
        -------
        str
            Format: "value=name" (e.g., "2=PQ")
        """
        return f'{self.value}="{self.value}"={self.int_value}'


class StrIntEnum(StrEnum):
    RED = "red"
    GREEN = "green"

def main():
    direct = EOTFType.PQ
    print(direct)

    parsed_int = EOTFType(2)
    print(parsed_int)

    parsed_str = EOTFType("PQ")
    print(parsed_str)

    print(f'parsed_str == "PQ": {parsed_str == "PQ"}')
    print(f'parsed_str == "PQ": {parsed_str == 2}')
    print(f'parsed_str.int_value == "PQ": {parsed_str.int_value == 2}')

    StrIntEnum.GREEN._add_value_alias_(4)
    print(StrIntEnum(4))


if __name__ == "__main__":
    main()

Is Any really the correct annotation here or is object better... as above?

As for the special enums (StrEnum, IntEnum)... It seems the alias can still be "any".

def _add_alias_(self, name: str) -> None: ...

if sys.version_info >= (3, 11):
class ReprEnum(Enum): ...
Expand Down
Loading