sinapsis_core 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sinapsis_core/__init__.py +0 -0
- sinapsis_core/agent/__init__.py +5 -0
- sinapsis_core/agent/agent.py +362 -0
- sinapsis_core/agent/agent_helpers.py +112 -0
- sinapsis_core/agent/base_models.py +361 -0
- sinapsis_core/agent/builder.py +112 -0
- sinapsis_core/agent/profiler_agent.py +117 -0
- sinapsis_core/cli/__init__.py +0 -0
- sinapsis_core/cli/arg_parser.py +29 -0
- sinapsis_core/cli/display_template_info.py +202 -0
- sinapsis_core/cli/run_agent_from_config.py +40 -0
- sinapsis_core/context_managers/__init__.py +0 -0
- sinapsis_core/context_managers/env_var_override_context_manager.py +39 -0
- sinapsis_core/context_managers/working_dir_context_manager.py +57 -0
- sinapsis_core/data_containers/__init__.py +0 -0
- sinapsis_core/data_containers/annotations.py +152 -0
- sinapsis_core/data_containers/data_packet.py +364 -0
- sinapsis_core/data_containers/model_types/__init__.py +38 -0
- sinapsis_core/data_containers/model_types/_asr_base_models.py +39 -0
- sinapsis_core/data_containers/model_types/llm_types.py +189 -0
- sinapsis_core/exceptions/agent_exceptions.py +2 -0
- sinapsis_core/hooks/__init__.py +15 -0
- sinapsis_core/hooks/pre_init_hook_registry.py +225 -0
- sinapsis_core/template_base/__init__.py +10 -0
- sinapsis_core/template_base/base_models.py +59 -0
- sinapsis_core/template_base/copy_data_container.py +46 -0
- sinapsis_core/template_base/dynamic_template.py +169 -0
- sinapsis_core/template_base/dynamic_template_factory.py +454 -0
- sinapsis_core/template_base/dynamic_template_helpers/__init__.py +0 -0
- sinapsis_core/template_base/dynamic_template_helpers/builder_methods.py +92 -0
- sinapsis_core/template_base/dynamic_template_helpers/signature_parser.py +625 -0
- sinapsis_core/template_base/empty_data_container.py +23 -0
- sinapsis_core/template_base/input_template.py +8 -0
- sinapsis_core/template_base/merge_data_flow.py +33 -0
- sinapsis_core/template_base/multi_execute_template.py +65 -0
- sinapsis_core/template_base/output_template.py +8 -0
- sinapsis_core/template_base/split_data_flow.py +50 -0
- sinapsis_core/template_base/template.py +292 -0
- sinapsis_core/template_base/transfer_data_container.py +13 -0
- sinapsis_core/utils/__init__.py +0 -0
- sinapsis_core/utils/base_model_updater.py +148 -0
- sinapsis_core/utils/doc_string_generator.py +20 -0
- sinapsis_core/utils/env_var_keys.py +249 -0
- sinapsis_core/utils/env_var_utils.py +6 -0
- sinapsis_core/utils/logging_utils.py +53 -0
- sinapsis_core-1.0.0.dist-info/METADATA +239 -0
- sinapsis_core-1.0.0.dist-info/RECORD +50 -0
- sinapsis_core-1.0.0.dist-info/WHEEL +4 -0
- sinapsis_core-1.0.0.dist-info/licenses/LICENSE-APACHE +202 -0
- sinapsis_core-1.0.0.dist-info/licenses/LICENSE-MIT +21 -0
|
@@ -0,0 +1,625 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
import abc
|
|
3
|
+
import importlib
|
|
4
|
+
import inspect
|
|
5
|
+
import types
|
|
6
|
+
from typing import ( # type:ignore[attr-defined]
|
|
7
|
+
Any,
|
|
8
|
+
Callable,
|
|
9
|
+
Literal,
|
|
10
|
+
Optional,
|
|
11
|
+
Union,
|
|
12
|
+
# _get_defaults,
|
|
13
|
+
get_args,
|
|
14
|
+
get_origin,
|
|
15
|
+
get_type_hints,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
from docstring_parser import parse
|
|
19
|
+
|
|
20
|
+
from sinapsis_core.template_base.dynamic_template_helpers.builder_methods import (
|
|
21
|
+
ArgDictKeywords,
|
|
22
|
+
NoAnnotationError,
|
|
23
|
+
evaluate_str_type_annotation,
|
|
24
|
+
extract_set_from_docstring_annotation,
|
|
25
|
+
)
|
|
26
|
+
from sinapsis_core.utils.logging_utils import sinapsis_logger
|
|
27
|
+
|
|
28
|
+
PROTECTED_SIGNATURE_KEYS = ["self", "return"]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def is_built_in(p_object: Any) -> bool:
|
|
32
|
+
"""Check if an object is a Python built-in type.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
p_object (Any): Any Python object to check
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
bool: True if object is from builtins or __builtin__ modules, False otherwise
|
|
39
|
+
|
|
40
|
+
Examples:
|
|
41
|
+
>>> is_built_in(str)
|
|
42
|
+
True
|
|
43
|
+
>>> is_built_in(CustomClass)
|
|
44
|
+
False
|
|
45
|
+
"""
|
|
46
|
+
return get_class_object_module(p_object) in ["builtins", "__builtin__"]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def get_class_object_module(p_object: Any) -> str:
|
|
50
|
+
"""Get the module name where an object's class is defined.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
p_object (Any): Any Python object
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
str: Name of the module containing the object's class definition
|
|
57
|
+
|
|
58
|
+
Examples:
|
|
59
|
+
>>> get_class_object_module(str())
|
|
60
|
+
'builtins'
|
|
61
|
+
"""
|
|
62
|
+
return p_object.__class__.__module__
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def has_custom_meta_class(class_obj: Callable) -> bool:
|
|
66
|
+
"""Determine if a class uses a custom metaclass.
|
|
67
|
+
|
|
68
|
+
Args:
|
|
69
|
+
class_obj (Callable): Class object to inspect
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
bool: True if class uses custom metaclass other than abc.ABCMeta, False otherwise
|
|
73
|
+
"""
|
|
74
|
+
return class_obj.__class__ != abc.ABCMeta
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def has_empty_or_no_annotations(class_or_method: Callable) -> bool:
|
|
78
|
+
"""Check if a class or method lacks type annotations.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
class_or_method (Callable): Class or method to inspect
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
bool: True if object has no __annotations__ attribute or empty annotations
|
|
85
|
+
|
|
86
|
+
Examples:
|
|
87
|
+
>>> def func(): pass
|
|
88
|
+
>>> has_empty_or_no_annotations(func)
|
|
89
|
+
True
|
|
90
|
+
"""
|
|
91
|
+
return not hasattr(class_or_method, ArgDictKeywords.annotations) or not class_or_method.__annotations__
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def inspect_param_is_empty(att_val: Any) -> bool:
|
|
95
|
+
"""Check if a parameter value is empty according to inspect module.
|
|
96
|
+
|
|
97
|
+
Args:
|
|
98
|
+
att_val (Any): Parameter value to check
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
bool: True if parameter is inspect._empty, False otherwise
|
|
102
|
+
|
|
103
|
+
Examples:
|
|
104
|
+
>>> inspect_param_is_empty(inspect.Parameter.empty)
|
|
105
|
+
True
|
|
106
|
+
"""
|
|
107
|
+
return att_val == inspect._empty
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def get_class_name(class_or_method: Callable) -> str:
|
|
111
|
+
"""Get the name of a class or method.
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
class_or_method (Callable): Class or method object
|
|
115
|
+
|
|
116
|
+
Returns:
|
|
117
|
+
str: Name of the class or method
|
|
118
|
+
|
|
119
|
+
Examples:
|
|
120
|
+
>>> class Example: pass
|
|
121
|
+
>>> get_class_name(Example)
|
|
122
|
+
'Example'
|
|
123
|
+
"""
|
|
124
|
+
return class_or_method.__name__
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class SignatureParser:
|
|
128
|
+
"""
|
|
129
|
+
This class provides a collection of methods to parse a method or class object's signature and
|
|
130
|
+
type annotations.
|
|
131
|
+
"""
|
|
132
|
+
|
|
133
|
+
logger = sinapsis_logger
|
|
134
|
+
|
|
135
|
+
@classmethod
|
|
136
|
+
def parse_callable_signature(cls, callable_object: Callable) -> tuple[dict, dict]:
|
|
137
|
+
"""Parse a callable's signature to extract parameter information.
|
|
138
|
+
|
|
139
|
+
Args:
|
|
140
|
+
callable_object (Callable): Function, method or class to inspect
|
|
141
|
+
|
|
142
|
+
Returns:
|
|
143
|
+
tuple[dict, dict]: Two dictionaries containing:
|
|
144
|
+
- Parameter default values {param_name: default_value}
|
|
145
|
+
- Parameter type hints {param_name: type_annotation}
|
|
146
|
+
|
|
147
|
+
Raises:
|
|
148
|
+
ValueError: If unable to get type hints for the callable
|
|
149
|
+
|
|
150
|
+
Examples:
|
|
151
|
+
>>> def example(x: int = 1, y: str = "test"): pass
|
|
152
|
+
>>> defaults, types = SignatureParser.parse_callable_signature(example)
|
|
153
|
+
>>> print(defaults)
|
|
154
|
+
{'x': 1, 'y': 'test'}
|
|
155
|
+
>>> print(types)
|
|
156
|
+
{'x': <class 'int'>, 'y': <class 'str'>}
|
|
157
|
+
"""
|
|
158
|
+
type_hints = cls.__get_type_hints(callable_object)
|
|
159
|
+
default_values = cls.__get_default_values(callable_object)
|
|
160
|
+
return default_values, type_hints
|
|
161
|
+
|
|
162
|
+
# @classmethod
|
|
163
|
+
# def __get_default_values(cls, class_or_method: Callable) -> dict:
|
|
164
|
+
# """
|
|
165
|
+
# Retrieves the default values of a callable object. It tries to use typing,
|
|
166
|
+
# and if it fails it falls back to inspect.
|
|
167
|
+
# Args:
|
|
168
|
+
# class_or_method (Callable): Function, method, or class to analyze
|
|
169
|
+
#
|
|
170
|
+
# Returns:
|
|
171
|
+
# dict[str, Any]: Returns a dictionary mapping parameter names to their
|
|
172
|
+
# default values
|
|
173
|
+
# """
|
|
174
|
+
# try:
|
|
175
|
+
# default_values = _get_defaults(class_or_method)
|
|
176
|
+
# except NameError:
|
|
177
|
+
# default_values = _get_defaults(class_or_method.__init__) if inspect.isclass(class_or_method) else {}
|
|
178
|
+
#
|
|
179
|
+
# if not default_values:
|
|
180
|
+
# default_values = cls._parse_defaults_with_inspect(class_or_method)
|
|
181
|
+
#
|
|
182
|
+
# cls._remove_protected_arg_keys(default_values)
|
|
183
|
+
# return default_values
|
|
184
|
+
|
|
185
|
+
@classmethod
|
|
186
|
+
def __get_default_values(cls, class_or_method: Callable) -> dict[str, Any]:
|
|
187
|
+
"""
|
|
188
|
+
Retrieves the default values of a callable object using inspect.signature.
|
|
189
|
+
|
|
190
|
+
Args:
|
|
191
|
+
class_or_method (Callable): Function, method, or class to analyze
|
|
192
|
+
|
|
193
|
+
Returns:
|
|
194
|
+
dict[str, Any]: Returns a dictionary mapping parameter names to their
|
|
195
|
+
default values.
|
|
196
|
+
"""
|
|
197
|
+
default_values = cls._parse_defaults_with_inspect(class_or_method)
|
|
198
|
+
cls._remove_protected_arg_keys(default_values)
|
|
199
|
+
return default_values
|
|
200
|
+
|
|
201
|
+
@classmethod
|
|
202
|
+
def _parse_defaults_with_inspect(cls, class_or_method: Callable) -> dict[str, Any]:
|
|
203
|
+
"""
|
|
204
|
+
Uses inspect to parse the default values of a callable object. If there are no default values
|
|
205
|
+
due to an argument being required, it gets populated with None.
|
|
206
|
+
|
|
207
|
+
Args:
|
|
208
|
+
class_or_method (Callable): Function, method or class to inspect
|
|
209
|
+
|
|
210
|
+
Returns:
|
|
211
|
+
dict[str, Any]: Parameter names mapped to default values, with None for required parameters
|
|
212
|
+
|
|
213
|
+
Examples:
|
|
214
|
+
>>> def func(x: int = 1, y: str): pass
|
|
215
|
+
>>> SignatureParser._parse_defaults_with_inspect(func)
|
|
216
|
+
{'x': 1, 'y': None}
|
|
217
|
+
"""
|
|
218
|
+
sig = inspect.signature(class_or_method)
|
|
219
|
+
|
|
220
|
+
default_vals_with_none = {
|
|
221
|
+
param_name: cls._empty_required_arg_value_to_none(param_value.default)
|
|
222
|
+
for param_name, param_value in sig.parameters.items()
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return default_vals_with_none
|
|
226
|
+
|
|
227
|
+
@classmethod
|
|
228
|
+
def _remove_protected_arg_keys(cls, default_vals_or_typing_dict: dict[str, Any]) -> None:
|
|
229
|
+
"""Remove 'self' and 'return' keys from parameter dictionaries.
|
|
230
|
+
|
|
231
|
+
Args:
|
|
232
|
+
default_vals_or_typing_dict (dict[str, Any]): Dictionary of parameter information
|
|
233
|
+
|
|
234
|
+
Examples:
|
|
235
|
+
>>> d = {'self': None, 'x': 1, 'return': None}
|
|
236
|
+
>>> SignatureParser._remove_protected_arg_keys(d)
|
|
237
|
+
>>> print(d)
|
|
238
|
+
{'x': 1}
|
|
239
|
+
"""
|
|
240
|
+
_ = default_vals_or_typing_dict.pop("return", None)
|
|
241
|
+
_ = default_vals_or_typing_dict.pop("self", None)
|
|
242
|
+
|
|
243
|
+
@classmethod
|
|
244
|
+
def __set_custom_metadata_types_if_needed(cls, class_or_method: Callable) -> dict[str, Any]:
|
|
245
|
+
"""
|
|
246
|
+
Custom metaclasses will return type hints for class attributes when using
|
|
247
|
+
typing.get_type_hints, instead of type hints for the __init__ method.
|
|
248
|
+
|
|
249
|
+
Args:
|
|
250
|
+
class_or_method (Callable): Class or method to inspect
|
|
251
|
+
|
|
252
|
+
Returns:
|
|
253
|
+
dict[str, Any]: Type hints dictionary for class attributes when using custom metaclass
|
|
254
|
+
"""
|
|
255
|
+
if (has_custom_meta_class(class_or_method)) or (has_empty_or_no_annotations(class_or_method)):
|
|
256
|
+
_, inspect_type_hints = cls._build_args_dict_with_inspect(class_or_method)
|
|
257
|
+
if inspect.ismethod(class_or_method):
|
|
258
|
+
class_or_method.__func__.__annotations__ = inspect_type_hints
|
|
259
|
+
else:
|
|
260
|
+
class_or_method.__annotations__ = inspect_type_hints
|
|
261
|
+
return inspect_type_hints
|
|
262
|
+
return {}
|
|
263
|
+
|
|
264
|
+
@classmethod
|
|
265
|
+
def __get_type_hints(cls, class_or_method: Callable) -> dict:
|
|
266
|
+
"""
|
|
267
|
+
Extract type hints from a callable using multiple strategies.
|
|
268
|
+
|
|
269
|
+
Args:
|
|
270
|
+
class_or_method (Callable): Function, method or class to inspect
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
Returns:
|
|
274
|
+
dict: Mapping of parameter names to their type annotations
|
|
275
|
+
|
|
276
|
+
Raises:
|
|
277
|
+
ValueError: If unable to get type hints through any method
|
|
278
|
+
|
|
279
|
+
Notes:
|
|
280
|
+
- If the dynamic class is a child class, and the `from __future__ import annotations`
|
|
281
|
+
directive is used in the definition of either the child or
|
|
282
|
+
parent class, it may result in NameError exceptions.
|
|
283
|
+
This occurs because, at the time the class is being dynamically constructed,
|
|
284
|
+
certain references in globals() or locals() may not yet be fully resolved or defined
|
|
285
|
+
"""
|
|
286
|
+
|
|
287
|
+
inspect_type_hints = cls.__set_custom_metadata_types_if_needed(class_or_method)
|
|
288
|
+
try:
|
|
289
|
+
type_hints = get_type_hints(class_or_method, include_extras=False)
|
|
290
|
+
if inspect_type_hints:
|
|
291
|
+
type_hints = {k: v for k, v in type_hints.items() if k in inspect_type_hints}
|
|
292
|
+
except NameError:
|
|
293
|
+
type_hints = inspect_type_hints
|
|
294
|
+
|
|
295
|
+
if not type_hints:
|
|
296
|
+
cls.logger.debug(f"Unable to get type hints for {class_or_method}, trying with inspect")
|
|
297
|
+
_, type_hints = cls._build_args_dict_with_inspect(class_or_method)
|
|
298
|
+
|
|
299
|
+
cls._remove_protected_arg_keys(type_hints)
|
|
300
|
+
|
|
301
|
+
if not type_hints:
|
|
302
|
+
cls.logger.error(f"Unable to get type hints for {class_or_method}")
|
|
303
|
+
# raise ValueError(f"Unable to get type hints for {class_or_method}")
|
|
304
|
+
|
|
305
|
+
return type_hints
|
|
306
|
+
|
|
307
|
+
# @classmethod
|
|
308
|
+
# def _parse_defaults_with_inspect(cls, class_or_method: Callable) -> dict[str, Any]:
|
|
309
|
+
# """
|
|
310
|
+
# Uses inspect to parse the default values of a callable object. If there are no default values
|
|
311
|
+
# due to an argument being required, it gets populated with None.
|
|
312
|
+
#
|
|
313
|
+
# Args:
|
|
314
|
+
# class_or_method (Callable): Function, method or class to inspect
|
|
315
|
+
#
|
|
316
|
+
# Returns:
|
|
317
|
+
# dict[str, Any]: Parameter names mapped to default values, with None for required parameters
|
|
318
|
+
#
|
|
319
|
+
# Examples:
|
|
320
|
+
# >>> def func(x: int = 1, y: str): pass
|
|
321
|
+
# >>> SignatureParser._parse_defaults_with_inspect(func)
|
|
322
|
+
# {'x': 1, 'y': None}
|
|
323
|
+
# """
|
|
324
|
+
# default_vals = {
|
|
325
|
+
# param_name: param_value.default
|
|
326
|
+
# for param_name, param_value in inspect.signature(class_or_method).parameters.items()
|
|
327
|
+
# }
|
|
328
|
+
# default_vals_with_none = {}
|
|
329
|
+
# for param_name, param_value in default_vals.items():
|
|
330
|
+
# default_vals_with_none[param_name] = cls._empty_required_arg_value_to_none(param_value)
|
|
331
|
+
# if default_vals_with_none:
|
|
332
|
+
# return default_vals_with_none
|
|
333
|
+
# return default_vals
|
|
334
|
+
|
|
335
|
+
@classmethod
|
|
336
|
+
def _resolve_non_built_in_types(cls, default_val: Any, att_val: Any) -> Any:
|
|
337
|
+
"""
|
|
338
|
+
For cases where typing is a string or a non-built in type, try to resolve it by importing
|
|
339
|
+
the type from the module it belongs to. If this fails, fall back to the original annotation.
|
|
340
|
+
Args:
|
|
341
|
+
default_val (Any): Parameter information from inspect.Parameter
|
|
342
|
+
att_val (Any): Default value of the parameter
|
|
343
|
+
|
|
344
|
+
Returns:
|
|
345
|
+
Any: Resolved type annotation or original annotation if resolution fails
|
|
346
|
+
"""
|
|
347
|
+
default_val_module = get_class_object_module(att_val)
|
|
348
|
+
|
|
349
|
+
if isinstance(default_val.annotation, str) or get_origin(default_val.annotation) == Union:
|
|
350
|
+
ann_name = default_val.annotation
|
|
351
|
+
else:
|
|
352
|
+
ann_name = default_val.annotation.__name__
|
|
353
|
+
|
|
354
|
+
try:
|
|
355
|
+
if isinstance(default_val_module, str):
|
|
356
|
+
annotation = getattr(importlib.import_module(default_val_module), ann_name)
|
|
357
|
+
else:
|
|
358
|
+
annotation = getattr(importlib.import_module(default_val.default), default_val.default.__name__)
|
|
359
|
+
except (TypeError, AttributeError):
|
|
360
|
+
sinapsis_logger.error(
|
|
361
|
+
"Unable to find user-defined annotation type, falling back to the original annotation."
|
|
362
|
+
)
|
|
363
|
+
annotation = default_val.annotation
|
|
364
|
+
return annotation
|
|
365
|
+
|
|
366
|
+
@classmethod
|
|
367
|
+
def _resolve_union_types(cls, default_val: Any) -> Union[tuple[Any]]:
|
|
368
|
+
"""
|
|
369
|
+
In cases where we have a UnionType, iterate over each member and discard non-built-in types.
|
|
370
|
+
|
|
371
|
+
Args:
|
|
372
|
+
default_val (Any): Parameter information containing Union type annotation
|
|
373
|
+
|
|
374
|
+
Returns:
|
|
375
|
+
Union[tuple[Any]]: Updated Union type containing only built-in types
|
|
376
|
+
|
|
377
|
+
Examples:
|
|
378
|
+
>>> # Union[str, CustomType] becomes Union[str]
|
|
379
|
+
"""
|
|
380
|
+
updated_annotation: Any = [arg for arg in get_args(default_val.annotation) if is_built_in(arg)]
|
|
381
|
+
updated_annotation = Union[tuple(updated_annotation)]
|
|
382
|
+
return updated_annotation
|
|
383
|
+
|
|
384
|
+
@classmethod
|
|
385
|
+
def _raise_if_not_parseable(cls, class_name: str, argument_name: str, inspect_param: Any) -> None:
|
|
386
|
+
"""Validate that a parameter has either type annotation or default value.
|
|
387
|
+
|
|
388
|
+
Args:
|
|
389
|
+
class_name (str): Name of the class being inspected
|
|
390
|
+
argument_name (str): Name of the parameter
|
|
391
|
+
inspect_param: Parameter information from inspect.Parameter
|
|
392
|
+
|
|
393
|
+
Raises:
|
|
394
|
+
NoAnnotationError: If parameter lacks both annotation and default value
|
|
395
|
+
"""
|
|
396
|
+
if inspect_param_is_empty(inspect_param.annotation) and inspect_param_is_empty(inspect_param.default):
|
|
397
|
+
raise NoAnnotationError(f"{argument_name} for {class_name} is empty or undefined but it is also required")
|
|
398
|
+
|
|
399
|
+
@classmethod
|
|
400
|
+
def _build_args_dict_with_inspect(cls, dynamic_class: Callable) -> tuple[dict, dict]:
|
|
401
|
+
"""
|
|
402
|
+
This method tries to produce similar results to typing.get_type_hints with the exclusion of
|
|
403
|
+
non-built-in types which cannot be resolved. One example of such types is ForwardRefs or user
|
|
404
|
+
defined types that have not yet been imported.
|
|
405
|
+
|
|
406
|
+
If the assigned default value is a non-built-in type, the method attempts to resolve it by
|
|
407
|
+
importing the type from its corresponding module.
|
|
408
|
+
|
|
409
|
+
Args:
|
|
410
|
+
dynamic_class (Callable): Class, function or method to inspect
|
|
411
|
+
|
|
412
|
+
Returns:
|
|
413
|
+
tuple[dict, dict]: Returns a tuple (args_dict, ann_types), where args_dict
|
|
414
|
+
maps parameter names to default values, and ann_types maps parameter
|
|
415
|
+
names to type annotations
|
|
416
|
+
|
|
417
|
+
Raises:
|
|
418
|
+
NoAnnotationError: When required parameters lack both annotation and default value
|
|
419
|
+
|
|
420
|
+
Notes:
|
|
421
|
+
- Handles built-in types, unions, and forward references
|
|
422
|
+
- Skips optional arguments that cannot be resolved
|
|
423
|
+
- Processes non-built-in types through module imports
|
|
424
|
+
|
|
425
|
+
Examples:
|
|
426
|
+
>>> class Example:
|
|
427
|
+
... def __init__(self, x: int, y: str = "test"): pass
|
|
428
|
+
>>> args, types = SignatureParser._build_args_dict_with_inspect(Example)
|
|
429
|
+
>>> print(args)
|
|
430
|
+
{'x': None, 'y': 'test'}
|
|
431
|
+
"""
|
|
432
|
+
|
|
433
|
+
args_dict = {}
|
|
434
|
+
ann_types = {}
|
|
435
|
+
class_name = get_class_name(dynamic_class)
|
|
436
|
+
for argument_name, inspect_param in inspect.signature(dynamic_class).parameters.items():
|
|
437
|
+
if inspect_param.name in PROTECTED_SIGNATURE_KEYS or inspect_param.kind == inspect.Parameter.VAR_KEYWORD:
|
|
438
|
+
continue
|
|
439
|
+
annotation = inspect_param.annotation
|
|
440
|
+
cls._raise_if_not_parseable(class_name, argument_name, inspect_param)
|
|
441
|
+
if inspect_param_is_empty(annotation):
|
|
442
|
+
annotation = type(inspect_param.default)
|
|
443
|
+
att_val = cls._empty_required_arg_value_to_none(inspect_param.default)
|
|
444
|
+
|
|
445
|
+
if not is_built_in(inspect_param.default):
|
|
446
|
+
annotation = cls._resolve_non_built_in_types(inspect_param, att_val)
|
|
447
|
+
|
|
448
|
+
elif isinstance(inspect_param.annotation, types.UnionType):
|
|
449
|
+
annotation = cls._resolve_union_types(inspect_param)
|
|
450
|
+
|
|
451
|
+
if isinstance(annotation, str):
|
|
452
|
+
annotation = evaluate_str_type_annotation(annotation)
|
|
453
|
+
|
|
454
|
+
# if we reach this far and still have not managed to resolve the annotation, we shouldn't use it
|
|
455
|
+
if not annotation and inspect_param_is_empty(inspect_param.default):
|
|
456
|
+
raise NoAnnotationError(
|
|
457
|
+
f"{argument_name} for {class_name} is empty or undefined but it is also required"
|
|
458
|
+
)
|
|
459
|
+
elif not annotation: # if not ann but is optional, skip it
|
|
460
|
+
sinapsis_logger.warning(f"Skipping optional argument {argument_name} for {class_name}")
|
|
461
|
+
continue
|
|
462
|
+
|
|
463
|
+
sinapsis_logger.debug(
|
|
464
|
+
f"adding argument_name: {argument_name}, att_val: {att_val}, "
|
|
465
|
+
f"inspect_param.name: {inspect_param.name} ann: {annotation}"
|
|
466
|
+
)
|
|
467
|
+
args_dict[argument_name] = att_val
|
|
468
|
+
ann_types[inspect_param.name] = annotation
|
|
469
|
+
return args_dict, ann_types
|
|
470
|
+
|
|
471
|
+
@classmethod
|
|
472
|
+
def _empty_required_arg_value_to_none(cls, att_val: Any) -> Any:
|
|
473
|
+
"""
|
|
474
|
+
If a required argument is not present in the signature, set its value to None
|
|
475
|
+
so that it can be appended to the argument dictionary.
|
|
476
|
+
Args:
|
|
477
|
+
att_val (Any): Parameter value to check and potentially convert
|
|
478
|
+
|
|
479
|
+
Returns:
|
|
480
|
+
Any: None if value is inspect._empty, otherwise original value
|
|
481
|
+
"""
|
|
482
|
+
if att_val == inspect._empty:
|
|
483
|
+
att_val = None
|
|
484
|
+
return att_val
|
|
485
|
+
|
|
486
|
+
|
|
487
|
+
class SignatureParserFromDocStr(SignatureParser):
|
|
488
|
+
@staticmethod
|
|
489
|
+
def _str_annotation_is_set(att_val: Any, doc_str_param: Any) -> bool:
|
|
490
|
+
"""Check if docstring contains a set-like type annotation.
|
|
491
|
+
|
|
492
|
+
Args:
|
|
493
|
+
att_val (Any): Parameter's default value
|
|
494
|
+
doc_str_param: Parsed docstring parameter information
|
|
495
|
+
|
|
496
|
+
Returns:
|
|
497
|
+
bool: True if annotation represents a set of values
|
|
498
|
+
|
|
499
|
+
Examples:
|
|
500
|
+
>>> # Matches format like: {option1,option2,option3}
|
|
501
|
+
"""
|
|
502
|
+
return (type(att_val) is str or att_val is None) and doc_str_param.type_name.startswith("{")
|
|
503
|
+
|
|
504
|
+
@classmethod
|
|
505
|
+
def parse_callable_signature(cls, callable_object: Callable) -> tuple[dict, dict]:
|
|
506
|
+
"""Parse callable signature using docstring type information.
|
|
507
|
+
|
|
508
|
+
Args:
|
|
509
|
+
callable_object (Callable): Function, method or class to inspect
|
|
510
|
+
|
|
511
|
+
Returns:
|
|
512
|
+
tuple[dict, dict]: Returns a tuple (args_dict, ann_types), where args_dict
|
|
513
|
+
contains parameter defaults extracted from the signature, and ann_types
|
|
514
|
+
contains type information parsed from docstrings
|
|
515
|
+
|
|
516
|
+
Notes:
|
|
517
|
+
- Extracts type information from docstring format
|
|
518
|
+
- Handles set-like annotations in docstrings
|
|
519
|
+
- Converts docstring types to typing module equivalents
|
|
520
|
+
|
|
521
|
+
Examples:
|
|
522
|
+
>>> def func(x):
|
|
523
|
+
... '''Args: x (int): A number'''
|
|
524
|
+
... pass
|
|
525
|
+
>>> defaults, types = SignatureParserFromDocStr.parse_callable_signature(func)
|
|
526
|
+
>>> defaults
|
|
527
|
+
{'x': None}
|
|
528
|
+
"""
|
|
529
|
+
|
|
530
|
+
args_dict = {}
|
|
531
|
+
ann_types = {}
|
|
532
|
+
|
|
533
|
+
doc_string = parse(callable_object.__doc__)
|
|
534
|
+
|
|
535
|
+
for argument_name, inspect_param in inspect.signature(callable_object).parameters.items():
|
|
536
|
+
att_val = inspect_param.default
|
|
537
|
+
|
|
538
|
+
for doc_str_param in doc_string.params:
|
|
539
|
+
if argument_name == doc_str_param.arg_name:
|
|
540
|
+
annotation = inspect_param.annotation
|
|
541
|
+
if cls._str_annotation_is_set(att_val, doc_str_param):
|
|
542
|
+
annotation = cls._parse_set_doc_str_typing(doc_str_param)
|
|
543
|
+
|
|
544
|
+
if annotation == inspect._empty:
|
|
545
|
+
try:
|
|
546
|
+
ann_name = doc_str_param.type_name.split(",")[0]
|
|
547
|
+
annotation = None if ann_name in ["object", "callable"] else eval(ann_name)
|
|
548
|
+
except TypeError:
|
|
549
|
+
# special case for sktime
|
|
550
|
+
import re
|
|
551
|
+
|
|
552
|
+
match = re.match(r"^([a-zA-Z_]\w*)\s*\(default\s*=", text)
|
|
553
|
+
if match:
|
|
554
|
+
annotation = match.group(1)
|
|
555
|
+
else:
|
|
556
|
+
raise
|
|
557
|
+
except (NameError, SyntaxError):
|
|
558
|
+
annotation = type(att_val)
|
|
559
|
+
|
|
560
|
+
if type(att_val) is not annotation and get_origin(annotation) != Literal:
|
|
561
|
+
annotation = Optional[annotation]
|
|
562
|
+
|
|
563
|
+
args_dict[argument_name] = att_val
|
|
564
|
+
ann_types[inspect_param.name] = annotation
|
|
565
|
+
cls.logger.info(
|
|
566
|
+
f"{callable_object} adding att: {argument_name}, att_val: {att_val}, ann: {annotation}"
|
|
567
|
+
)
|
|
568
|
+
return args_dict, ann_types
|
|
569
|
+
|
|
570
|
+
@classmethod
|
|
571
|
+
def _parse_set_doc_str_typing(cls, doc_str_param: Any) -> str | Any | None:
|
|
572
|
+
"""Convert set-like docstring annotations to typing types.
|
|
573
|
+
|
|
574
|
+
Args:
|
|
575
|
+
doc_str_param: Parsed docstring parameter information containing set-like type
|
|
576
|
+
|
|
577
|
+
Returns:
|
|
578
|
+
str | Any | None: Returns either a Literal type
|
|
579
|
+
for string-only sets or an Optional Union for mixed types
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
Examples:
|
|
583
|
+
>>> # For docstring: "{red,green,blue}"
|
|
584
|
+
>>> # Returns: Literal["red", "green", "blue"]
|
|
585
|
+
"""
|
|
586
|
+
ann_tuple = extract_set_from_docstring_annotation(doc_str_param.type_name)
|
|
587
|
+
evaluated_annotations = [evaluate_str_type_annotation(ann_option) or ann_option for ann_option in ann_tuple]
|
|
588
|
+
all_str_ann_tuple = all(isinstance(x, str) for x in evaluated_annotations)
|
|
589
|
+
annotation = Literal[tuple(ann_tuple)] if all_str_ann_tuple else Optional[Union[str, Any]]
|
|
590
|
+
|
|
591
|
+
return annotation
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
class SignatureParserWithMRO(SignatureParser):
|
|
595
|
+
@classmethod
|
|
596
|
+
def parse_callable_signature(cls, callable_object: Callable) -> tuple[dict, dict]:
|
|
597
|
+
"""Parse signatures through entire inheritance chain of a class.
|
|
598
|
+
|
|
599
|
+
Args:
|
|
600
|
+
callable_object (Callable): Class to inspect
|
|
601
|
+
|
|
602
|
+
Returns:
|
|
603
|
+
tuple[dict, dict]: Returns a tuple (merged_args_dict, merged_ann_types),
|
|
604
|
+
where merged_args_dict combines parameter defaults from all classes
|
|
605
|
+
in the MRO chain, and merged_ann_types combines their type annotations
|
|
606
|
+
|
|
607
|
+
Examples:
|
|
608
|
+
>>> class Parent:
|
|
609
|
+
... def __init__(self, x: int): pass
|
|
610
|
+
>>> class Child(Parent):
|
|
611
|
+
... def __init__(self, y: str): pass
|
|
612
|
+
>>> defaults, types = SignatureParserWithMRO.parse_callable_signature(Child)
|
|
613
|
+
>>> print(types)
|
|
614
|
+
{'x': <class 'int'>, 'y': <class 'str'>}
|
|
615
|
+
"""
|
|
616
|
+
merged_args_dict = {}
|
|
617
|
+
merged_ann_types = {}
|
|
618
|
+
for base_cls in reversed(inspect.getmro(callable_object)): # type:ignore[arg-type]
|
|
619
|
+
if base_cls is object or base_cls is abc.ABC:
|
|
620
|
+
continue
|
|
621
|
+
args_dict, ann_types = super(SignatureParserWithMRO, cls).parse_callable_signature(base_cls)
|
|
622
|
+
merged_args_dict.update(args_dict)
|
|
623
|
+
merged_ann_types.update(ann_types)
|
|
624
|
+
|
|
625
|
+
return merged_args_dict, merged_ann_types
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from sinapsis_core.data_containers.data_packet import DataContainer
|
|
3
|
+
from sinapsis_core.template_base.transfer_data_container import TransferDataContainer
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class EmptyDataContainer(TransferDataContainer):
|
|
7
|
+
"""
|
|
8
|
+
Utility template used to generate empty DataContainers
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
def execute(self, container: DataContainer) -> DataContainer:
|
|
12
|
+
"""
|
|
13
|
+
Ignores received DataContainer and returns an empty one
|
|
14
|
+
|
|
15
|
+
Args:
|
|
16
|
+
container (DataContainer): Received DataContainer
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
DataContainer: Empty DataContainer
|
|
20
|
+
"""
|
|
21
|
+
_ = self
|
|
22
|
+
_ = container
|
|
23
|
+
return DataContainer()
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
from sinapsis_core.data_containers.data_packet import DataContainer
|
|
3
|
+
from sinapsis_core.template_base.base_models import TemplateAttributes
|
|
4
|
+
from sinapsis_core.template_base.copy_data_container import CopyDataContainer
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class MergeDataFlow(CopyDataContainer):
|
|
8
|
+
"""
|
|
9
|
+
Utility used to define DataContainer flow among
|
|
10
|
+
templates and agents.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
def execute(self, container: list[DataContainer]) -> DataContainer:
|
|
14
|
+
_ = self
|
|
15
|
+
if not isinstance(container, list) or not container:
|
|
16
|
+
raise ValueError(f"Expected container to be a list, but got {type(container)}")
|
|
17
|
+
container_base = container.pop()
|
|
18
|
+
for container_item in container:
|
|
19
|
+
container_base += container_item
|
|
20
|
+
return container_base
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class MPIMergeDataFlow(MergeDataFlow):
|
|
24
|
+
"""
|
|
25
|
+
This template is not exactly used as a normal template. It gets replaced
|
|
26
|
+
by MPI routines. Its main purpose is to avoid user mistakes when defining
|
|
27
|
+
MPI workflows by being explicit about data flow and how data containers are
|
|
28
|
+
to be received from workers and merged.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
class AttributesBaseModel(TemplateAttributes):
|
|
32
|
+
recv_container_from_parallel_workers: bool = True
|
|
33
|
+
stop_receiving_from_workers_upon_empty_container: bool = False
|