4

The inspect.getmembers function breaks when used with a class that defines the __getattr__ method (in Python 3.x):

import inspect

class C:
    def __getattr__(self, name):
        def wrapper():
            print("For MCVE purposes, this does nothing useful.")
        return wrapper

print(inspect.getmembers(C()))

The resulting error looks like this:

Traceback (most recent call last):
  File "tmp.py", line 9, in <module>
    print(inspect.getmembers(C()))
  File "/usr/lib/python3.7/inspect.py", line 330, in getmembers
    for base in object.__bases__:
TypeError: 'function' object is not iterable

It seems that the problem lies in the fact that inspect.getmodule relies on the easier to ask for forgiveness approach and assumes that the __bases__ attribute either does not exist (which throws the AttributeError exception) or contains the base classes.

I could fix this by explicitly throwing AttributeError in C.__getattr__; however, I want to use inspect.getmembers with objects of classes that I have no control over. My question is this: Is there a workaround for this problem that works without any modification of the class C or its instances?

Nikola Benes
  • 2,372
  • 1
  • 20
  • 33
  • The only solution I can think of would be to monkeypatch such classes when you find one... But most `__getattr__` implementations I've seen so far restrict the available names (usually delegating to another object) instead of unconditionnally return something whatever the name. – bruno desthuilliers Feb 01 '19 at 11:53

2 Answers2

1

You asked for a workaround that works without modification of the class C or its instances. Here is a solution that does the job, although it involves rewriting the long getmembers() function, which may not be convenient either. Perhaps this is more appropriate for a Pull Request on the library.

(source code from https://github.com/python/cpython/blob/3.7/Lib/inspect.py)

import inspect

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."""
    # Line below adds inspect. reference to isclass()
    if inspect.isclass(object):
        # Line below adds inspect. reference to getmro()
        mro = (object,) + inspect.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:
        for base in object.__bases__:
            for k, v in base.__dict__.items():
                if isinstance(v, types.DynamicClassAttribute):
                    names.append(k)
    # Line below edited to catch TypeError
    except (AttributeError, TypeError):
        pass
    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
        # looking in the __dict__.
        try:
            value = getattr(object, key)
            # handle the duplicate key
            if key in processed:
                raise AttributeError
        except AttributeError:
            for base in mro:
                if key in base.__dict__:
                    value = base.__dict__[key]
                    break
            else:
                # could be a (currently) missing slot member, or a buggy
                # __dir__; discard and move on
                continue
        if not predicate or predicate(value):
            results.append((key, value))
        processed.add(key)
    results.sort(key=lambda pair: pair[0])
    return results

Now if you run your new getmembers():

class C:
    def __getattr__(self, name):
        def wrapper():
            print("For MCVE purposes, this does nothing useful.")
        return wrapper

print(getmembers(C()))

You should get the result you're after:

[('__class__', <class '__main__.C'>), ('__delattr__', <method-wrapper '__delattr__' of C object at 0x1155fa7b8>), ('__dict__', {}), ('__dir__', <built-in method __dir__ of C object at 0x1155fa7b8>), ('__doc__', None), ('__eq__', <method-wrapper '__eq__' of C object at 0x1155fa7b8>), ('__format__', <built-in method __format__ of C object at 0x1155fa7b8>), ('__ge__', <method-wrapper '__ge__' of C object at 0x1155fa7b8>), ('__getattr__', <bound method C.__getattr__ of <__main__.C object at 0x1155fa7b8>>), ('__getattribute__', <method-wrapper '__getattribute__' of C object at 0x1155fa7b8>), ('__gt__', <method-wrapper '__gt__' of C object at 0x1155fa7b8>), ('__hash__', <method-wrapper '__hash__' of C object at 0x1155fa7b8>), ('__init__', <method-wrapper '__init__' of C object at 0x1155fa7b8>), ('__init_subclass__', <built-in method __init_subclass__ of type object at 0x7fd01899e4c8>), ('__le__', <method-wrapper '__le__' of C object at 0x1155fa7b8>), ('__lt__', <method-wrapper '__lt__' of C object at 0x1155fa7b8>), ('__module__', '__main__'), ('__ne__', <method-wrapper '__ne__' of C object at 0x1155fa7b8>), ('__new__', <built-in method __new__ of type object at 0x1008b0c48>), ('__reduce__', <built-in method __reduce__ of C object at 0x1155fa7b8>), ('__reduce_ex__', <built-in method __reduce_ex__ of C object at 0x1155fa7b8>), ('__repr__', <method-wrapper '__repr__' of C object at 0x1155fa7b8>), ('__setattr__', <method-wrapper '__setattr__' of C object at 0x1155fa7b8>), ('__sizeof__', <built-in method __sizeof__ of C object at 0x1155fa7b8>), ('__str__', <method-wrapper '__str__' of C object at 0x1155fa7b8>), ('__subclasshook__', <built-in method __subclasshook__ of type object at 0x7fd01899e4c8>), ('__weakref__', None)]
ajrwhite
  • 7,728
  • 1
  • 11
  • 24
-1

Try setting c.__bases__ = () before calling inspect.getmembers(c).

Edit: safer and more elaborate version in response to comment:

object.__setattr__(c, '__bases__', ())
try:
    members = inspect.getmembers(c)
finally:
    object.__delattr__(c, '__bases__')

This will fail if:

  1. C defines __slots__ or doesn't allow setting __bases__ for some other reason, which will raise an exception.
  2. c already has an actual __bases__ attribute that shouldn't be overwritten. This can be checked for with some extra code.
Alex Hall
  • 34,833
  • 5
  • 57
  • 89