0
0
mirror of https://github.com/python/cpython.git synced 2024-11-24 00:38:00 +01:00

gh-116789: Add more tests for inspect.getmembers (#116802)

This commit is contained in:
sobolevn 2024-08-18 18:42:56 +03:00 committed by GitHub
parent b0f462d4c8
commit c15bfa9a71
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1550,6 +1550,56 @@ class TestClassesAndFunctions(unittest.TestCase):
self.assertIn(('f', b.f), inspect.getmembers(b))
self.assertIn(('f', b.f), inspect.getmembers(b, inspect.ismethod))
def test_getmembers_custom_dir(self):
class CorrectDir:
def __init__(self, attr):
self.attr = attr
def method(self):
return self.attr + 1
def __dir__(self):
return ['attr', 'method']
cd = CorrectDir(5)
self.assertEqual(inspect.getmembers(cd), [
('attr', 5),
('method', cd.method),
])
self.assertEqual(inspect.getmembers(cd, inspect.ismethod), [
('method', cd.method),
])
def test_getmembers_custom_broken_dir(self):
# inspect.getmembers calls `dir()` on the passed object inside.
# if `__dir__` mentions some non-existent attribute,
# we still need to return others correctly.
class BrokenDir:
existing = 1
def method(self):
return self.existing + 1
def __dir__(self):
return ['method', 'missing', 'existing']
bd = BrokenDir()
self.assertEqual(inspect.getmembers(bd), [
('existing', 1),
('method', bd.method),
])
self.assertEqual(inspect.getmembers(bd, inspect.ismethod), [
('method', bd.method),
])
def test_getmembers_custom_duplicated_dir(self):
# Duplicates in `__dir__` must not fail and return just one result.
class DuplicatedDir:
attr = 1
def __dir__(self):
return ['attr', 'attr']
dd = DuplicatedDir()
self.assertEqual(inspect.getmembers(dd), [
('attr', 1),
])
def test_getmembers_VirtualAttribute(self):
class M(type):
def __getattr__(cls, name):