Skip to content

Commit 2d3bd7c

Browse files
committed
Feedback from Petr
1 parent 8c2c1e1 commit 2d3bd7c

2 files changed

Lines changed: 6 additions & 11 deletions

File tree

Lib/dataclasses.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,10 @@ def _add_slots(cls, is_frozen, weakref_slot, defined_fields):
12831283
if '__slots__' in cls.__dict__:
12841284
raise TypeError(f'{cls.__name__} already specifies __slots__')
12851285

1286+
# gh-102069: Remove existing __weakref__ descriptor.
1287+
# gh-135228: Make sure the original class can be garbage collected.
1288+
sys._clear_type_descriptors(cls)
1289+
12861290
# Create a new dict for our new class.
12871291
cls_dict = dict(cls.__dict__)
12881292
field_names = tuple(f.name for f in fields(cls))
@@ -1300,12 +1304,6 @@ def _add_slots(cls, is_frozen, weakref_slot, defined_fields):
13001304
# available in _MARKER.
13011305
cls_dict.pop(field_name, None)
13021306

1303-
# Remove __dict__ itself.
1304-
cls_dict.pop('__dict__', None)
1305-
1306-
# Clear existing `__weakref__` descriptor, it belongs to a previous type:
1307-
cls_dict.pop('__weakref__', None) # gh-102069
1308-
13091307
# And finally create the class.
13101308
qualname = getattr(cls, '__qualname__', None)
13111309
newcls = type(cls)(cls.__name__, cls.__bases__, cls_dict)
@@ -1338,9 +1336,6 @@ def _add_slots(cls, is_frozen, weakref_slot, defined_fields):
13381336
or _update_func_cell_for__class__(member.fdel, cls, newcls)):
13391337
break
13401338

1341-
# gh-135228: Make sure the original class can be garbage collected.
1342-
sys._clear_type_descriptors(cls)
1343-
13441339
return newcls
13451340

13461341

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
When :mod:`dataclasses` replaces a class with a slotted dataclass, the
2-
original class is now garbage collected again. Earlier changes in Python
3-
3.14 caused this class to remain in existence together with the replacement
2+
original class can now be garbage collected again. Earlier changes in Python
3+
3.14 caused this class to always remain in existence together with the replacement
44
class synthesized by :mod:`dataclasses`.

0 commit comments

Comments
 (0)