Skip to content

Incorrect __qualname__ in slotted dataclasses in __init_subclass__ #129915

@thejcannon

Description

@thejcannon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtopic-dataclassestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions