Skip to content

.enum_members property does not account to @member decorator and call #18557

Closed

Description

@sobolevn

Code in question:

mypy/mypy/nodes.py

Lines 3232 to 3243 in 93d1ce4

@property
def enum_members(self) -> list[str]:
return [
name
for name, sym in self.names.items()
if (
isinstance(sym.node, Var)
and name not in EXCLUDED_ENUM_ATTRIBUTES
and not name.startswith("__")
and sym.node.has_explicit_value
)
]

This is a problem, because methods decorated as @member are not included into the members list.

The simplest repro where this is a problem:

from enum import Enum, member
from typing import reveal_type

class Pet(Enum):
    CAT = 1

    @member
    def human(self) -> int:
        return 2

def a(x: Pet) -> None:
    if x is not Pet.CAT:
        reveal_type(x) 

With --warn-unreachable it will produce:

ex.py:13: note: Revealed type is "enum.member[def (self: ex.Pet) -> builtins.int]"
ex.py:18: error: Statement is unreachable  [unreachable]

Because .enum_members here will only contain CAT:

mypy/mypy/typeops.py

Lines 974 to 978 in 93d1ce4

if typ.type.is_enum:
items = [LiteralType(name, typ) for name in typ.type.enum_members]
if not items:
return typ
return make_simplified_union(items, contract_literals=False)

I will send a PR with the fix.

Activity

  1. self-assigned this
    on Jan 28, 2025
  2. sobolevn commented on Jan 28, 2025

    @sobolevn
    MemberAuthor

    It also does not account for nonmember calls like: DOG = nonmember(2)

  3. added a commit that references this issue on Feb 14, 2025
    f404b16
  4. added a commit that references this issue on Feb 24, 2025
    2607c06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions