python / python/mypy

`dataclass_transform` decorator not detected when using alias

Open
#19,731 4 comments 1 reaction 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

I came across this while trying to conditionally use typing.dataclass_transform or typing_extensions.dataclass_transforms depending on the Python version running the code.

If one defines an alias for dataclass_transform, using the alias as a decorator does not result in the expected result (mypy does not recognize the decorated class as if being decorated with dataclass_transform)

To Reproduce

Playground link: https://mypy-play.net/?mypy=latest&python=3.12&gist=c88ba0d811ade24ad47f302dfdb5b6f7

import dataclasses
import typing

dataclass_transform = typing.dataclass_transform


@dataclass_transform()
def transform(cls, /, **kwargs):
    return dataclasses.dataclass(**kwargs)


@transform
class Example:
    field: int


Example(field=1)

Expected Behavior

No error, same as if typing.dataclass_transform was being used directly

Actual Behavior
Example is not recognized as a dataclass-like class:

main.py:17: error: Unexpected keyword argument "field" for "Example"  [call-arg]

Your Environment

  • Mypy version used: 1.17.1
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12

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 report in the linked mypy playground using the shown Python 3.12 example and confirm the error reported for main.py. Trace dataclass_transform handling from that reproduction; the work is done when the alias-decorated Example produces no error and accepts Example(field=1).

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.