Skip to content
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

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

Open
sobolevn opened this issue Jan 28, 2025 · 1 comment · May be fixed by #18559
Open

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

sobolevn opened this issue Jan 28, 2025 · 1 comment · May be fixed by #18559
Assignees
Labels
bug mypy got something wrong topic-enum

Comments

@sobolevn
Copy link
Member

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.

@sobolevn sobolevn added bug mypy got something wrong topic-enum labels Jan 28, 2025
@sobolevn sobolevn self-assigned this Jan 28, 2025
@sobolevn
Copy link
Member Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-enum
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant