Reflection Module¶
This module provides tools for dynamic proxy creation and reflection
DecoratorDescriptor(decorator, *args)
¶
A DecoratorDescriptor covers the decorator - a callable - and the passed arguments
Decorators
¶
Utility class that caches decorators ( Python does not have a feature for this )
add(func_or_class, decorator, *args)
classmethod
¶
Remember the decorator Args: func_or_class: a function or class decorator: the decorator *args: any arguments supplied to the decorator
get(func_or_class)
classmethod
¶
return the list of decorators associated with the given function or class Args: func_or_class: the function or class
Returns:
Type | Description |
---|---|
list[DecoratorDescriptor]
|
list[DecoratorDescriptor]: the list |
has_decorator(func_or_class, callable)
classmethod
¶
Return True, if the function or class is decorated with the decorator Args: func_or_class: a function or class callable: the decorator
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
the result |
DynamicProxy(type, invocation_handler)
¶
Bases: Generic[T]
DynamicProxy enables dynamic method interception and delegation for any class type.
It is used to create proxy objects that forward method calls to a custom InvocationHandler. This allows for advanced patterns such as aspect-oriented programming, logging, or remote invocation, by intercepting method calls at runtime and handling them as needed.
Usage:
class MyHandler(DynamicProxy.InvocationHandler):
def invoke(self, invocation):
print(f"Intercepted: {invocation.name}")
# custom logic here
return ...
proxy = DynamicProxy.create(SomeClass, MyHandler())
proxy.some_method(args) # Will be intercepted by MyHandler.invoke
TypeDescriptor(cls)
¶
This class provides a way to introspect Python classes, their methods, decorators, and type hints.
MethodDescriptor(cls, method)
¶
This class represents a method of a class, including its decorators, parameter types, and return type.
get_decorator(decorator)
¶
return the DecoratorDescriptor - if any - associated with the passed Callable
Parameters:
Name | Type | Description | Default |
---|---|---|---|
decorator
|
Callable
|
the decorator |
required |
Returns:
Type | Description |
---|---|
Optional[DecoratorDescriptor]
|
Optional[DecoratorDescriptor]: the DecoratorDescriptor or None |
get_doc(default='')
¶
return the method docstring
Parameters:
Name | Type | Description | Default |
---|---|---|---|
default
|
the default if no docstring is found |
''
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
the docstring |
get_name()
¶
return the method name
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
the method name |
has_decorator(decorator)
¶
return True if the method is decorated with the decorator
Parameters:
Name | Type | Description | Default |
---|---|---|---|
decorator
|
Callable
|
the decorator callable |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the method is decorated with the decorator |
is_async()
¶
return true if the method is asynchronous
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
async flag |
for_type(clazz)
classmethod
¶
Returns a TypeDescriptor for the given class, using a cache to avoid redundant introspection.
get_decorator(decorator)
¶
Returns the first decorator of the given type, or None if not found.
get_method(name, local=False)
¶
Returns a MethodDescriptor for the method with the given name. If local is True, only searches for methods defined in the class itself, otherwise includes inherited methods.
get_methods(local=False)
¶
Returns a list of MethodDescriptor objects for the class. If local is True, only returns methods defined in the class itself, otherwise includes inherited methods.
has_decorator(decorator)
¶
Checks if the class has a decorator of the given type.