python / python/mypy

@dataclass_transform not recognized when used with an instance method

Open
#16,527 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-dataclass-transform
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the instance-method example using the linked mypy-play playground and compare it with the top-level wrapper example. Trace how mypy handles @dataclass_transform on the method, then add coverage showing that Person(name="test") is accepted without an unexpected-keyword error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.