utils module

The utils module contains utility functions and classes to assist in porchlight’s internal functioning.

Inspection utilities

Tools for introspection of functions extending what inspect can do.

porchlight.utils.inspect_functions.get_all_source(function: Callable) Tuple[List[str], int]

Retrieves all source code related to a given function, even if it has been otherwise wrapped.

It returns a tuple containing a list of strings containing the source code and an integer (starting line number). This is output by the eventual call to inspect.getsourcelines on the wrapped function.

Parameters:

function (Callable) – A defined function to get the source code for.

porchlight.utils.inspect_functions.get_wrapped_function(function: Callable) Callable

If the input callable has the __closure__ attr and its first cell is a function, it will descend until it has found a callable object with no __closure__ variable.

Typing utilities

porchlight.utils.typing_functions.decompose_type(typevar: Type, break_types: List[Type] = [], include_base_types: bool = True) List[Type]

Decomposes a single type that takes arguments and returns a list containing all types referenced within, recursively.

Parameters:
  • typevar (~typing.Type) – type annotation to decompose.

  • break_types (~typing.List[~typing.Type], optional) – If a list of types is provided, instances of this particular type will always halt the decomposition process. So, for example, if break_types = [Callable], and typevar = Callable[str, float], then all_types (return value) = [Callable[str, float]]

  • include_base_types (bool, optional) – If False (default True), base types containing other relevant types are ignored. These are only for resolvable types at return, such as Tuples, Lists, and Iterables. Callables are excluded by default.