Reflection Module¶
The rite.reflection module provides runtime introspection, dynamic loading, and reflection utilities.
Overview¶
reflection ¶
Reflection Module¶
Comprehensive runtime introspection utilities.
This module provides utilities organized into semantic submodules: - importing: Dynamic imports (load_class, load_module, load_function) - inspection: Object inspection (get_members, get_methods, get_source) - attributes: Attribute manipulation (has_attr, get_attr, set_attr) - types: Type checking (is_class, is_function, is_method) - signature: Signature inspection (get_signature, get_parameters) - documentation: Documentation access (get_docstring, get_file)
Examples¶
from rite.reflection import ( ... importing_load_class, ... inspection_get_members ... ) OrderedDict = importing_load_class("collections.OrderedDict") import json members = inspection_get_members(json) len(members) > 0 True
Legacy Functions¶
from rite.reflection import load_class MyClass = load_class("collections.OrderedDict")
Classes¶
ClassImportError ¶
Bases: ImportError
Raised when class cannot be dynamically imported.
Modules¶
attributes ¶
Attributes Module¶
Attribute manipulation utilities.
This submodule provides utilities for working with object attributes dynamically.
Examples¶
from rite.reflection.attributes import ( ... attributes_has_attr, ... attributes_get_attr ... ) attributes_has_attr("hello", "upper") True attributes_get_attr("hello", "upper")
Modules¶
attributes_del_attr ¶
Delete Attribute¶
Delete attribute from object.
Examples¶
from rite.reflection.attributes import attributes_del_attr class MyClass: ... value = 42 obj = MyClass() attributes_del_attr(obj, "value")
Delete attribute from object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to delete attribute from. |
required |
name
|
str
|
Attribute name. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If attribute doesn't exist. |
Examples:
>>> class MyClass:
... value = 42
>>> obj = MyClass()
>>> attributes_del_attr(obj, "value")
>>> hasattr(obj, "value")
False
Notes
Wrapper around delattr().
attributes_get_attr ¶
Get Attribute¶
Get attribute value from object.
Examples¶
from rite.reflection.attributes import attributes_get_attr attributes_get_attr("hello", "upper")
Get attribute value from object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to get attribute from. |
required |
name
|
str
|
Attribute name. |
required |
default
|
Any
|
Default value if not found. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Attribute value or default. |
Examples:
>>> attributes_get_attr("hello", "upper")
<built-in method upper of str object at 0x...>
>>> attributes_get_attr("hello", "missing", "default")
'default'
Notes
Wrapper around getattr(). Returns default if attribute not found.
attributes_has_attr ¶
Has Attribute Checker¶
Check if object has an attribute.
Examples¶
from rite.reflection.attributes import attributes_has_attr attributes_has_attr("hello", "upper") True
Check if object has an attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to check. |
required |
name
|
str
|
Attribute name. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if attribute exists. |
Examples:
>>> attributes_has_attr("hello", "upper")
True
>>> attributes_has_attr("hello", "nonexistent")
False
>>> attributes_has_attr([], "append")
True
Notes
Wrapper around hasattr().
attributes_set_attr ¶
Set Attribute¶
Set attribute value on object.
Examples¶
from rite.reflection.attributes import attributes_set_attr class MyClass: ... pass obj = MyClass() attributes_set_attr(obj, "value", 42) obj.value 42
Set attribute value on object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to set attribute on. |
required |
name
|
str
|
Attribute name. |
required |
value
|
Any
|
Value to set. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Examples:
>>> class MyClass:
... pass
>>> obj = MyClass()
>>> attributes_set_attr(obj, "value", 42)
>>> obj.value
42
Notes
Wrapper around setattr(). Creates attribute if doesn't exist.
attributes_del_attr ¶
Delete Attribute¶
Delete attribute from object.
Examples¶
from rite.reflection.attributes import attributes_del_attr class MyClass: ... value = 42 obj = MyClass() attributes_del_attr(obj, "value")
Functions¶
attributes_del_attr ¶
Delete attribute from object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to delete attribute from. |
required |
name
|
str
|
Attribute name. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If attribute doesn't exist. |
Examples:
>>> class MyClass:
... value = 42
>>> obj = MyClass()
>>> attributes_del_attr(obj, "value")
>>> hasattr(obj, "value")
False
Notes
Wrapper around delattr().
attributes_get_attr ¶
Get Attribute¶
Get attribute value from object.
Examples¶
from rite.reflection.attributes import attributes_get_attr attributes_get_attr("hello", "upper")
Functions¶
attributes_get_attr ¶
Get attribute value from object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to get attribute from. |
required |
name
|
str
|
Attribute name. |
required |
default
|
Any
|
Default value if not found. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Attribute value or default. |
Examples:
>>> attributes_get_attr("hello", "upper")
<built-in method upper of str object at 0x...>
>>> attributes_get_attr("hello", "missing", "default")
'default'
Notes
Wrapper around getattr(). Returns default if attribute not found.
attributes_has_attr ¶
Has Attribute Checker¶
Check if object has an attribute.
Examples¶
from rite.reflection.attributes import attributes_has_attr attributes_has_attr("hello", "upper") True
Functions¶
attributes_has_attr ¶
Check if object has an attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to check. |
required |
name
|
str
|
Attribute name. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if attribute exists. |
Examples:
>>> attributes_has_attr("hello", "upper")
True
>>> attributes_has_attr("hello", "nonexistent")
False
>>> attributes_has_attr([], "append")
True
Notes
Wrapper around hasattr().
attributes_set_attr ¶
Set Attribute¶
Set attribute value on object.
Examples¶
from rite.reflection.attributes import attributes_set_attr class MyClass: ... pass obj = MyClass() attributes_set_attr(obj, "value", 42) obj.value 42
Functions¶
attributes_set_attr ¶
Set attribute value on object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to set attribute on. |
required |
name
|
str
|
Attribute name. |
required |
value
|
Any
|
Value to set. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Examples:
>>> class MyClass:
... pass
>>> obj = MyClass()
>>> attributes_set_attr(obj, "value", 42)
>>> obj.value
42
Notes
Wrapper around setattr(). Creates attribute if doesn't exist.
documentation ¶
Documentation Module¶
Documentation inspection utilities.
This submodule provides utilities for inspecting docstrings, comments, and source file locations.
Examples¶
from rite.reflection.documentation import ( ... documentation_get_docstring ... ) def func(): ... '''This is a docstring.''' ... pass documentation_get_docstring(func) 'This is a docstring.'
Modules¶
documentation_get_comments ¶
Comments Inspector¶
Get comments for an object.
Examples¶
from rite.reflection.documentation import ( ... documentation_get_comments ... ) def func(): ... pass comments = documentation_get_comments(func)
documentation_get_docstring ¶
Docstring Inspector¶
Get docstring of an object.
Examples¶
from rite.reflection.documentation import ( ... documentation_get_docstring ... ) def func(): ... '''This is a docstring.''' ... pass documentation_get_docstring(func) 'This is a docstring.'
Get docstring of an object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to get docstring from. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Docstring or None if not found. |
Examples:
>>> def func():
... '''This is a docstring.'''
... pass
>>> documentation_get_docstring(func)
'This is a docstring.'
Notes
Uses inspect.getdoc. Cleans up indentation.
documentation_get_file ¶
File Inspector¶
Get file path of an object.
Examples¶
from rite.reflection.documentation import ( ... documentation_get_file ... ) import json path = documentation_get_file(json) path.endswith('json/init.py') True
documentation_get_comments ¶
Comments Inspector¶
Get comments for an object.
Examples¶
from rite.reflection.documentation import ( ... documentation_get_comments ... ) def func(): ... pass comments = documentation_get_comments(func)
documentation_get_docstring ¶
Docstring Inspector¶
Get docstring of an object.
Examples¶
from rite.reflection.documentation import ( ... documentation_get_docstring ... ) def func(): ... '''This is a docstring.''' ... pass documentation_get_docstring(func) 'This is a docstring.'
Functions¶
documentation_get_docstring ¶
Get docstring of an object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to get docstring from. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Docstring or None if not found. |
Examples:
>>> def func():
... '''This is a docstring.'''
... pass
>>> documentation_get_docstring(func)
'This is a docstring.'
Notes
Uses inspect.getdoc. Cleans up indentation.
documentation_get_file ¶
File Inspector¶
Get file path of an object.
Examples¶
from rite.reflection.documentation import ( ... documentation_get_file ... ) import json path = documentation_get_file(json) path.endswith('json/init.py') True
importing ¶
Importing Module¶
Dynamic import utilities.
This submodule provides utilities for dynamically importing classes, functions, and modules at runtime.
Examples¶
from rite.reflection.importing import ( ... importing_load_class, ... importing_load_module ... ) importing_load_class("collections.OrderedDict")
Classes¶
ClassImportError ¶
Bases: ImportError
Raised when class cannot be dynamically imported.
Modules¶
importing_load_class ¶
Class Importer¶
Dynamically load class from module path.
Examples¶
from rite.reflection.importing import importing_load_class OrderedDict = importing_load_class("collections.OrderedDict") isinstance(OrderedDict(), dict) True
Bases: ImportError
Raised when class cannot be dynamically imported.
Dynamically load class from module path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Fully qualified path (e.g., "module.Class"). |
required |
Returns:
| Type | Description |
|---|---|
type
|
Loaded class object. |
Raises:
| Type | Description |
|---|---|
ClassImportError
|
If module or class cannot be imported. |
Examples:
>>> importing_load_class("collections.OrderedDict")
<class 'collections.OrderedDict'>
>>> importing_load_class("pathlib.Path")
<class 'pathlib.Path'>
Notes
Path format: "module.submodule.ClassName".
importing_load_function ¶
Function Importer¶
Dynamically load function from module path.
Examples¶
from rite.reflection.importing import importing_load_function dumps = importing_load_function("json.dumps") dumps({"key": "value"}) '{"key": "value"}'
Dynamically load function from module path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Fully qualified path (e.g., "module.function"). |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
Loaded function object. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If module cannot be imported. |
AttributeError
|
If function not found. |
Examples:
>>> importing_load_function("json.dumps")
<function dumps at 0x...>
>>> importing_load_function("os.path.join")
<function join at 0x...>
Notes
Path format: "module.submodule.function_name".
importing_load_module ¶
Module Importer¶
Dynamically import module by name.
Examples¶
from rite.reflection.importing import importing_load_module json_module = importing_load_module("json") json_module.dumps({"key": "value"}) '{"key": "value"}'
Dynamically import module by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Module name (e.g., "json", "os.path"). |
required |
Returns:
| Type | Description |
|---|---|
ModuleType
|
Imported module object. |
Raises:
| Type | Description |
|---|---|
ModuleNotFoundError
|
If module cannot be imported. |
Examples:
>>> importing_load_module("json")
<module 'json' from '...'>
>>> importing_load_module("os.path")
<module 'posixpath' from '...'>
Notes
Uses importlib.import_module.
importing_load_class ¶
Class Importer¶
Dynamically load class from module path.
Examples¶
from rite.reflection.importing import importing_load_class OrderedDict = importing_load_class("collections.OrderedDict") isinstance(OrderedDict(), dict) True
Classes¶
ClassImportError ¶
Bases: ImportError
Raised when class cannot be dynamically imported.
Functions¶
importing_load_class ¶
Dynamically load class from module path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Fully qualified path (e.g., "module.Class"). |
required |
Returns:
| Type | Description |
|---|---|
type
|
Loaded class object. |
Raises:
| Type | Description |
|---|---|
ClassImportError
|
If module or class cannot be imported. |
Examples:
>>> importing_load_class("collections.OrderedDict")
<class 'collections.OrderedDict'>
>>> importing_load_class("pathlib.Path")
<class 'pathlib.Path'>
Notes
Path format: "module.submodule.ClassName".
importing_load_function ¶
Function Importer¶
Dynamically load function from module path.
Examples¶
from rite.reflection.importing import importing_load_function dumps = importing_load_function("json.dumps") dumps({"key": "value"}) '{"key": "value"}'
Functions¶
importing_load_function ¶
Dynamically load function from module path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Fully qualified path (e.g., "module.function"). |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
Loaded function object. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If module cannot be imported. |
AttributeError
|
If function not found. |
Examples:
>>> importing_load_function("json.dumps")
<function dumps at 0x...>
>>> importing_load_function("os.path.join")
<function join at 0x...>
Notes
Path format: "module.submodule.function_name".
importing_load_module ¶
Module Importer¶
Dynamically import module by name.
Examples¶
from rite.reflection.importing import importing_load_module json_module = importing_load_module("json") json_module.dumps({"key": "value"}) '{"key": "value"}'
Functions¶
importing_load_module ¶
Dynamically import module by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Module name (e.g., "json", "os.path"). |
required |
Returns:
| Type | Description |
|---|---|
ModuleType
|
Imported module object. |
Raises:
| Type | Description |
|---|---|
ModuleNotFoundError
|
If module cannot be imported. |
Examples:
>>> importing_load_module("json")
<module 'json' from '...'>
>>> importing_load_module("os.path")
<module 'posixpath' from '...'>
Notes
Uses importlib.import_module.
inspection ¶
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
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
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
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
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.
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 ¶
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 ¶
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 ¶
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 ¶
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.
signature ¶
Signature Module¶
Function signature inspection utilities.
This submodule provides utilities for inspecting function signatures, parameters, and return annotations.
Examples¶
from rite.reflection.signature import ( ... signature_get_signature, ... signature_get_parameters ... ) def func(a: int, b: str = "default") -> str: ... return f"{a}{b}" sig = signature_get_signature(func) len(sig.parameters) 2
Modules¶
signature_get_parameters ¶
Parameters Inspector¶
Get function parameters.
Examples¶
from rite.reflection.signature import signature_get_parameters def func(a: int, b: str = "default") -> str: ... return f"{a}{b}" params = signature_get_parameters(func) list(params.keys()) ['a', 'b']
Get function parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Function or method to inspect. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Parameter]
|
Dictionary of parameter names to Parameter objects. |
Examples:
>>> def func(a: int, b: str = "default") -> str:
... return f"{a}{b}"
>>> params = signature_get_parameters(func)
>>> 'a' in params
True
>>> 'b' in params
True
Notes
Uses inspect.signature. Returns ordered dict of parameters.
signature_get_return_annotation ¶
Return Annotation Inspector¶
Get function return annotation.
Examples¶
from rite.reflection.signature import ( ... signature_get_return_annotation ... ) def func(a: int) -> str: ... return str(a) signature_get_return_annotation(func)
Get function return annotation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Function or method to inspect. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Return annotation or Signature.empty. |
Examples:
>>> def func(a: int) -> str:
... return str(a)
>>> signature_get_return_annotation(func)
<class 'str'>
Notes
Returns Signature.empty if no annotation.
signature_get_signature ¶
Signature Inspector¶
Get function or method signature.
Examples¶
from rite.reflection.signature import signature_get_signature def func(a: int, b: str = "default") -> str: ... return f"{a}{b}" sig = signature_get_signature(func) str(sig) "(a: int, b: str = 'default') -> str"
Get function or method signature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Function, method, or class to inspect. |
required |
Returns:
| Type | Description |
|---|---|
Signature
|
Signature object. |
Examples:
>>> def func(a: int, b: str = "default") -> str:
... return f"{a}{b}"
>>> sig = signature_get_signature(func)
>>> len(sig.parameters)
2
Notes
Uses inspect.signature. Returns Signature object.
signature_get_parameters ¶
Parameters Inspector¶
Get function parameters.
Examples¶
from rite.reflection.signature import signature_get_parameters def func(a: int, b: str = "default") -> str: ... return f"{a}{b}" params = signature_get_parameters(func) list(params.keys()) ['a', 'b']
Functions¶
signature_get_parameters ¶
Get function parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Function or method to inspect. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Parameter]
|
Dictionary of parameter names to Parameter objects. |
Examples:
>>> def func(a: int, b: str = "default") -> str:
... return f"{a}{b}"
>>> params = signature_get_parameters(func)
>>> 'a' in params
True
>>> 'b' in params
True
Notes
Uses inspect.signature. Returns ordered dict of parameters.
signature_get_return_annotation ¶
Return Annotation Inspector¶
Get function return annotation.
Examples¶
from rite.reflection.signature import ( ... signature_get_return_annotation ... ) def func(a: int) -> str: ... return str(a) signature_get_return_annotation(func)
Functions¶
signature_get_return_annotation ¶
Get function return annotation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Function or method to inspect. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Return annotation or Signature.empty. |
Examples:
>>> def func(a: int) -> str:
... return str(a)
>>> signature_get_return_annotation(func)
<class 'str'>
Notes
Returns Signature.empty if no annotation.
signature_get_signature ¶
Signature Inspector¶
Get function or method signature.
Examples¶
from rite.reflection.signature import signature_get_signature def func(a: int, b: str = "default") -> str: ... return f"{a}{b}" sig = signature_get_signature(func) str(sig) "(a: int, b: str = 'default') -> str"
Functions¶
signature_get_signature ¶
Get function or method signature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Function, method, or class to inspect. |
required |
Returns:
| Type | Description |
|---|---|
Signature
|
Signature object. |
Examples:
>>> def func(a: int, b: str = "default") -> str:
... return f"{a}{b}"
>>> sig = signature_get_signature(func)
>>> len(sig.parameters)
2
Notes
Uses inspect.signature. Returns Signature object.
types ¶
Types Module¶
Type checking utilities.
This submodule provides utilities for checking object types at runtime.
Examples¶
from rite.reflection.types import ( ... types_is_class, ... types_is_function ... ) types_is_class(str) True types_is_function(lambda x: x) True
Modules¶
types_is_class ¶
types_is_function ¶
Function Type Checker¶
Check if object is a function.
Examples¶
from rite.reflection.types import types_is_function def my_func(): ... pass types_is_function(my_func) True
Check if object is a function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if object is a function. |
Examples:
>>> def my_func():
... pass
>>> types_is_function(my_func)
True
>>> types_is_function(lambda x: x)
True
>>> types_is_function("not a function")
False
Notes
Uses inspect.isfunction. Does not match methods.
types_is_method ¶
Method Type Checker¶
Check if object is a method.
Examples¶
from rite.reflection.types import types_is_method types_is_method("hello".upper) True
Check if object is a method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if object is a method. |
Examples:
>>> types_is_method("hello".upper)
True
>>> types_is_method([].append)
True
>>> types_is_method(str.upper)
False
Notes
Uses inspect.ismethod. Only bound methods return True.
types_is_class ¶
types_is_function ¶
Function Type Checker¶
Check if object is a function.
Examples¶
from rite.reflection.types import types_is_function def my_func(): ... pass types_is_function(my_func) True
Functions¶
types_is_function ¶
Check if object is a function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if object is a function. |
Examples:
>>> def my_func():
... pass
>>> types_is_function(my_func)
True
>>> types_is_function(lambda x: x)
True
>>> types_is_function("not a function")
False
Notes
Uses inspect.isfunction. Does not match methods.
types_is_method ¶
Method Type Checker¶
Check if object is a method.
Examples¶
from rite.reflection.types import types_is_method types_is_method("hello".upper) True
Functions¶
types_is_method ¶
Check if object is a method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if object is a method. |
Examples:
>>> types_is_method("hello".upper)
True
>>> types_is_method([].append)
True
>>> types_is_method(str.upper)
False
Notes
Uses inspect.ismethod. Only bound methods return True.
Submodules¶
Importing¶
Dynamic module and class loading.
Importing Module¶
Dynamic import utilities.
This submodule provides utilities for dynamically importing classes, functions, and modules at runtime.
Examples¶
from rite.reflection.importing import ( ... importing_load_class, ... importing_load_module ... ) importing_load_class("collections.OrderedDict")
Modules¶
importing_load_module ¶
Module Importer¶
Dynamically import module by name.
Examples¶
from rite.reflection.importing import importing_load_module json_module = importing_load_module("json") json_module.dumps({"key": "value"}) '{"key": "value"}'
Functions¶
importing_load_module ¶
Dynamically import module by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Module name (e.g., "json", "os.path"). |
required |
Returns:
| Type | Description |
|---|---|
ModuleType
|
Imported module object. |
Raises:
| Type | Description |
|---|---|
ModuleNotFoundError
|
If module cannot be imported. |
Examples:
>>> importing_load_module("json")
<module 'json' from '...'>
>>> importing_load_module("os.path")
<module 'posixpath' from '...'>
Notes
Uses importlib.import_module.
importing_load_class ¶
Class Importer¶
Dynamically load class from module path.
Examples¶
from rite.reflection.importing import importing_load_class OrderedDict = importing_load_class("collections.OrderedDict") isinstance(OrderedDict(), dict) True
Classes¶
ClassImportError ¶
Bases: ImportError
Raised when class cannot be dynamically imported.
Functions¶
importing_load_class ¶
Dynamically load class from module path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Fully qualified path (e.g., "module.Class"). |
required |
Returns:
| Type | Description |
|---|---|
type
|
Loaded class object. |
Raises:
| Type | Description |
|---|---|
ClassImportError
|
If module or class cannot be imported. |
Examples:
>>> importing_load_class("collections.OrderedDict")
<class 'collections.OrderedDict'>
>>> importing_load_class("pathlib.Path")
<class 'pathlib.Path'>
Notes
Path format: "module.submodule.ClassName".
importing_load_function ¶
Function Importer¶
Dynamically load function from module path.
Examples¶
from rite.reflection.importing import importing_load_function dumps = importing_load_function("json.dumps") dumps({"key": "value"}) '{"key": "value"}'
Functions¶
importing_load_function ¶
Dynamically load function from module path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Fully qualified path (e.g., "module.function"). |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
Loaded function object. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If module cannot be imported. |
AttributeError
|
If function not found. |
Examples:
>>> importing_load_function("json.dumps")
<function dumps at 0x...>
>>> importing_load_function("os.path.join")
<function join at 0x...>
Notes
Path format: "module.submodule.function_name".
Inspection¶
Inspect Python objects at runtime.
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_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 ¶
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 ¶
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_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 ¶
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_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 ¶
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.
Attributes¶
Attribute access and manipulation.
Attributes Module¶
Attribute manipulation utilities.
This submodule provides utilities for working with object attributes dynamically.
Examples¶
from rite.reflection.attributes import ( ... attributes_has_attr, ... attributes_get_attr ... ) attributes_has_attr("hello", "upper") True attributes_get_attr("hello", "upper")
Modules¶
attributes_get_attr ¶
Get Attribute¶
Get attribute value from object.
Examples¶
from rite.reflection.attributes import attributes_get_attr attributes_get_attr("hello", "upper")
Functions¶
attributes_get_attr ¶
Get attribute value from object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to get attribute from. |
required |
name
|
str
|
Attribute name. |
required |
default
|
Any
|
Default value if not found. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Attribute value or default. |
Examples:
>>> attributes_get_attr("hello", "upper")
<built-in method upper of str object at 0x...>
>>> attributes_get_attr("hello", "missing", "default")
'default'
Notes
Wrapper around getattr(). Returns default if attribute not found.
attributes_set_attr ¶
Set Attribute¶
Set attribute value on object.
Examples¶
from rite.reflection.attributes import attributes_set_attr class MyClass: ... pass obj = MyClass() attributes_set_attr(obj, "value", 42) obj.value 42
Functions¶
attributes_set_attr ¶
Set attribute value on object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to set attribute on. |
required |
name
|
str
|
Attribute name. |
required |
value
|
Any
|
Value to set. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Examples:
>>> class MyClass:
... pass
>>> obj = MyClass()
>>> attributes_set_attr(obj, "value", 42)
>>> obj.value
42
Notes
Wrapper around setattr(). Creates attribute if doesn't exist.
attributes_has_attr ¶
Has Attribute Checker¶
Check if object has an attribute.
Examples¶
from rite.reflection.attributes import attributes_has_attr attributes_has_attr("hello", "upper") True
Functions¶
attributes_has_attr ¶
Check if object has an attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to check. |
required |
name
|
str
|
Attribute name. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if attribute exists. |
Examples:
>>> attributes_has_attr("hello", "upper")
True
>>> attributes_has_attr("hello", "nonexistent")
False
>>> attributes_has_attr([], "append")
True
Notes
Wrapper around hasattr().
attributes_del_attr ¶
Delete Attribute¶
Delete attribute from object.
Examples¶
from rite.reflection.attributes import attributes_del_attr class MyClass: ... value = 42 obj = MyClass() attributes_del_attr(obj, "value")
Functions¶
attributes_del_attr ¶
Delete attribute from object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Object to delete attribute from. |
required |
name
|
str
|
Attribute name. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If attribute doesn't exist. |
Examples:
>>> class MyClass:
... value = 42
>>> obj = MyClass()
>>> attributes_del_attr(obj, "value")
>>> hasattr(obj, "value")
False
Notes
Wrapper around delattr().
Signature¶
Function signature inspection.
Signature Module¶
Function signature inspection utilities.
This submodule provides utilities for inspecting function signatures, parameters, and return annotations.
Examples¶
from rite.reflection.signature import ( ... signature_get_signature, ... signature_get_parameters ... ) def func(a: int, b: str = "default") -> str: ... return f"{a}{b}" sig = signature_get_signature(func) len(sig.parameters) 2
Modules¶
signature_get_signature ¶
Signature Inspector¶
Get function or method signature.
Examples¶
from rite.reflection.signature import signature_get_signature def func(a: int, b: str = "default") -> str: ... return f"{a}{b}" sig = signature_get_signature(func) str(sig) "(a: int, b: str = 'default') -> str"
Functions¶
signature_get_signature ¶
Get function or method signature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Function, method, or class to inspect. |
required |
Returns:
| Type | Description |
|---|---|
Signature
|
Signature object. |
Examples:
>>> def func(a: int, b: str = "default") -> str:
... return f"{a}{b}"
>>> sig = signature_get_signature(func)
>>> len(sig.parameters)
2
Notes
Uses inspect.signature. Returns Signature object.
signature_get_parameters ¶
Parameters Inspector¶
Get function parameters.
Examples¶
from rite.reflection.signature import signature_get_parameters def func(a: int, b: str = "default") -> str: ... return f"{a}{b}" params = signature_get_parameters(func) list(params.keys()) ['a', 'b']
Functions¶
signature_get_parameters ¶
Get function parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Function or method to inspect. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Parameter]
|
Dictionary of parameter names to Parameter objects. |
Examples:
>>> def func(a: int, b: str = "default") -> str:
... return f"{a}{b}"
>>> params = signature_get_parameters(func)
>>> 'a' in params
True
>>> 'b' in params
True
Notes
Uses inspect.signature. Returns ordered dict of parameters.
signature_get_return_annotation ¶
Return Annotation Inspector¶
Get function return annotation.
Examples¶
from rite.reflection.signature import ( ... signature_get_return_annotation ... ) def func(a: int) -> str: ... return str(a) signature_get_return_annotation(func)
Functions¶
signature_get_return_annotation ¶
Get function return annotation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Function or method to inspect. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Return annotation or Signature.empty. |
Examples:
>>> def func(a: int) -> str:
... return str(a)
>>> signature_get_return_annotation(func)
<class 'str'>
Notes
Returns Signature.empty if no annotation.
Examples¶
from rite.reflection import (
importing_load_class,
inspection_get_methods,
signature_get_parameters
)
# Load class dynamically
MyClass = importing_load_class("mypackage.mymodule.MyClass")
# Inspect methods
methods = inspection_get_methods(MyClass)
# Get function parameters
def example(a: int, b: str = "default"):
pass
params = signature_get_parameters(example)