python / python/mypy

Type `_order_` field in `Enum`

Open
#11,715 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Right now this code passes type checking:

from enum import Enum

class E(Enum):
    _order_ = 'X Y Z'  # Should be 'X Y'
    X = 1
    Y = 2

But, in runtime it fails:

» python out/ex.py       
Traceback (most recent call last):
  File "/Users/sobolev/Desktop/mypy/out/ex.py", line 3, in <module>
    class E(Enum):
  File "/Users/sobolev/.pyenv/versions/3.10.0/lib/python3.10/enum.py", line 348, in __new__
    raise TypeError('member order does not match _order_')
TypeError: member order does not match _order_

We need to check that _order_ field is valid.
This can be a great addition to my "better Enums" series of PRs.

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 tracing mypy's handling of Python Enum class bodies and reproduce the issue's example with the mismatched _order_ value. The work is done when type checking reports an invalid member order while accepting a matching order, with focused tests covering both cases.

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
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.