python / python/mypy

No-field class derived from `dataclass_transform` base class uses base class's constructor signature

Open
#19,452 1 comment 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

When a class derives from a base class marked with dataclass_transform and the derived class has no fields, mypy incorrectly infers that the derived class does not provide its own constructor. This issue does not happen if the derived class has any fields.

Pyright infers the correct constructor signature here.

To Reproduce

from typing import dataclass_transform, reveal_type

@dataclass_transform()
class Base:
   def __init__( self, a: int ) -> None:
      pass

class DerivedNoFields( Base ):
   pass

class DerivedWithFields( Base ):
   b: str

reveal_type( DerivedNoFields.__init__ )
reveal_type( DerivedWithFields.__init__ )

Playground link

Expected Behavior

main.py:14: note: Revealed type is "def (self: __main__.DerivedNoFields)"
main.py:15: note: Revealed type is "def (self: __main__.DerivedWithFields, b: builtins.str)"

Actual Behavior

main.py:14: note: Revealed type is "def (self: __main__.Base, a: builtins.int)"
main.py:15: note: Revealed type is "def (self: __main__.DerivedWithFields, b: builtins.str)"

Your Environment

  • Mypy version used: 3.16.1
  • Python version used: 3.13
  • No command-line flags or configuration options

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

Start by running the main.py reproduction from the issue or its mypy-play link and compare the two reveal_type results. Trace the dataclass_transform constructor-synthesis entry point and existing tests, then add coverage for the no-field derived class; done means DerivedNoFields.__init__ has only self while the field-bearing case remains unchanged.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.