python / python/mypy

mypy reports an error when passing enum values as arguments

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

Starting with https://github.com/python/mypy/commit/1a04c073260c13107abfab6ae0ff4c8cc214eb54 @sterliakov enum values assigned to functions are typed as Callables. This seems to make mypy report an error when passing enum values to functions that expect the enum.

To Reproduce


from enum import Enum
import operator

class Op(Enum):
    ADD = operator.add
    SUB = operator.sub
    MUL = operator.mul

def takes_op(op: Op, x: int, y: int):
    return op.value(x, y)

print(takes_op(Op.ADD, 2, 2))

Expected Behavior

No error

Actual Behavior

/Users/psawicki/test.py:12: error: Argument 1 to "takes_op" has incompatible type "Callable[[Any, Any], Any]"; expected "Op"  [arg-type]

Your Environment

  • Mypy version used: mypy 1.20.0+dev.1a04c073260c13107abfab6ae0ff4c8cc214eb54 and up
  • Mypy command-line flags: mypy ~/test.py
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.13.5

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

Run the minimal Python example from the issue with mypy 1.20.0+dev.1a04c073260c13107abfab6ae0ff4c8cc214eb54 and trace how enum members assigned callable values are typed. Add a regression test for passing Op.ADD to takes_op, and confirm that mypy reports no argument-type error.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.