python / python/mypy

mypy Type error for a class with an imported metaclass

Open
#9,185 15 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Please provide more information to help us understand the issue:

  • Are you reporting a bug, or opening a feature request? bug
  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.
    Here is an example class I am working with.
@enum.unique
class MyEnumClass(Enum, metaclass=MyEnumMetaClass):
    OUT_OF_STOCK = "Out of Stock"
    LOW_STOCK = "Low Stock"
    IN_STOCK = "In Stock"
    EXCESS = "Excess"

I am making a change where I am importing the MyEnumMetaClass class from a PIP package now, whereas before it was located within the same project as this class.

  • What is the actual behavior/output?
    With MyEnumMetaClass imported, whenever I create a MyEnumClass instance, mypy gives me the following error:
    error: The type "Type[MyEnumClass]" is not generic and not indexable
  • What is the behavior/output you expect?
    I expect mypy to succeed here, given that the code is the same, it is just imported from a package now.

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 supplied minimal enum and metaclass example, checking it once with MyEnumMetaClass defined locally and once when imported from a package. Trace the difference in mypy's handling of the class and confirm that the imported version no longer produces the reported non-generic, non-indexable error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.