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 — Unexpected behavior with dataclasses and weakref · Issue #102069 · python/cpython

Unexpected behavior with dataclasses and weakref #102069

New issue
Copy link
New issue
Copy link
Closed
Closed
Unexpected behavior with dataclasses and weakref#102069
Copy link
Assignees
sobolevn
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

@jbaudisch
jbaudisch
opened on Feb 20, 2023
Issue body actions

We recently discovered the following error in our project:
TypeError: descriptor '__weakref__' for 'XYZ' objects doesn't apply to a 'XYZ' object

XYZ in our case is a dataclass which uses slots (added v3.10) and weakref_slot (added v3.11) parameters.

We further investigated the error and have come to the following example to reproduce it:

import dataclasses
import weakref


@dataclasses.dataclass(slots=True, weakref_slot=True)
class EntityAsDataclass:
    x: int = 10


class Entity:
    __slots__ = ("x", "__weakref__")

    def __init__(self, x: int = 10) -> None:
        self.x = x


e1 = Entity()
e1_ref = weakref.ref(e1)

e2 = EntityAsDataclass()
e2_ref = weakref.ref(e2)


assert e1.__weakref__ is e1_ref  # works
assert e2.__weakref__ is e2_ref  # fails with "TypeError: descriptor '__weakref__' for 'EntityAsDataclass' objects doesn't apply to a 'EntityAsDataclass' object"

I don't know if this is intended, but we expect EntityAsDataclass and Entity to have the same behavior here.

Thanks!

Linked PRs

  • gh-102069: Fix __weakref__ descriptor generation for custom dataclasses #102075
  • [3.11] gh-102069: Fix __weakref__ descriptor generation for custom dataclasses (GH-102075) #102662

Activity

  1. AlexWaygood
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    3.11only security fixesonly security fixes
    on Feb 20, 2023
  2. sobolevn
    self-assigned this
    on Feb 20, 2023
  3. sobolevn commented on Feb 20, 2023

    @sobolevn
    sobolevn
    on Feb 20, 2023
    Member
    More actions

    I know how to fix it! Thanks for the report! PR is on its way.

    Reacted by Justin Baudisch and Carl Meyer
  4. sobolevn
    added a commit that references this issue on Feb 20, 2023

    pythongh-102069: Fix `__weakref__` descriptor generation for custom d…

    a388296
  5. sobolevn
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Feb 21, 2023
  6. carljm
    added a commit that references this issue on Mar 13, 2023

    gh-102069: Fix `__weakref__` descriptor generation for custom datacla…

    d97757f
  7. miss-islington
    added a commit that references this issue on Mar 13, 2023

    pythongh-102069: Fix `__weakref__` descriptor generation for custom d…

    ad32998
  8. carljm
    added a commit that references this issue on Mar 13, 2023

    [3.11] gh-102069: Fix `__weakref__` descriptor generation for custom …

    b0e221c
  9. carljm
    closed this as completedon Mar 13, 2023
  10. carljm
    added a commit that references this issue on Mar 14, 2023

    Merge branch 'main' into inlinecomp2

    6c5f269
  11. Fidget-Spinner
    added a commit that references this issue on Mar 27, 2023

    pythongh-102069: Fix `__weakref__` descriptor generation for custom d…

    98b34ba
  12. warsaw
    added a commit that references this issue on Apr 11, 2023

    pythongh-102069: Fix `__weakref__` descriptor generation for custom d…

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

Metadata

Metadata

Assignees

  • @sobolevn
    sobolevn

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.