python / python/mypy

Private members (__underscored) in an Enum are incorrectly considered members of the enum

Open
#20,789 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

Mypy, at least versions 1.17.1 and the latest 1.19.1 incorrectly classifies private members as Enum members and tags them as final.

But, since Python 3.11, private members are not enum members, see https://docs.python.org/3/howto/enum.html#private-names

To Reproduce

https://mypy-play.net/?gist=afd9e4e68abe69277d97eb42f33adbcf

from typing import ClassVar
from enum import IntEnum

class WithDocReference:
  __doc_reference: ClassVar[str]

class MyEnum(WithDocReference, IntEnum):
  __doc_reference = "https://www.google.com"
  A = 1
  B = 2

Expected Behavior

Type checks without error.

Actual Behavior

mypy 1.17.1 fails with

$ mypy test.py
test.py:8: error: Cannot override writable attribute "__doc_reference" with a final one  [misc]
Found 1 error in 1 file (checked 1 source file)

mypy 1.19.1 has an additional error.

Your Environment

  • Mypy version used: 1.17.1 and 1.19.1
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.11.9

Edited: private names should start with two underscores and not one

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 issue from the linked mypy-play gist or the test.py example with Python 3.11 and the reported mypy versions. Compare the result with Python's documented handling of private Enum names; the work is done when the example type-checks without the reported errors and the behavior is covered by a regression test.

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.