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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

tjdcs
Copy link

@tjdcs tjdcs commented Jul 14, 2025

Fixes #14408

Summary

  • Add missing _add_value_alias_ method to Enum class for Python 3.13
  • Add comprehensive test cases for the MultiValueEnum pattern

Fixes #14408 by resolving the type checker error when using _add_value_alias_ method in Python 3.13 enum classes.

Changes Made

  • Added _add_value_alias_(self, value: Any) -> None method to Enum class in stdlib/enum.pyi
  • Added proper Python 3.13 version guard (if sys.version_info >= (3, 13):)
  • Added comprehensive test cases in stdlib/@tests/test_cases/check_enum.py including:
    • MultiValueEnum pattern from Python documentation
    • Type inference tests for primary and alias values
    • Literal type verification

Test Plan

  • Verify Pyright recognizes the method without errors (0 errors, 0 warnings)
  • Verify mypy recognizes the method without errors (Success: no issues found)
  • Test runtime functionality with Python 3.13 (✅ Works correctly)
  • Ensure no regressions in existing enum functionality (✅ All tests pass)
  • Test cases pass with both type checkers (✅ Pyright and mypy both succeed)

Example Usage

from enum import Enum

class MultiValueEnum(Enum):
    def __new__(cls, value, *values):
        self = object.__new__(cls)
        self._value_ = value
        for v in values:
            self._add_value_alias_(v)  # No longer causes type error
        return self

class DType(MultiValueEnum):
    float32 = 'f', 8
    double64 = 'd', 9

# Both work correctly
print(DType('f'))  # <DType.float32: 'f'>
print(DType(8))    # <DType.float32: 'f'>

🤖 Generated with Claude Code

Fixes python#14408 by adding the _add_value_alias_ method to the Enum class
with proper Python 3.13 version guard. Also adds comprehensive test
cases for the MultiValueEnum pattern.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

This comment has been minimized.

@tjdcs tjdcs force-pushed the fix-enum-add-value-alias branch from cb06731 to 693d71d Compare July 14, 2025 18:08
@tjdcs tjdcs force-pushed the fix-enum-add-value-alias branch from 693d71d to 1e82dac Compare July 14, 2025 18:12
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@tjdcs tjdcs marked this pull request as ready for review July 14, 2025 18:28
Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

Thanks, remarks below. We should also add overridden variants of this method to StrEnum and IntEnum, similar to what we do for other attributes/methods where value is used.

Also, for future reference: Please don't use AI PR summaries. Those use a lot of words for something very simple, making it considerably harder to understand the issue and PR. In this case, a simple link back to the original issued would have sufficed.

Comment on lines +39 to +65


if sys.version_info >= (3, 13):

class MultiValueEnum(enum.Enum):
def __new__(cls, value: object, *values: Any) -> "MultiValueEnum":
self = object.__new__(cls)
self._value_ = value
for v in values:
self._add_value_alias_(v)
return self

class DType(MultiValueEnum):
float32 = "f", 8
double64 = "d", 9

# Test type inference for primary values
assert_type(DType("f"), DType)
assert_type(DType("d"), DType)

# Test type inference for alias values
assert_type(DType(8), DType)
assert_type(DType(9), DType)

# Test that the enum members have the correct literal types
assert_type(DType.float32, Literal[DType.float32])
assert_type(DType.double64, Literal[DType.double64])
Copy link
Collaborator

Choose a reason for hiding this comment

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

These tests don't seem to test anything relevant – at least not as it relates to type annotations. We generally don't run our test cases, we just type check them, so any logic is meaningless. This is also seems to test something called a MultiValueEnum, which isn't a concept in Python's stdlib.

@@ -219,6 +219,8 @@ 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):
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

@donBarbos donBarbos left a comment

Choose a reason for hiding this comment

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

I would like to note that the _add_alias_ method was added in the same version, and it would be possible to add them together

@tjdcs
Copy link
Author

tjdcs commented Jul 15, 2025

Thank you for your comments. Give me a day or two to address them and correct this PR. I will add the requisite comments, remove the irrelevant tests, and check the additional special enum types.

Also, my apologies for the verbose PR summary. I was really just letting Claude run rampant and see what he did / what he came up with. I'll keep that in mind for the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing attribute _add_value_alias_ for Enum
3 participants