Skip to content

dataclass_transform decorator not detected when using alias #19731

Open

Description

@facutuesca

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

Activity

  1. sterliakov commented on Aug 27, 2025

    @sterliakov
    Collaborator

    There are multiple hardcoded fullnames in mypy that do not support aliasing. The same applies to typing.no_type_check and typing.TypedDict, for example.

    https://mypy-play.net/?mypy=master&python=3.10&flags=strict&gist=e75986dae521c8386ff818e57e79ad40

    Apparently dataclasses.dataclass can't be aliased either.

  2. alex commented on Sep 1, 2025

    @alex
    Member

    Is this a fundamental limitation of mypy's design, or just an incidental limitation for how these APIs were implemented?

  3. sterliakov commented on Sep 1, 2025

    @sterliakov
    Collaborator

    It's an implementation issue AFAIC. Maybe I'll try to get it working this week, there are multiple tickets caused by this.

  4. alex commented on Sep 1, 2025

    @alex
    Member

    Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions