Overload
framework3.container.overload
¶
R = TypeVar('R')
module-attribute
¶
T = TypeVar('T')
module-attribute
¶
DispatchableMethod
¶
Bases: Protocol[R]
Protocol for a dispatchable method.
This protocol defines the interface for a method that can be dispatched based on the type of its arguments and can register new implementations.
Key Features
- Defines a callable interface
- Provides a register method for new implementations
Usage
This protocol is typically used as a type hint for methods that support dynamic dispatch based on argument types.
Methods:
Name | Description |
---|---|
__call__ |
Any, **kwargs: Any) -> R: Call the method with the given arguments. |
register |
type[T], func: Callable[..., R]) -> Callable[..., R]: Register a new implementation for the given class. |
Note
This is a Protocol class and is not meant to be instantiated directly. It serves as a structural subtyping tool for static type checking.
Source code in framework3/container/overload.py
__call__(*args, **kwargs)
¶
Call the method with the given arguments.
This method represents the main functionality of the dispatchable method. It will be called when the method is invoked and will dispatch to the appropriate implementation based on the types of the arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
Any
|
Positional arguments passed to the method. |
()
|
**kwargs
|
Any
|
Keyword arguments passed to the method. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
R |
R
|
The result of calling the appropriate implementation of the method. |
Source code in framework3/container/overload.py
register(cls, func)
¶
Register a new implementation for the given class.
This method allows registering new implementations for specific types. When the dispatchable method is called with an instance of the registered class as its argument, the corresponding implementation will be used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls
|
type[T]
|
The class for which to register the implementation. |
required |
func
|
Callable[..., R]
|
The function to be used as the implementation for the given class. |
required |
Returns:
Type | Description |
---|---|
Callable[..., R]
|
Callable[..., R]: The registered function, allowing for decorator-style usage. |
Source code in framework3/container/overload.py
SingleDispatch
¶
Bases: Protocol[R]
Protocol for a single dispatch function.
This protocol defines the interface for a function that can be dispatched based on the type of its first argument and can register new implementations.
Key Features
- Defines a callable interface
- Provides methods for registering and dispatching implementations
Usage
This protocol is typically used as a type hint for functions that support single dispatch based on the type of the first argument.
Methods:
Name | Description |
---|---|
__call__ |
Any, **kwargs: Any) -> R: Call the function with the given arguments. |
register |
type, func: Callable[..., R]) -> Callable[..., R]: Register a new implementation for the given class. |
dispatch |
type) -> Callable[..., R]: Return the implementation for the given class. |
Note
This is a Protocol class and is not meant to be instantiated directly. It serves as a structural subtyping tool for static type checking.
Source code in framework3/container/overload.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
|
__call__(*args, **kwargs)
¶
Call the function with the given arguments.
This method represents the main functionality of the single dispatch function. It will be called when the function is invoked and will dispatch to the appropriate implementation based on the type of the first argument.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
Any
|
Positional arguments passed to the function. |
()
|
**kwargs
|
Any
|
Keyword arguments passed to the function. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
R |
R
|
The result of calling the appropriate implementation of the function. |
Source code in framework3/container/overload.py
dispatch(cls)
¶
Return the implementation for the given class.
This method is used internally by the single dispatch mechanism to retrieve the appropriate implementation for a given type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls
|
type
|
The class for which to retrieve the implementation. |
required |
Returns:
Type | Description |
---|---|
Callable[..., R]
|
Callable[..., R]: The implementation function registered for the given class. |
Note
This method is typically used internally by the dispatch mechanism and not called directly by users of the single dispatch function.
Source code in framework3/container/overload.py
register(cls, func)
¶
Register a new implementation for the given class.
This method allows registering new implementations for specific types. When the single dispatch function is called with an instance of the registered class as its first argument, the corresponding implementation will be used.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls
|
type
|
The class for which to register the implementation. |
required |
func
|
Callable[..., R]
|
The function to be used as the implementation for the given class. |
required |
Returns:
Type | Description |
---|---|
Callable[..., R]
|
Callable[..., R]: The registered function, allowing for decorator-style usage. |
Source code in framework3/container/overload.py
fundispatch(func)
¶
Decorator for creating a function dispatch.
This decorator creates a wrapper around the given function that dispatches based on the type of the first argument.
Key Features
- Creates a dispatchable function
- Dispatches based on the type of the first argument
- Allows registration of new implementations
Usage
Use this decorator on functions that need to dispatch based on the type of their first argument.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
SingleDispatch[R]
|
The function to be wrapped. |
required |
Returns:
Type | Description |
---|---|
SingleDispatch[R]
|
SingleDispatch[R]: A wrapper function with dispatch capabilities. |
Note
The wrapped function will dispatch based on the type of the first argument. If the first argument is a type object, it will dispatch on that type directly. Otherwise, it will dispatch on the type of the first argument.
Source code in framework3/container/overload.py
methdispatch(func)
¶
Decorator for creating a method dispatch.
This decorator creates a wrapper around the given function that dispatches based on the type of the second argument (typically 'self' in method calls).
Key Features
- Creates a dispatchable method
- Dispatches based on the type of the second argument
- Allows registration of new implementations
Usage
Use this decorator on methods that need to dispatch based on the type of their second argument (typically the first argument after 'self').
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable[..., R]
|
The function to be wrapped. |
required |
Returns:
Type | Description |
---|---|
DispatchableMethod[R]
|
DispatchableMethod[R]: A wrapper function with dispatch capabilities. |
Note
The wrapped method will dispatch based on the type of the second argument, which is typically the first argument after 'self' in method calls.