Skip to content

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

Closed
@sobolevn

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.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions