Skip to content

Fix Enum.value inference for Enums 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 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 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 2 additions & 7 deletions mypy/plugins/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class SomeEnum:
if _implements_new(info):
return ctx.default_attr_type

stnodes = (info.get(name) for name in info.names)
stnodes = (info.get(name) for name in info.enum_members)

# Enums _can_ have methods, instance attributes, and `nonmember`s.
# Omit methods and attributes created by assigning to self.*
Expand All @@ -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 any enum_members that are implicit? 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.py
index 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 -k testValueFallbackWithCachedMethod
============================= test session starts ==============================
platform linux -- Python 3.12.6, pytest-8.3.5, pluggy-1.6.0
rootdir: /home/kevinmurphy/src/python/mypy
configfile: pyproject.toml
testpaths: mypy/test, mypyc/test
plugins: xdist-3.7.0, cov-6.1.1
6 workers [1 item]      
F                                                                        [100%]
=================================== FAILURES ===================================
______________________ testValueFallbackWithCachedMethod _______________________
[gw0] linux -- Python 3.12.6 /home/kevinmurphy/src/python/mypy/venv/bin/python3
data: /home/kevinmurphy/src/python/mypy/test-data/unit/check-enum.test:2543:
Failed: Unexpected type checker output (/home/kevinmurphy/src/python/mypy/test-data/unit/check-enum.test, line 2543)
----------------------------- Captured stderr call -----------------------------
Expected:
  main:19: note: Revealed type is "builtins.int"
  main:22: note: Revealed type is "builtins.int" (diff)
Actual:
  main:19: note: Revealed type is "builtins.int"
  main:22: note: Revealed type is "Any" (diff)

Alignment of first line difference:
  E: ...ote: Revealed type is "builtins.int"
  A: ...ote: Revealed type is "Any"
                               ^
Update the test output using --update-data (implies -n0; you can additionally use the -k selector to update only specific tests)
=========================== short test summary info ============================
FAILED mypy/test/testcheck.py::TypeCheckSuite::check-enum.test::testValueFallbackWithCachedMethod
============================== 1 failed in 2.26s ===============================

)
proper_types = [
_infer_value_type_with_auto_fallback(ctx, t)
for t in node_types
if t is None
or (not isinstance(t, CallableType) and not is_named_instance(t, "enum.nonmember"))
]
proper_types = [_infer_value_type_with_auto_fallback(ctx, t) for t in node_types]
underlying_type = _first(proper_types)
if underlying_type is None:
return ctx.default_attr_type
Expand Down
25 changes: 25 additions & 0 deletions test-data/unit/check-enum.test
Original file line number Diff line number Diff line change
Expand Up @@ -2539,3 +2539,28 @@ def check(thing: Things) -> None:
return None
return None # E: Statement is unreachable
[builtins fixtures/enum.pyi]

[case testValueFallbackWithCachedMethod]
from enum import Enum, auto
from collections.abc import Hashable
from typing import Callable, Generic, TypeVar

_T = TypeVar("_T")

class _lru_cache_wrapper(Generic[_T]):
def __call__(self, *args: Hashable, **kwargs: Hashable) -> _T: ...

def cache(user_function: Callable[..., _T], /) -> _lru_cache_wrapper[_T]: ...

class Color(Enum):
RED = auto()

@cache
def lowercase_name(self) -> str:
return self.name

reveal_type(Color.RED.value) # N: Revealed type is "builtins.int"

def frobnicate(color: Color) -> None:
reveal_type(color.value) # N: Revealed type is "builtins.int"
[builtins fixtures/primitives.pyi]