Skip to content

dataclass(slots=True) incompatible with __init_subclass__ #91126

Closed

Description

@crusaderky
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']

Linked PRs

Activity

  1. crusaderky commented on Mar 10, 2022

    crusaderkymannequin
    MannequinAuthor

    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'

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Mar 10, 2022
  3. ericvsmith commented on Mar 10, 2022

    @ericvsmith
    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'

  4. self-assigned this
    on Mar 10, 2022
  5. added
    type-bugAn unexpected behavior, bug, or error
    and removed
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Mar 10, 2022
  6. self-assigned this
    on Mar 10, 2022
  7. added
    type-bugAn unexpected behavior, bug, or error
    and removed
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Mar 10, 2022
  8. transferred this issue fromon Apr 10, 2022
  9. added a commit that references this issue on Aug 27, 2023
  10. sobolevn commented on Aug 23, 2024

    @sobolevn
    Member

    I think that we should add this case to the docs. Because there's really nothing we can do to support this case.

    Solutions:

    1. Don't do that
    2. Don't use slots=True, write manual slots
    3. Have defaults for __init_subclass__

    I will send a PR with the proposed note about that.

  11. self-assigned this
    on Aug 23, 2024
  12. added 2 commits that reference this issue on Aug 26, 2024
  13. added 2 commits that reference this issue on Sep 1, 2024
  14. added 2 commits that reference this issue on Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

3.10 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytopic-dataclassestype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions