Skip to content

can't specify generic types when instantiating frozen dataclass with slots #116669

Description

@daddinuz

Bug report

Bug description:

from dataclasses import dataclass

@dataclass(frozen=True, slots=True)
class Foo[T]:
  pass

foo = Foo() # this line works
foo = Foo[int]() # this one does not
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.12/typing.py", line 1142, 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

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

Activity

  1. added
    3.12only security fixes
    3.13only security fixes
    on Mar 12, 2024
  2. Eclips4 commented on Mar 12, 2024

    @Eclips4
    Member

    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
  3. AlexWaygood commented on Mar 12, 2024

    @AlexWaygood
    Member

    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 typing module is in the clear here.

    However, it looks like dataclasses might also be using super() incorrectly there? Not sure.

  4. Eclips4 commented on Mar 12, 2024

    @Eclips4
  5. JelleZijlstra commented on Mar 12, 2024

    @JelleZijlstra
    Member

    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.

  6. AlexWaygood commented on Mar 12, 2024

    @AlexWaygood
    Member

    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

  7. JelleZijlstra commented on Mar 12, 2024

    @JelleZijlstra
    Member
  8. AlexWaygood commented on Mar 12, 2024

    @AlexWaygood
    Member

    Thanks for the report, @daddinuz! Your bug was valid; we'd just already fixed it before you filed the issue ;)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixes3.12only security fixes3.13only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions