Skip to content

[3.13] gh-116789: Add more tests for inspect.getmembers (GH-116802) #123129

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 1 commit into from
Aug 26, 2024
Merged
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
50 changes: 50 additions & 0 deletions Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,56 @@ def f(self):
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):
Expand Down
Loading