python / python/mypy

Stubbed Enum classes does not allow indexing

Open
#8,968 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug false-positive priority-1-normal topic-enum
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

I am seeing the issue described here: https://github.com/python/mypy/issues/3137 again on mypy 0.780, but only if the enum is stubbed

# test.py
from package import E
name = 'a'
val = E[name]
assert val == E.a
# package/__init__.pyi
import package.test_def
E = package.test_def.E
# package/test_def.py
from enum import Enum

class E(Enum):
    a = 'a'
    b = 'b'
    c = 'c'

In terminal

$ python -m mypy --version                                                                                                                                          
mypy 0.780
$ python -m mypy ./
test.py:4: error: "E" expects no type arguments, but 1 given
test.py:4: error: Variable "test.name" is not valid as a type
test.py:4: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases

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 with the minimal reproduction in test.py, package/init.pyi, and package/test_def.py, then run mypy 0.780 or the repository's equivalent test command to confirm the diagnostics. Trace how the stubbed enum alias is analyzed, and consider the issue done when indexing E by a string is accepted and the example type-checks without the reported errors.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.