python / python/mypy

@dataclass_transform not recognized when used with an instance method

Aperta
#16,527 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-dataclass-transform
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci l’esempio del metodo di istanza usando il playground mypy-play collegato e confrontalo con l’esempio del wrapper di primo livello. Traccia il modo in cui mypy gestisce @dataclass_transform sul metodo, quindi aggiungi una coverage che dimostri che Person(name="test") viene accettato senza un errore unexpected-keyword imprevisto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.