python / python/mypy

mypy Type error for a class with an imported metaclass

Offen
#9,185 15 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug false-positive priority-1-normal topic-metaclasses
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem bereitgestellten minimalen Enum- und Metaklassenbeispiel und prüfe es einmal mit lokal definiertem MyEnumMetaClass und einmal, wenn es aus einem Paket importiert wird. Verfolge den Unterschied in mypys Behandlung der Klasse und bestätige, dass die importierte Version den gemeldeten Fehler, dass sie nicht generisch und nicht indizierbar ist, nicht mehr erzeugt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.