Skip to content

Reflection Inspection

Introspection helpers for objects and modules.

Inspection Module

Object inspection utilities.

This submodule provides utilities for inspecting objects, classes, and modules at runtime.

Examples

from rite.reflection.inspection import ( ... inspection_get_members, ... inspection_get_methods ... ) import json members = inspection_get_members(json) len(members) > 0 True

Modules

inspection_get_functions

Functions Inspector

Get all functions from a module.

Examples

from rite.reflection.inspection import inspection_get_functions import json funcs = inspection_get_functions(json) 'dumps' in [name for name, _ in funcs] True

Functions

inspection_get_functions(obj: Any) -> list[tuple[str, Any]]

Get all functions from a module or class.

Parameters:

Name Type Description Default
obj Any

Module or class to inspect.

required

Returns:

Type Description
list[tuple[str, Any]]

List of (function_name, function) tuples.

Examples:

>>> import json
>>> funcs = inspection_get_functions(json)
>>> 'dumps' in [name for name, _ in funcs]
True
Notes

Uses inspect.getmembers with isfunction filter.

inspection_get_members

Members Inspector

Get all members of an object.

Examples

from rite.reflection.inspection import inspection_get_members import json members = inspection_get_members(json) 'dumps' in dict(members) True

Functions

inspection_get_members(obj: Any, predicate: Any = None) -> list[tuple[str, Any]]

Get all members of an object.

Parameters:

Name Type Description Default
obj Any

Object to inspect.

required
predicate Any

Optional filter function.

None

Returns:

Type Description
list[tuple[str, Any]]

List of (name, value) tuples.

Examples:

>>> import json
>>> members = inspection_get_members(json)
>>> len(members) > 0
True
>>> inspection_get_members(json, inspect.isfunction)
[('dump', ...), ('dumps', ...), ...]
Notes

Uses inspect.getmembers. Predicate can filter by type.

inspection_get_methods

Methods Inspector

Get all methods of a class or object.

Examples

from rite.reflection.inspection import inspection_get_methods methods = inspection_get_methods(str) 'upper' in [name for name, _ in methods] True

Functions

inspection_get_methods(obj: Any) -> list[tuple[str, Any]]

Get all methods of a class or object.

Parameters:

Name Type Description Default
obj Any

Class or object to inspect.

required

Returns:

Type Description
list[tuple[str, Any]]

List of (method_name, method) tuples.

Examples:

>>> methods = inspection_get_methods(str)
>>> 'upper' in [name for name, _ in methods]
True
>>> methods = inspection_get_methods([])
>>> 'append' in [name for name, _ in methods]
True
Notes

Uses inspect.getmembers with ismethod filter. Includes both instance and class methods.

inspection_get_source

Source Inspector

Get source code of an object.

Examples

from rite.reflection.inspection import inspection_get_source def my_func(): ... return 42 source = inspection_get_source(my_func) 'return 42' in source True

Functions

inspection_get_source(obj: Any) -> str

Get source code of an object.

Parameters:

Name Type Description Default
obj Any

Function, method, or class to inspect.

required

Returns:

Type Description
str

Source code as string.

Raises:

Type Description
OSError

If source cannot be retrieved.

Examples:

>>> def my_func():
...     return 42
>>> source = inspection_get_source(my_func)
>>> 'return 42' in source
True
Notes

Uses inspect.getsource. May fail for built-in functions.

options: show_root_heading: true show_source: false heading_level: 2