Content-Length: 299909 | pFad | http://github.com/python/cpython/issues/111500

E0 dataclass, slots, __post_init__ and super · Issue #111500 · python/cpython · GitHub
Skip to content

dataclass, slots, __post_init__ and super #111500

@voidspace

Description

@voidspace

Bug report

Bug description:

Using super() in __post_init__ on a dataclass with slots fails.

This code:

from dataclasses import dataclass

@dataclass(slots=True)
class Base:
    def __post_init__(self):
        pass

@dataclass(slots=True)
class Thing(Base):
    a: int
    b: int
    c: int = 0

    def __post_init__(self):
        self.c = self.a + self.b
        super().__post_init__()

t = Thing(1,3)

Produces this exception:

Traceback (most recent call last):
  File "C:\Users\michael.foord\Code\one-touch-switch-service\example.py", line 18, in <module>
    t = Thing(1,3)
        ^^^^^^^^^^
  File "<string>", line 6, in __init__
  File "C:\Users\michael.foord\Code\one-touch-switch-service\example.py", line 16, in __post_init__
    super().__post_init__()
    ^^^^^^^
TypeError: super(type, obj): obj must be an instance or subtype of type

The fix is to use Python 2 style super(Class, self).__post_init__().

CPython versions tested on:

3.11

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

Labels

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

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions









    ApplySandwichStrip

    pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


    --- a PPN by Garber Painting Akron. With Image Size Reduction included!

    Fetched URL: http://github.com/python/cpython/issues/111500

    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy