python / python/mypy

Inconsistent errors with custom enum base auto items from installed library

Open
#15,382 4 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hi, I've defined a custom enum base to copy the names of the enum items as values when using auto(). Mypy curiously throws an assignment error about incompatible types when using the items as e.g. default arguments in a function. However, this only happens if the custom base is imported from another library (in site packages, using another local package is fine).

Here's some example code:

from enum import Enum, auto
# from lib import CopyNameEnum

class CopyNameEnum(Enum):
    def _generate_next_value_(name, start, count, last_values):
        return name

class Items(str, CopyNameEnum):
    A = auto()
    B = auto()

def process(item: Items = Items.A):
    pass

The above is fine and produces no errors. However, when using the import instead, we get

Incompatible default for argument "item" (default has type "auto", argument has type "Items") [assignment]

I would assume that this is a bug, but if you have any other thoughts or debugging suggestions, let me know! I found another issue about the same private method in Enum in #7591, but the issue doesn't seem to be exactly related.

Environment
  • mypy 1.3.0
  • Python 3.9.16

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 reproducing the provided custom Enum and auto() example with mypy 1.3.0, comparing the local definition with the imported-library case. Trace mypy's handling of enum auto values and installed-library metadata, then verify that the default Items.A is accepted without an incompatible-default error in both cases.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.