8000 gh-102069: Fix `__weakref__` descriptor generation for custom dataclasses by sobolevn · Pull Request #102075 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-102069: Fix __weakref__ descriptor generation for custom dataclasses #102075

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 2 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,9 @@ def _add_slots(cls, is_frozen, weakref_slot):
# Remove __dict__ itself.
cls_dict.pop('__dict__', None)

# Clear existing `__weakref__` descriptor, it belongs to a previous type:
cls_dict.pop('__weakref__', None) # gh-102069

# And finally create the class.
qualname = getattr(cls, '__qualname__', None)
cls = type(cls)(cls.__name__, cls.__bases__, cls_dict)
Expand Down
15 changes: 11 additions & 4 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3119,6 +3119,8 @@ class A:
with self.assertRaisesRegex(TypeError,
"cannot create weak reference"):
weakref.ref(a)
with self.assertRaises(AttributeError):
a.__weakref__

def test_slots_weakref(self):
@dataclass(slots=True, weakref_slot=True)
Expand All @@ -3127,7 +3129,9 @@ class A:

self.assertIn("__weakref__", A.__slots__)
a = A(1)
weakref.ref(a)
a_ref = weakref.ref(a)

self.assertIs(a.__weakref__, a_ref)

def test_slots_weakref_base_str(self):
class Base:
Expand Down Expand Up @@ -3193,7 +3197,8 @@ class A(Base):
self.assertIn("__weakref__", Base.__slots__)
self.assertNotIn("__weakref__", A.__slots__)
a = A(1)
weakref.ref(a)
a_ref = weakref.ref(a)
self.assertIs(a.__weakref__, a_ref)

def test_weakref_slot_subclass_no_weakref_slot(self):
@dataclass(slots=True, weakref_slot=True)
Expand All @@ -3209,7 +3214,8 @@ class A(Base):
self.assertIn("__weakref__", Base.__slots__)
self.assertNotIn("__weakref__", A.__slots__)
a = A(1)
weakref.ref(a)
a_ref = weakref.ref(a)
self.assertIs(a.__weakref__, a_ref)

def test_weakref_slot_normal_base_weakref_slot(self):
class Base:
Expand All @@ -3224,7 +3230,8 @@ class A(Base):
self.assertIn("__weakref__", Base.__slots__)
self.assertNotIn("__weakref__", A.__slots__)
a = A(1)
weakref.ref(a)
a_ref = weakref.ref(a)
self.assertIs(a.__weakref__, a_ref)


class TestDescriptors(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``__weakref__`` descriptor generation for custom dataclasses.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
0