diff --git a/mypy/plugins/enums.py b/mypy/plugins/enums.py index d21b21fb39f8..ef6fed9c6fd4 100644 --- a/mypy/plugins/enums.py +++ b/mypy/plugins/enums.py @@ -196,22 +196,11 @@ class SomeEnum: if _implements_new(info): return ctx.default_attr_type - stnodes = (info.get(name) for name in info.names) - # Enums _can_ have methods, instance attributes, and `nonmember`s. # Omit methods and attributes created by assigning to self.* # for our value inference. - node_types = ( - get_proper_type(n.type) if n else None - for n in stnodes - if n is None or not n.implicit - ) - 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")) - ] + node_types = (get_proper_type(info[name].type) for name in info.enum_members) + 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 diff --git a/test-data/unit/check-enum.test b/test-data/unit/check-enum.test index d034fe1a6f5f..b90cff1db08f 100644 --- a/test-data/unit/check-enum.test +++ b/test-data/unit/check-enum.test @@ -2635,3 +2635,28 @@ reveal_type(Wrapper.Nested.FOO) # N: Revealed type is "Literal[__main__.Wrapper reveal_type(Wrapper.Nested.FOO.value) # N: Revealed type is "builtins.ellipsis" reveal_type(Wrapper.Nested.FOO._value_) # N: Revealed type is "builtins.ellipsis" [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]