Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dataclass(slots=True) incompatible with __init_subclass__ #91126

Open
crusaderky mannequin opened this issue Mar 10, 2022 · 2 comments
Open

dataclass(slots=True) incompatible with __init_subclass__ #91126

crusaderky mannequin opened this issue Mar 10, 2022 · 2 comments
Assignees
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@crusaderky
Copy link
Mannequin

crusaderky mannequin commented Mar 10, 2022

BPO 46970
Nosy @ericvsmith, @crusaderky

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/ericvsmith'
closed_at = None
created_at = <Date 2022-03-10.00:14:28.417>
labels = ['type-bug', 'library', '3.10']
title = 'dataclass(slots=True) incompatible with __init_subclass__'
updated_at = <Date 2022-03-10.00:28:03.767>
user = 'https://github.com/crusaderky'

bugs.python.org fields:

activity = <Date 2022-03-10.00:28:03.767>
actor = 'eric.smith'
assignee = 'eric.smith'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2022-03-10.00:14:28.417>
creator = 'crusaderky'
dependencies = []
files = []
hgrepos = []
issue_num = 46970
keywords = []
message_count = 2.0
messages = ['414822', '414823']
nosy_count = 2.0
nosy_names = ['eric.smith', 'crusaderky']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue46970'
versions = ['Python 3.10']

@crusaderky
Copy link
Mannequin Author

crusaderky mannequin commented Mar 10, 2022

Related to bpo-46382
A class decorated with dataclass(slots=True) can't pass any parameters to the __init_subclass__ method of its parent class.

from dataclasses import dataclass

class A:
    __slots__ = ()
    def __init_subclass__(cls, msg):
        print(msg)

@dataclass(slots=True)
class B(A, msg="Hello world!"):
    pass

File "lib/python3.10/dataclasses.py", line 1145, in _add_slots
cls = type(cls)(cls.__name__, cls.__bases__, cls_dict)
TypeError: A.__init_subclass__() missing 1 required positional argument: 'msg'

@crusaderky crusaderky mannequin added 3.10 only security fixes stdlib Python modules in the Lib dir type-crash A hard crash of the interpreter, possibly with a core dump labels Mar 10, 2022
@ericvsmith
Copy link
Member

This appears to be due to dataclasses needing to create a new class in order to set __slots__. I'll look at it, but I doubt there's anything that can be done.

attrs has the same issue:
File "xxxxx/.local/lib/python3.8/site-packages/attr/make.py", line 889, in _create_slots_class
cls = type(self._cls)(self._cls.__name
_, self._cls.__bases__, cd)
TypeError: __init_subclass__() missing 1 required positional argument: 'msg'

@ericvsmith ericvsmith self-assigned this Mar 10, 2022
@ericvsmith ericvsmith added type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Mar 10, 2022
@ericvsmith ericvsmith self-assigned this Mar 10, 2022
@ericvsmith ericvsmith added type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Mar 10, 2022
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
andreabak added a commit to andreabak/sibilant that referenced this issue Aug 27, 2023
The main change is to remove slots=True from dataclasses decorators,
due to CPython bug: python/cpython#91126
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant