python / python/mypy

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

Open
#18,491 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-enum
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example with mypy 1.14.1 on Python 3.13, focusing on Enum handling of str, ignore, ignored, and the name-mangled private member. Trace the type-checking path that classifies Enum attributes and add regression coverage so ignored and private names are not treated as normal members.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.