Skip to content
Closed
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
22 changes: 10 additions & 12 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,23 +319,21 @@ def isabstract(object):
def getmembers(object, predicate=None):
"""Return all members of an object as (name, value) pairs sorted by name.
Optionally, only return members that satisfy a given predicate."""
if isclass(object):
mro = (object,) + getmro(object)
else:
mro = ()
results = []
processed = set()
names = dir(object)
# :dd any DynamicClassAttributes to the list of names if object is a class;
# this may result in duplicate entries if, for example, a virtual
# attribute with the same name as a DynamicClassAttribute exists
try:
if isclass(object):
mro = getmro(object)
# Add any DynamicClassAttributes to the list of names
# if object is a class;
# this may result in duplicate entries if, for example, a virtual
# attribute with the same name as a DynamicClassAttribute exists
for base in object.__bases__:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not it iterate mro instead of object.__bases__? Or _static_getmro(object)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, it should apparently use one of those.

for k, v in base.__dict__.items():
if isinstance(v, types.DynamicClassAttribute):
names.append(k)
except AttributeError:
pass
else:
mro = ()
results = []
processed = set()
for key in names:
# First try to get the value via getattr. Some descriptors don't
# like calling their __get__ (see bug #1785), so fall back to
Expand Down
30 changes: 30 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,36 @@ class C(metaclass=M):
attrs = [a[0] for a in inspect.getmembers(C)]
self.assertNotIn('missing', attrs)

def test_getmembers_with_non_class(self):
name = '__inspect_dummy'
m = types.ModuleType(name)
m.__bases__ = None
attrs = [a[0] for a in inspect.getmembers(m)]
self.assertIn('__bases__', attrs)

def test_getmembers_with_custom_getattr(self):
class CustomGetattr(object):
Comment thread
serhiy-storchaka marked this conversation as resolved.
def __getattr__(self, attr):
return None

CustomGetattr.__test__ = None
attrs = [a[0] for a in inspect.getmembers(CustomGetattr())]
self.assertIn('__test__', attrs)

def test_getmembers_with_dynamic_class_attribute(self):
class A:
@types.DynamicClassAttribute
def ham(self):
return 'eggs'
class B(A): pass
class C(B): pass

attrs = [b[0] for b in inspect.getmembers(B)]
self.assertIn('ham', attrs)
attrs = [c[0] for c in inspect.getmembers(C)]
self.assertIn('ham', attrs)


class TestIsDataDescriptor(unittest.TestCase):

def test_custom_descriptors(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix :func:`inspect.getmembers` to traversing mro when the object is a class only.