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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
tjdcs wants to merge3 commits intopython:main
base:main
Choose a base branch
Loading
fromtjdcs:fix-enum-add-value-alias

Conversation

tjdcs
Copy link

@tjdcstjdcs commentedJul 14, 2025
edited
Loading

Fixes#14408

Summary

  • Add missing_add_value_alias_ method toEnum 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 toEnum class instdlib/enum.pyi
  • Added proper Python 3.13 version guard (if sys.version_info >= (3, 13):)
  • Added comprehensive test cases instdlib/@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

fromenumimportEnumclassMultiValueEnum(Enum):def__new__(cls,value,*values):self=object.__new__(cls)self._value_=valueforvinvalues:self._add_value_alias_(v)# No longer causes type errorreturnselfclassDType(MultiValueEnum):float32='f',8double64='d',9# Both work correctlyprint(DType('f'))# <DType.float32: 'f'>print(DType(8))# <DType.float32: 'f'>

🤖 Generated withClaude Code

Fixespython#14408 by adding the _add_value_alias_ method to the Enum classwith proper Python 3.13 version guard. Also adds comprehensive testcases for the MultiValueEnum pattern.🤖 Generated with [Claude Code](https://claude.ai/code)Co-Authored-By: Claude <noreply@anthropic.com>
@github-actionsGitHub Actions

This comment has been minimized.

@tjdcstjdcsforce-pushed thefix-enum-add-value-alias branch fromcb06731 to693d71dCompareJuly 14, 2025 18:08
@tjdcstjdcsforce-pushed thefix-enum-add-value-alias branch from693d71d to1e82dacCompareJuly 14, 2025 18:12
@github-actionsGitHub Actions
Copy link
Contributor

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

@tjdcstjdcs marked this pull request as ready for reviewJuly 14, 2025 18:28
Copy link
Collaborator

@srittausrittau 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 toStrEnum andIntEnum, similar to what we do for other attributes/methods wherevalue 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 aMultiValueEnum, 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-obviousAnys. 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

@donBarbosdonBarbos 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

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.

srittau reacted with heart emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@donBarbosdonBarbosdonBarbos left review comments

@srittausrittausrittau requested changes

Requested changes must be addressed to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Missing attribute _add_value_alias_ for Enum
3 participants
@tjdcs@srittau@donBarbos

[8]ページ先頭

©2009-2025 Movatter.jp