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

Unexpected behavior with dataclasses and weakref #102069

Closed
jbaudisch opened this issue Feb 20, 2023 · 1 comment
Closed

Unexpected behavior with dataclasses and weakref #102069

jbaudisch opened this issue Feb 20, 2023 · 1 comment
Assignees
Labels
3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@jbaudisch
Copy link

jbaudisch commented Feb 20, 2023

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

@AlexWaygood AlexWaygood added stdlib Python modules in the Lib dir 3.11 only security fixes labels Feb 20, 2023
@sobolevn sobolevn self-assigned this Feb 20, 2023
@sobolevn
Copy link
Member

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

sobolevn added a commit to sobolevn/cpython that referenced this issue Feb 20, 2023
@sobolevn sobolevn added the type-bug An unexpected behavior, bug, or error label Feb 21, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 13, 2023
…ataclasses (pythonGH-102075)

(cherry picked from commit d97757f)

Co-authored-by: Nikita Sobolev <[email protected]>
carljm pushed a commit that referenced this issue Mar 13, 2023
…dataclasses (GH-102075) (#102662)

gh-102069: Fix `__weakref__` descriptor generation for custom dataclasses (GH-102075)
(cherry picked from commit d97757f)

Co-authored-by: Nikita Sobolev <[email protected]>
@carljm carljm closed this as completed Mar 13, 2023
carljm added a commit to carljm/cpython that referenced this issue Mar 14, 2023
* main: (50 commits)
  pythongh-102674: Remove _specialization_stats from Lib/opcode.py (python#102685)
  pythongh-102660: Handle m_copy Specially for the sys and builtins Modules (pythongh-102661)
  pythongh-102354: change python3 to python in docs examples (python#102696)
  pythongh-81057: Add a CI Check for New Unsupported C Global Variables (pythongh-102506)
  pythonGH-94851: check unicode consistency of static strings in debug mode (python#102684)
  pythongh-100315: clarification to `__slots__` docs. (python#102621)
  pythonGH-100227: cleanup initialization of global interned dict (python#102682)
  doc: Remove a duplicate 'versionchanged' in library/asyncio-task (pythongh-102677)
  pythongh-102013: Add PyUnstable_GC_VisitObjects (python#102014)
  pythonGH-102670: Use sumprod() to simplify, speed up, and improve accuracy of statistics functions (pythonGH-102649)
  pythongh-102627: Replace address pointing toward malicious web page (python#102630)
  pythongh-98831: Use DECREF_INPUTS() more (python#102409)
  pythongh-101659: Avoid Allocation for Shared Exceptions in the _xxsubinterpreters Module (pythongh-102659)
  pythongh-101524: Fix the ChannelID tp_name (pythongh-102655)
  pythongh-102069: Fix `__weakref__` descriptor generation for custom dataclasses (python#102075)
  pythongh-98169 dataclasses.astuple support DefaultDict (python#98170)
  pythongh-102650: Remove duplicate include directives from multiple source files (python#102651)
  pythonGH-100987: Don't cache references to the names and consts array in `_PyEval_EvalFrameDefault`. (python#102640)
  pythongh-87092: refactor assemble() to a number of separate functions, which do not need the compiler struct (python#102562)
  pythongh-102192: Replace PyErr_Fetch/Restore etc by more efficient alternatives (python#102631)
  ...
Fidget-Spinner pushed a commit to Fidget-Spinner/cpython that referenced this issue Mar 27, 2023
warsaw pushed a commit to warsaw/cpython that referenced this issue Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 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

4 participants