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 _ignore_'d names and private names are treated like normal members #18491

Open
injust opened this issue Jan 20, 2025 · 0 comments
Open

Enum _ignore_'d names and private names are treated like normal members #18491

injust opened this issue Jan 20, 2025 · 0 comments
Labels
bug mypy got something wrong topic-enum

Comments

@injust
Copy link

injust commented Jan 20, 2025

Bug Report

To Reproduce

from enum import Enum
from typing import TYPE_CHECKING


class Foo(Enum):
    __str__ = lambda self: "blah"
    _ignore_ = ["ignored"]
    ignored = lambda _: "blah"
    _Foo__private = lambda _: "blah"


print(Foo.__members__)  # {}

print(Foo.__str__)  # <function Foo.<lambda> at 0x...>
if TYPE_CHECKING:
    reveal_type(Foo.__str__)  # note: Revealed type is "def () -> builtins.str" ✅

print(hasattr(Foo, "_ignore_"))  # False
if TYPE_CHECKING:
    reveal_type(Foo._ignore_)  # error: "type[Foo]" has no attribute "_ignore_"  [attr-defined] ✅

print(hasattr(Foo, "ignored"))  # False
if TYPE_CHECKING:
    reveal_type(Foo.ignored)  # note: Revealed type is "Literal[__main__.Foo.ignored]?" ❌

print(Foo._Foo__private)  # <function Foo.<lambda> at 0x...>
if TYPE_CHECKING:
    reveal_type(Foo._Foo__private)  # note: Revealed type is "Literal[__main__.Foo._Foo__private]?" ❌

https://mypy-play.net/?mypy=latest&python=3.13&gist=ea296898a9c0e29c550b6154fb2cb594

Actual Behavior

main.py:16: note: Revealed type is "def () -> builtins.str"
main.py:20: error: "type[Foo]" has no attribute "_ignore_"  [attr-defined]
main.py:20: note: Revealed type is "Any"
main.py:24: note: Revealed type is "Literal[__main__.Foo.ignored]?"
main.py:28: note: Revealed type is "Literal[__main__.Foo._Foo__private]?"
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.14.1
  • Python version used: 3.13
@injust injust added the bug mypy got something wrong label Jan 20, 2025
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

No branches or pull requests

2 participants