Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
Prev Previous commit
Next Next commit
Add tests for the generic descriptors
  • Loading branch information
encukou committed Aug 12, 2025
commit 64ebb4fff632853b330c496855b46134ce45d3a5
64 changes: 64 additions & 0 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -6013,5 +6013,69 @@ class A(metaclass=M):
pass


class TestGenericDescriptors(unittest.TestCase):
def test___dict__(self):
class CustomClass:
pass
class SlotClass:
__slots__ = ['foo']
class SlotSubClass(SlotClass):
pass
class IntSubclass(int):
pass

dict_descriptor = CustomClass.__dict__['__dict__']
self.assertEqual(dict_descriptor.__objclass__, object)

for cls in CustomClass, SlotSubClass, IntSubclass:
with self.subTest(cls=cls):
self.assertIs(cls.__dict__['__dict__'], dict_descriptor)
instance = cls()
instance.attr = 123
self.assertEqual(
dict_descriptor.__get__(instance, cls),
{'attr': 123},
)
with self.assertRaises(AttributeError):
print(dict_descriptor.__get__(True, bool))
with self.assertRaises(AttributeError):
print(dict_descriptor.__get__(SlotClass(), SlotClass))

# delegation to type.__dict__
self.assertIsInstance(
dict_descriptor.__get__(type, type),
types.MappingProxyType,
)

def test___weakref__(self):
class CustomClass:
pass
class SlotClass:
__slots__ = ['foo']
class SlotSubClass(SlotClass):
pass
class IntSubclass(int):
pass

weakref_descriptor = CustomClass.__dict__['__weakref__']
self.assertEqual(weakref_descriptor.__objclass__, object)

for cls in CustomClass, SlotSubClass:
with self.subTest(cls=cls):
self.assertIs(cls.__dict__['__weakref__'], weakref_descriptor)
instance = cls()
instance.attr = 123
self.assertEqual(
weakref_descriptor.__get__(instance, cls),
None,
)
with self.assertRaises(AttributeError):
print(weakref_descriptor.__get__(True, bool))
with self.assertRaises(AttributeError):
print(weakref_descriptor.__get__(SlotClass(), SlotClass))
with self.assertRaises(AttributeError):
print(weakref_descriptor.__get__(IntSubclass(), IntSubclass))
Comment thread
encukou marked this conversation as resolved.
Outdated


if __name__ == "__main__":
unittest.main()