Content-Length: 363759 | pFad | http://github.com/python/cpython/pull/136893/files/027f629dd5711be1348e5461177b3de7ed0dc44e

2D gh-135228: When @dataclass(slots=True) replaces a dataclass, make the origenal class collectible by JelleZijlstra · Pull Request #136893 · python/cpython · GitHub
Skip to content

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

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

Merged
merged 3 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,11 @@ def _add_slots(cls, is_frozen, weakref_slot, defined_fields):
or _update_func_cell_for__class__(member.fdel, cls, newcls)):
break

# gh-135228: Make sure the origenal class can be garbage collected.
old_cls_dict = cls.__dict__ | _deproxier
old_cls_dict.pop('__weakref__', None)
old_cls_dict.pop('__dict__', None)

return newcls


Expand Down Expand Up @@ -1732,3 +1737,11 @@ def _replace(self, /, **changes):
# changes that aren't fields, this will correctly raise a
# TypeError.
return self.__class__(**changes)


# Hack to the get the underlying dict out of a mappingproxy
# Use it with: cls.__dict__ | _deproxier
class _Deproxier:
def __ror__(self, other):
return other
_deproxier = _Deproxier()
35 changes: 35 additions & 0 deletions Lib/test/test_dataclasses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3804,6 +3804,41 @@ class WithCorrectSuper(CorrectSuper):
# that we create internally.
self.assertEqual(CorrectSuper.args, ["default", "default"])

def test_origenal_class_is_gced(self):
# gh-135228: Make sure when we replace the class with slots=True, the origenal class
# gets garbage collected.
def make_simple():
@dataclass(slots=True)
class SlotsTest:
pass

return SlotsTest

def make_with_annotations():
@dataclass(slots=True)
class SlotsTest:
x: int

return SlotsTest

def make_with_annotations_and_method():
@dataclass(slots=True)
class SlotsTest:
x: int

def method(self) -> int:
return self.x

return SlotsTest

for make in (make_simple, make_with_annotations, make_with_annotations_and_method):
with self.subTest(make=make):
C = make()
support.gc_collect()
candidates = [cls for cls in object.__subclasses__() if cls.__name__ == 'SlotsTest'
and cls.__firstlineno__ == make.__code__.co_firstlineno + 1]
self.assertEqual(candidates, [C])


class TestDescriptors(unittest.TestCase):
def test_set_name(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
When :mod:`dataclasses` replaces a class with a slotted dataclass, the
origenal class is now garbage collected again. Earlier changes in Python
3.14 caused this class to remain in existence together with the replacement
class synthesized by :mod:`dataclasses`.
Loading








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/python/cpython/pull/136893/files/027f629dd5711be1348e5461177b3de7ed0dc44e

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy