Skip to content

Commit 470e2eb

Browse files
committed
Set __attrs_base_of_slotted__ for unused base classes
1 parent ff4ba8e commit 470e2eb

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/attr/_make.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import sys
1313
import types
1414
import unicodedata
15+
import weakref
1516

1617
from collections.abc import Callable, Mapping
1718
from functools import cached_property
@@ -836,6 +837,7 @@ def build_class(self):
836837
self._eval_snippets()
837838
if self._slots is True:
838839
cls = self._create_slots_class()
840+
self._cls.__attrs_base_of_slotted__ = weakref.ref(cls)
839841
else:
840842
cls = self._patch_original_class()
841843
if PY_3_10_PLUS:

tests/test_slots.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ def test_slots_being_used():
104104
assert attr.asdict(slot_instance) == attr.asdict(non_slot_instance)
105105

106106

107+
def test_slots_base_of_slotted():
108+
"""
109+
The (hopefully gc'ed) temporary base class of a slotted class contains a
110+
reference to the slotted class.
111+
"""
112+
113+
class Base:
114+
pass
115+
116+
Slotted = attr.s(slots=True)(Base)
117+
118+
assert Slotted is Base.__attrs_base_of_slotted__()
119+
120+
107121
def test_basic_attr_funcs():
108122
"""
109123
Comparison, `__eq__`, `__hash__`, `__repr__`, `attrs.asdict` work.
@@ -755,7 +769,7 @@ def f(self):
755769
assert "__dict__" not in dir(A)
756770

757771

758-
def test_slots_cached_property_works_on_frozen_isntances():
772+
def test_slots_cached_property_works_on_frozen_instances():
759773
"""
760774
Infers type of cached property.
761775
"""

0 commit comments

Comments
 (0)