Skip to content

gh-135228: When @dataclass(slots=True) replaces a dataclass, make the original class collectible - #136893

Merged
JelleZijlstra merged 3 commits into
python:mainfrom
JelleZijlstra:hackweakref
Jul 22, 2025
Merged

JelleZijlstra merged 3 commits into
python:mainfrom
JelleZijlstra:hackweakref

Conversation

@JelleZijlstra

@JelleZijlstra JelleZijlstra commented Jul 20, 2025 •

Copy link
Copy Markdown
Member

An interesting hack, but more localized in scope than #135230.

This may be a breaking change if people intentionally keep the original class around
when using @dataclass(slots=True), and then use __dict__ or __weakref__ on the
original class.

…ke the original class collectible

An interesting hack, but more localized in scope than python#135230.

This may be a breaking change if people intentionally keep the original class around
when using `@dataclass(slots=True)`, and then use `__dict__` or `__weakref__` on the
original class.
@ncoghlan

Copy link
Copy Markdown
Contributor

Relying on the __ror__ mapping proxy bypass is unpleasant, but this seems like the simplest near-term workaround, since even though del cls.__weakref__ works without any tricks, del cls.__dict__ fails due to the way that descriptor is defined.

@ncoghlan ncoghlan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While far from ideal, this mitigation feels like a better state to leave things in while we ponder the possibilities for a cleaner solution.

Suggested tweaks inline to make it clearer that it's only del cls.__dict__ that needs the mapping proxy bypass.

Comment thread Lib/dataclasses.py Resolved
Comment thread Lib/dataclasses.py Resolved
Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
@JelleZijlstra JelleZijlstra added the needs backport to 3.14 bugs and security fixes label Jul 22, 2025
Comment thread Lib/dataclasses.py Resolved
@JelleZijlstra
JelleZijlstra merged commit 46cbdf9 into python:main Jul 22, 2025
@miss-islington-app

Copy link
Copy Markdown

Thanks @JelleZijlstra for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 22, 2025
…ke the original class collectible (pythonGH-136893)

An interesting hack, but more localized in scope than pythonGH-135230.

This may be a breaking change if people intentionally keep the original class around
when using `@dataclass(slots=True)`, and then use `__dict__` or `__weakref__` on the
original class.
(cherry picked from commit 46cbdf9)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
@bedevere-app

bedevere-app Bot commented Jul 22, 2025

Copy link
Copy Markdown

GH-136960 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jul 22, 2025
@JelleZijlstra
JelleZijlstra deleted the hackweakref branch July 22, 2025 04:51
JelleZijlstra added a commit that referenced this pull request Jul 22, 2025
…ake the original class collectible (GH-136893) (#136960)

gh-135228: When @DataClass(slots=True) replaces a dataclass, make the original class collectible (GH-136893)

An interesting hack, but more localized in scope than GH-135230.

This may be a breaking change if people intentionally keep the original class around
when using `@dataclass(slots=True)`, and then use `__dict__` or `__weakref__` on the
original class.
(cherry picked from commit 46cbdf9)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
hynek added a commit to python-attrs/attrs that referenced this pull request Jul 22, 2025
Ref python/cpython#136893 &
python/cpython#135228

Co-authored-by: Jelle Zijlstra
<906600+JelleZijlstra@users.noreply.github.com>
hynek added a commit to python-attrs/attrs that referenced this pull request Jul 22, 2025
Ref python/cpython#136893 &
python/cpython#135228

Co-authored-by: Jelle Zijlstra <906600+JelleZijlstra@users.noreply.github.com>
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this pull request Jul 22, 2025
…places a dataclass, make the original class collectible (pythonGH-136893) (python#136960)"

This reverts commit 6e1b31b.

Modifying the `__dict__` is likely to cause crashes
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this pull request Jul 22, 2025
…lass, make the original class collectible (python#136893)"

This reverts commit 46cbdf9.
JelleZijlstra added a commit that referenced this pull request Jul 23, 2025
…make the original class collectible (#136893)" (#137014)

This reverts commit 46cbdf9.
hynek added a commit to python-attrs/attrs that referenced this pull request Jul 23, 2025
Ref python/cpython#136893 &
python/cpython#135228

Co-authored-by: Jelle Zijlstra <906600+JelleZijlstra@users.noreply.github.com>
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this pull request Jul 23, 2025
…ke the original class collectible (python#136893)

An interesting hack, but more localized in scope than python#135230.

This may be a breaking change if people intentionally keep the original class around
when using `@dataclass(slots=True)`, and then use `__dict__` or `__weakref__` on the
original class.

Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
hynek added a commit to python-attrs/attrs that referenced this pull request Jul 25, 2025
Ref python/cpython#136893 &
python/cpython#135228

Co-authored-by: Jelle Zijlstra <906600+JelleZijlstra@users.noreply.github.com>
hynek added a commit to python-attrs/attrs that referenced this pull request Jul 25, 2025
Ref python/cpython#136893 &
python/cpython#135228

Co-authored-by: Jelle Zijlstra <906600+JelleZijlstra@users.noreply.github.com>
hynek added a commit to python-attrs/attrs that referenced this pull request Jul 27, 2025
Ref python/cpython#136893 &
python/cpython#135228

Co-authored-by: Jelle Zijlstra <906600+JelleZijlstra@users.noreply.github.com>
hynek added a commit to python-attrs/attrs that referenced this pull request Jul 27, 2025
Ref python/cpython#136893 &
python/cpython#135228

Co-authored-by: Jelle Zijlstra <906600+JelleZijlstra@users.noreply.github.com>
github-merge-queue Bot pushed a commit to python-attrs/attrs that referenced this pull request Jul 28, 2025
* Fix slotted reference cycles on 3.14

Ref python/cpython#136893 &
python/cpython#135228

Co-authored-by: Jelle Zijlstra <906600+JelleZijlstra@users.noreply.github.com>

* Add news fragment

* Bump version tag to CMA

Co-authored-by: Brandt Bucher
<40968415+brandtbucher@users.noreply.github.com>
Co-authored-by: Jelle Zijlstra
<906600+JelleZijlstra@users.noreply.github.com>

* Apply review comment

#1446 (comment)

* EAFP

---------

Co-authored-by: Jelle Zijlstra <906600+JelleZijlstra@users.noreply.github.com>
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
…ke the original class collectible (python#136893)

An interesting hack, but more localized in scope than python#135230.

This may be a breaking change if people intentionally keep the original class around
when using `@dataclass(slots=True)`, and then use `__dict__` or `__weakref__` on the
original class.

Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
…lass, make the original class collectible (python#136893)" (python#137014)

This reverts commit 46cbdf9.
Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request Aug 19, 2025
…ke the original class collectible (python#136893)

An interesting hack, but more localized in scope than python#135230.

This may be a breaking change if people intentionally keep the original class around
when using `@dataclass(slots=True)`, and then use `__dict__` or `__weakref__` on the
original class.

Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request Aug 19, 2025
…lass, make the original class collectible (python#136893)" (python#137014)

This reverts commit 46cbdf9.
kumaraditya303 pushed a commit to miss-islington/cpython that referenced this pull request Sep 9, 2025
…ass, make the original class collectible (pythonGH-136893) (python#136960)

pythongh-135228: When @DataClass(slots=True) replaces a dataclass, make the original class collectible (pythonGH-136893)

An interesting hack, but more localized in scope than pythonGH-135230.

This may be a breaking change if people intentionally keep the original class around
when using `@dataclass(slots=True)`, and then use `__dict__` or `__weakref__` on the
original class.
(cherry picked from commit 46cbdf9)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants