python / python/cpython

Incorrect `__qualname__` in slotted dataclasses in `__init_subclass__`

Open
#129,915 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-dataclasses type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

In the following:

import dataclasses

@dataclasses.dataclass(slots=True)
class Base:
    def __init_subclass__(cls) -> None:
        print(cls.__qualname__)


class Container:
    @dataclasses.dataclass(slots=True)
    class Dataclass(Base):
        pass

I get:

Container.Dataclass
Dataclass

(I can handle the double-call, but the second call having incorrect __qualname__ is just really annoying 😂 )


To clarify the annoyance, the real world use case looks something like:

@dataclasses.dataclass(slots=True)
class Base:
    def __init_subclass__(cls) -> None:
        if "__slots__" not in cls.__dict__:
            return  # We're the unslotted class, wait until we're the slotted "real" one
        
        ... # Some code here relying on `__qualname__`
CPython versions tested on:

3.11

Operating systems tested on:

macOS

Linked PRs
  • gh-129916

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 nested slotted-dataclass reproducer in the issue and read the linked PR gh-129916. Trace the dataclasses slots handling around the second init_subclass call, then add or run a regression test showing that the nested class retains its correct qualname.

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
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.