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

FixEnum.value inference forEnums with@cached methods#19374

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
kmurphy4 wants to merge1 commit intopython:master
base:master
Choose a base branch
Loading
fromkmurphy4:fix/enum-value-inference

Conversation

kmurphy4
Copy link

@kmurphy4kmurphy4 commentedJul 4, 2025
edited
Loading

Before this, adding an annotation like@functools.cache to any method on anEnum caused the inference for the class's.value to fail (i.e., becomeAny).

Fixes#19368

@kmurphy4
Copy link
Author

Should I add something to the changelog? I think this is a user-facing change.

@github-actionsGitHub Actions

This comment has been minimized.

@sterliakov
Copy link
Collaborator

We build changelog from the commit history, like#19138, so no need to add CHANGELOG entries in every PR:)

kmurphy4 reacted with thumbs up emoji

@@ -194,12 +194,7 @@ class SomeEnum:
for n in stnodes
if n is None or not n.implicit
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure this check is still needed - can there be anyenum_members that areimplicit? That should never happen AFAIC

Copy link
Author

Choose a reason for hiding this comment

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

Seems like it is? With

diff --git i/mypy/plugins/enums.py w/mypy/plugins/enums.pyindex 35338d091..c6e9f21b6 100644--- i/mypy/plugins/enums.py+++ w/mypy/plugins/enums.py@@ -192,7 +192,7 @@ def enum_value_callback(ctx: mypy.plugin.AttributeContext) -> Type:             node_types = (                 get_proper_type(n.type) if n else None                 for n in stnodes-                if n is None or not n.implicit+                if n is None             )             proper_types = [_infer_value_type_with_auto_fallback(ctx, t) for t in node_types]             underlying_type = _first(proper_types)

I get

$pytest-ktestValueFallbackWithCachedMethod=============================testsessionstarts==============================platformlinux--Python3.12.6,pytest-8.3.5,pluggy-1.6.0rootdir:/home/kevinmurphy/src/python/mypyconfigfile:pyproject.tomltestpaths:mypy/test,mypyc/testplugins:xdist-3.7.0,cov-6.1.16workers [1item]F                                                                        [100%]===================================FAILURES===================================______________________testValueFallbackWithCachedMethod_______________________[gw0]linux--Python3.12.6/home/kevinmurphy/src/python/mypy/venv/bin/python3data:/home/kevinmurphy/src/python/mypy/test-data/unit/check-enum.test:2543:Failed:Unexpectedtypecheckeroutput (/home/kevinmurphy/src/python/mypy/test-data/unit/check-enum.test,line2543)-----------------------------Capturedstderrcall-----------------------------Expected:main:19:note:Revealedtypeis"builtins.int"main:22:note:Revealedtypeis"builtins.int" (diff)Actual:main:19:note:Revealedtypeis"builtins.int"main:22:note:Revealedtypeis"Any" (diff)Alignmentoffirstlinedifference:E: ...ote:Revealedtypeis"builtins.int"A: ...ote:Revealedtypeis"Any"^Updatethetestoutputusing--update-data (implies-n0;youcanadditionallyusethe-kselectortoupdateonlyspecifictests)===========================shorttestsummaryinfo============================FAILEDmypy/test/testcheck.py::TypeCheckSuite::check-enum.test::testValueFallbackWithCachedMethod==============================1failedin2.26s===============================

Copy link
Collaborator

@sterliakovsterliakovJul 11, 2025
edited
Loading

Choose a reason for hiding this comment

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

Sorry, I didn't mean to suggest only retainingNone items:) I asked if theif clause can be removed altogether:n is None or not n.implicit should be true for alln coming fromenum_members (all members should not beimplicit - attrs with annotation and without value are assumed to be nonmembers according to the spec). Probably none of them should beNone either, maybe just

node_types = (get_proper_type(info[name].type) for name in info.enum_members)

is enough now?

Copy link
Author

Choose a reason for hiding this comment

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

Ah, yes, I think1cedcf9 will work! I'll go ahead and squash that into the other commit.

Copy link
Author

Choose a reason for hiding this comment

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

.. and resolve the upstream conflict while I'm at it.

Before this, adding an annotation like `@functools.cache` to anymethod on an `Enum` caused the inference for the class's `.value`to fail (i.e., become `Any`).Fixespython#19368Co-authored-by: Stanislav Terliakov <50529348+sterliakov@users.noreply.github.com>
@kmurphy4kmurphy4force-pushed thefix/enum-value-inference branch frome116ada to110a1a9CompareJuly 14, 2025 14:37
@github-actionsGitHub Actions
Copy link
Contributor

According tomypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

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

@sterliakovsterliakovsterliakov left review comments

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

Successfully merging this pull request may close these issues.

Enum.value typed asAny when usingfunctools.cache
2 participants
@kmurphy4@sterliakov

[8]ページ先頭

©2009-2025 Movatter.jp