python / python/mypy

Enum with a value named `value` assigned with auto gives incompatible types in assignment

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

Given an enum with a value named value assigned with enum.auto() incorrectly gives an assignment error.

To Reproduce

See https://mypy-play.net/?mypy=latest&python=3.10&gist=febab64ba10c1ebc532d7b6038194791

import enum
class A(enum.Enum):
    value = enum.auto()

Expected Behavior

No error.

Actual Behavior

main.py:3: error: Incompatible types in assignment (expression has type "auto", base class "Enum" defined the type as "int")  [assignment]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.18.2 (compiled: yes)
  • Mypy command-line flags: mypy main.py
  • Mypy configuration options from mypy.ini (and other config files): nothing
  • Python version used: 3.10.18

Additional information

The value must be called value, if it's anything else, no error. E.g., this works:

class A(enum.Enum):
    v = enum.auto()

Multiple values also works:

class A(enum.Enum):
    value = enum.auto()
    v = enum.auto()

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 report with the provided Python snippet using mypy 1.18.2, then trace enum.auto() handling and the special value name in mypy's enum analysis. Add a regression test for the single-member case and verify that it reports no assignment error while existing enum cases still pass.

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
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.