python / python/typeshed

Wrong Signature for Enum with Specific Dataclass Mixin Scenario

Offen
#15,908 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Python
Sterne
5.1k
Forks
2.1k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
82

Beschreibung

Note: I was thinking about putting this on pyright but it said standard library problems are more suited here, so I'll give it a shot.

I was trying to code an enum with a dataclass mixin where each value is overridden through the first string variable for easier referencing (rather than a dict from str to enum).

from dataclasses import dataclass
from enum import Enum, auto

@dataclass(frozen=True)
class SampleMixin:
    a_string: str
    a_number: int

class SampleEnum(SampleMixin, Enum):
    def __new__(cls, value: str, *_):
        obj = object.__new__(cls)
        obj._value_ = value

        return obj

    foo = "foo", 1
    bar = "bar", 2

class SampleEnum2(Enum):
    foo = auto()
    bar = auto()

baz = SampleEnum("foo")
qux = SampleEnum2("foo")

baz still gets SampleEnum.foo right, but the function signature is incorrectly identified. Ideally, it should be closer to SampleEnum2's function signature.

Image

I'm open to workarounds at the meantime. The code runs correctly, it's just that the inferred signature I got was wrong.

Code sample in pyright playground.

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 der Python-Reproduktion und dem verknüpften pyright playground und vergleiche die abgeleiteten Signaturen für SampleEnum und SampleEnum2. Verfolge die relevanten Typing-Definitionen für enum und dataclass aus der Standardbibliothek in typeshed und bestätige den Abschluss, sobald der dataclass-mixin-Fall die beabsichtigte enum-Konstruktor-Signatur erzeugt, ohne das Laufzeitverhalten zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
developer-experience
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
55/100

Neue Issues direkt in Ihr Postfach

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