@dataclass_transform not recognized when used with an instance method
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
Mypy does not seem to recognize @dataclass_transform when used on an instance method.
To Reproduce
For top level dataclass wrappers (as a func), it seems to work, as taken from the docs:
To have Mypy recognize a wrapper of dataclasses.dataclass as a dataclass decorator, consider using the dataclass_transform() decorator:
from dataclasses import dataclass, Field
from typing import TypeVar, dataclass_transform
T = TypeVar('T')
@dataclass_transform(field_specifiers=(Field,))
def my_dataclass(cls: type[T]) -> type[T]:
...
return dataclass(cls)
@my_dataclass
class Person:
name: str
person = Person(name="test") # this works
Gist URL: https://gist.github.com/mypy-play/db55cfe5da202e047d4d0aebef18868f
Playground URL: https://mypy-play.net/?mypy=latest&python=3.11&gist=db55cfe5da202e047d4d0aebef18868f
However if the dataclass wrapper is defined as an instance method, it does not work:
from dataclasses import Field, dataclass
from typing import TypeVar
from typing_extensions import dataclass_transform
T = TypeVar("T")
class Converter:
@dataclass_transform(field_specifiers=(Field,))
def my_dataclass(self, cls: type[T]) -> type[T]:
return dataclass(cls)
converter = Converter()
@converter.my_dataclass
class Person:
name: str
person = Person(name="test") # this fails
Gist URL: https://gist.github.com/mypy-play/b73870420265cbca2345a07890ab674a
Playground URL: https://mypy-play.net/?mypy=latest&python=3.11&gist=b73870420265cbca2345a07890ab674a
Expected Behavior
I'd expect the instance-method wrapper function to behave the same as a top-level wrapper, and recognize this as a dataclass-wrapping method.
Actual Behavior
error: Unexpected keyword argument "name" for "Person" [call-arg]
Your Environment
- Mypy version used: my-py playground (latest, 1.7.0)
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used: 3.11
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere das Beispiel für die Instanzmethode mit dem verlinkten mypy-play-Spielplatz und vergleiche es mit dem Beispiel für den Wrapper auf oberster Ebene. Verfolge, wie mypy @dataclass_transform auf der Methode verarbeitet, und füge eine Abdeckung hinzu, die zeigt, dass Person(name="test") ohne einen unerwarteten unexpected-keyword-Fehler akzeptiert wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100