python / python/mypy

Incompatible return value type `type[Enum]` when returning a functional-style Enum

Open
#17,147 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 returning a functional-style Enum class from a function with the return type type[Enum] mypy reports an error.

When the created Enum class is assigned to a variable and the variable is return, mypy does not report an error.

To Reproduce

from enum import Enum

def direct_return() -> type[Enum]:
    return Enum("direct", {"a": 0})    # error: Incompatible return value type (got "Enum", expected "type[Enum]")  [return-value]

def indirect_return() -> type[Enum]:
    indirect = Enum("indirect", {"a": 0})
    return indirect                    # no error as expected

Gist URL
Playground URL

Expected Behavior

no mypy warnings

Actual Behavior

main.py:4: error: Incompatible return value type (got "Enum", expected "type[Enum]") [return-value]

Your Environment

  • Mypy version used: 1.9.0 and master on mypy-play.net
  • Mypy command-line flags: mypy-play.net defaults
  • Mypy configuration options from mypy.ini (and other config files): mypy-play.net defaults
  • Python version used: 3.12

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 direct_return and indirect_return examples with mypy 1.9.0 or master, using the linked playground behavior as a reference. Trace how functional-style Enum calls are inferred in the type checker, then add a regression test showing that both functions return without warnings.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.