Description
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