GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Cannot specify `weakref_slot=True` twice in dataclass MRO · Issue #93521 · python/cpython

Cannot specify weakref_slot=True twice in dataclass MRO #93521

New issue
Copy link
New issue
Copy link
Closed
#93535
Closed
Cannot specify weakref_slot=True twice in dataclass MRO#93521
#93535
Copy link
Assignees
ericvsmith
Labels
3.11only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@Bluenix2
Bluenix2
opened on Jun 5, 2022
Issue body actions

Summary

If a child dataclass of a parent dataclass who specified weakref_slot, also decides to set weakref_slot to True, Python will raise a TypeError as seen below:

TypeError: __weakref__ slot disallowed: either we already got one, or __itemsize__ != 0

Description

I am porting dataclasses' slots and weakref slots kwargs into a decorator I can use. While doing so, I decided to change the default of the weakref_slot because I would like all of my classes to be weakref:able. This is when I ran into the issue which can be reproduced by the code below:

from dataclasses import dataclass


@dataclass(slots=True, weakref_slot=True)
class A:
    field: str


@dataclass(slots=True, weakref_slot=True)
class B(A):
    ...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "C:\Users\%username%\Projects\testing\copyclasses.py", line 1207, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash,
  File "C:\Users\%username%\Projects\testing\copyclasses.py", line 1108, in _process_class
    cls = _add_slots(cls, frozen, weakref_slot)
  File "C:\Users\%username%\Projects\testing\copyclasses.py", line 1176, in _add_slots    
    cls = type(cls)(cls.__name__, cls.__bases__, cls_dict)
TypeError: __weakref__ slot disallowed: either we already got one, or __itemsize__ != 0

Environment

This was tested by creating a file named copyclasses.py with the current contents of Lib/dataclasses.py ran using a 3.10 CPython interpreter. Note that I've restored the naming of the module in the codeblock above assuming those who will be running it will have a built 3.11 CPython interpreter.


Footnotes: I will mention @ericvsmith as the code owner of the dataclasses file.

Activity

  1. Bluenix2
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jun 5, 2022
  2. JelleZijlstra
    added
    3.11only security fixesonly security fixes
    on Jun 5, 2022
  3. AlexWaygood
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Jun 5, 2022
  4. AlexWaygood
    assigned
    ericvsmith
    on Jun 5, 2022
  5. ericvsmith commented on Jun 5, 2022

    @ericvsmith
    ericvsmith
    on Jun 5, 2022
    Member
    More actions

    Here's an example of the same thing without dataclasses:

    class A:
        __slots__='__weakref__'
    
    class B(A):
        __slots__='__weakref__'
    Traceback (most recent call last):
      File "gh-93521.py", line 4, in <module>
        class B(A):
    TypeError: __weakref__ slot disallowed: either we already got one, or __itemsize__ != 0
    
  6. ericvsmith commented on Jun 5, 2022

    @ericvsmith
    ericvsmith
    on Jun 5, 2022
    Member
    More actions

    I'm not sure this is a bug, just the way things work.

  7. JelleZijlstra commented on Jun 6, 2022

    @JelleZijlstra
    JelleZijlstra
    on Jun 6, 2022
    Member
    More actions

    It would be nice if dataclasses took care of this issue automatically though.

    Example scenario: I have a library in which I expose a dataclass. A user subclasses my class in another dataclass, and wants to use weakrefs, so they use weakref_slots. Then I change my library to allow weakrefs on the base class, but now the user's child class is broken.

  8. Bluenix2 commented on Jun 6, 2022

    @Bluenix2
    Bluenix2
    on Jun 6, 2022
    ContributorAuthor
    More actions

    Locally I fixed this already, and I am planning on opening a PR with an added test. Should that PR point to main or the 3.11 branch (seeing as both branches should get the patch)?

  9. ericvsmith commented on Jun 6, 2022

    @ericvsmith
    ericvsmith
    on Jun 6, 2022
    Member
    More actions

    main

    Reacted by Bluenix
  10. Bluenix2
    added a commit that references this issue on Jun 6, 2022

    pythonGH-93521: Filter out `__weakref__` slot if present in bases

    abdab30
  11. ericvsmith
    closed this as completedin #93535on Jun 8, 2022
  12. ericvsmith
    added a commit that references this issue on Jun 8, 2022

    GH-93521: For dataclasses, filter out `__weakref__` slot if present i…

    5849af7
  13. miss-islington
    added 2 commits that reference this issue on Jun 8, 2022

    pythonGH-93521: For dataclasses, filter out `__weakref__` slot if pre…

    79dabf2

    GH-93521: For dataclasses, filter out `__weakref__` slot if present i…

    121ab58
  14. serhiy-storchaka
    added a commit that references this issue on Jun 26, 2022

    Revert 30935 defer bpo45162 to 312 (#94285)

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

Metadata

Metadata

Assignees

  • @ericvsmith
    ericvsmith

Labels

3.11only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.