Incompatible return value type `type[Enum]` when returning a functional-style Enum
Nobody has claimed this yet.
- 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
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.0andmasteron 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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