python / python/mypy

false "Attempted to reuse member name in Enum definition"

Open
#16,876 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

When using an if/else in class scope of an Enum definition, there is a false error "Attempted to reuse member name in Enum definition".

I understand mypy might have an opinion about this pattern, but I noticed there is no equivalent issue for a plain class. So this seems to be an issue specific to Enum that could potentially be relaxed.

To Reproduce

class Foo(Enum):
    if IS_BAZ:
        DISABLED = 16
    else:
        # error: Attempted to reuse member name "DISABLED" in Enum definition "Foo"  [misc]
        DISABLED = 256

class Bar:
    if IS_BAZ:
        DISABLED = 16
    else:
        DISABLED = 256   # (no error)

Expected Behavior

no mypy errors

Actual Behavior

error: Attempted to reuse member name "DISABLED" in Enum definition "Foo"  [misc]

Your Environment

  • Mypy version used: 1.8.0
  • Python version used: 3.8

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

Start by running the provided Enum and plain-class reproducer against mypy 1.8.0, then trace the Enum member-name checking path responsible for the duplicate-name diagnostic. Done means the conditional Enum definition produces no mypy errors while duplicate members in genuinely conflicting definitions remain diagnosed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
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.