xoutil.future.inspect - Inspect live objects

This module extends the standard library’s functools. You may use it as a drop-in replacement in many cases.

Avoid importing * from this module since could be different in Python 2.7 and Python 3.3.

We added the following features.

xoutil.future.inspect.get_attr_value(obj, name, *default)[source]

Get a named attribute from an object in a safe way.

Similar to getattr but without triggering dynamic look-up via the descriptor protocol, __getattr__ or __getattribute__ by using getattr_static().

xoutil.future.inspect.type_name(obj, affirm=False)

Return the internal name for a type or a callable.

This function is safe. If :param obj: is not an instance of a proper type then returns the following depending on :param affirm:

  • If False returns None.
  • If True convert a single object to its type before returns the name, but if is a tuple, list or set; returns a string with a representation of contained types.

Examples:

>>> safe_name(int)
'int'

>>> safe_name(0) is None
True

>>> safe_name(0, affirm=True)
'int'

>>> safe_name((0, 1.1)) is None
True

>>> safe_name((0, 1.1), affirm=True)
'(int, float)'

We have backported several Python 3.3 features but maybe not all (some protected structures are not presented in this documentation).

xoutil.future.inspect.getfullargspec(func)[source]

Get the names and default values of a callable object’s arguments.

A tuple of seven things is returned: (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults annotations). ‘args’ is a list of the argument names. ‘varargs’ and ‘varkw’ are the names of the * and ** arguments or None. ‘defaults’ is an n-tuple of the default values of the last n arguments. ‘kwonlyargs’ is a list of keyword-only argument names. ‘kwonlydefaults’ is a dictionary mapping names from kwonlyargs to defaults. ‘annotations’ is a dictionary mapping argument names to annotations.

The first four items in the tuple correspond to getargspec().

This function is deprecated, use inspect.signature() instead.

xoutil.future.inspect.getattr_static(obj, attr, default=<object object>)[source]

Retrieve attributes without triggering dynamic lookup via the descriptor protocol, __getattr__ or __getattribute__.

Note: this function may not be able to retrieve all attributes that getattr can fetch (like dynamically created attributes) and may find attributes that getattr can’t (like descriptors that raise AttributeError). It can also return descriptor objects instead of instance members in some cases. See the documentation for details.