Repository navigation
can't specify generic types when instantiating frozen dataclass with slots #116669
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Mar 12, 2024 - addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Mar 12, 2024 - added3.12only security fixesonly security fixes3.13only security fixesonly security fixes
on Mar 12, 2024 FYI: this doesn't relate to the new (PEP 695) syntax.
Example with the old syntax:from dataclasses import dataclass from typing import TypeVar, Generic T = TypeVar("T") @dataclass(frozen=True, slots=True) class Foo(Generic[T]): pass foo = Foo() # this line works foo = Foo[int]() # this one does not
Traceback (most recent call last): File "/home/eclips4/CLionProjects/cpython/example.py", line 13, in <module> foo = Foo[int]() # this one does not ^^^^^^^^^^ File "/usr/local/lib/python3.11/typing.py", line 1277, in __call__ result.__orig_class__ = self ^^^^^^^^^^^^^^^^^^^^^ File "<string>", line 5, in __setattr__ TypeError: super(type, obj): obj must be an instance or subtype of type
Happily, this specific exception was fixed just last month, in #115213. The fix was backported, so it will be included in the next releases of Python 3.12 and Python 3.11. The
typingmodule is in the clear here.However, it looks like
dataclassesmight also be usingsuper()incorrectly there? Not sure.Reacted by Kirill PodoprigoraThis is a symptom of the general problem where slotted dataclasses work by creating a new class, which breaks
super(). There are several other similar issues and there's no obvious solution.cc @ericvsmith for dataclasses.
This is a symptom of the general problem where slotted dataclasses work by creating a new class, which breaks
super(). There are several other similar issues and there's no obvious solution.cc @ericvsmith for dataclasses.
If there are already other similar issues regarding
super()in dataclasses, then we can probably close this one, as the user-visible bug here has already been fixed by #115213- Reacted by Alex Waygood
Thanks for the report, @daddinuz! Your bug was valid; we'd just already fixed it before you filed the issue ;)
Reacted by daddinuz
Bug report
Bug description:
Python 3.12.2 (main, Feb 21 2024, 00:00:00) [GCC 13.2.1 20231205 (Red Hat 13.2.1-6)]CPython versions tested on:
3.12
Operating systems tested on:
Linux