|
1 | | -""" |
2 | | -CSP timeseries input handlers for numba_node. |
3 | | -
|
4 | | -Provides: |
5 | | -- TsInputHandler for ts[type] parameters |
6 | | -- ListBasketInputHandler for [ts[type]] list basket parameters |
7 | | -- DictBasketInputHandler for {key: ts[type]} dict basket parameters |
8 | | -""" |
| 1 | +"""Pass CSP-parsed timeseries input metadata to numba_cfunc_compiler.""" |
9 | 2 |
|
10 | 3 | import inspect |
11 | 4 | from dataclasses import dataclass |
12 | | -from typing import Any, Optional, get_args, get_origin |
| 5 | +from typing import Any, Optional |
13 | 6 |
|
14 | 7 | from numba_cfunc_compiler.function_analyzer import InputTypeHandler |
15 | 8 | from numba_cfunc_compiler.models import ParameterInfo |
16 | 9 |
|
17 | | -from csp.impl.types.tstype import TsType |
18 | | -from csp.impl.wiring.edge import Edge |
19 | | - |
20 | | - |
21 | | -def _extract_ts_inner_type(ann: Any) -> Optional[type]: |
22 | | - if get_origin(ann) is not TsType: |
23 | | - return None |
24 | | - args = get_args(ann) |
25 | | - return args[0] if args else getattr(ann, "typ", None) |
26 | | - |
27 | | - |
28 | | -def _parse_list_basket_annotation(ann: Any) -> tuple[bool, Optional[type]]: |
29 | | - # Legacy syntax: [ts[T]] |
30 | | - if isinstance(ann, list): |
31 | | - if len(ann) != 1: |
32 | | - return True, None |
33 | | - return True, _extract_ts_inner_type(ann[0]) |
34 | | - |
35 | | - # Modern syntax: list[ts[T]] / typing.List[ts[T]] |
36 | | - if get_origin(ann) is list: |
37 | | - args = get_args(ann) |
38 | | - if len(args) != 1: |
39 | | - return True, None |
40 | | - return True, _extract_ts_inner_type(args[0]) |
41 | | - |
42 | | - return False, None |
43 | | - |
44 | 10 |
|
45 | 11 | @dataclass(frozen=True) |
46 | | -class _DictBasketExpectedType: |
47 | | - key_type: type |
48 | | - element_type: type |
49 | | - |
| 12 | +class CspInputMetadata: |
| 13 | + category: str |
50 | 14 |
|
51 | | -def _parse_dict_basket_annotation(ann: Any) -> tuple[bool, Optional[type], Optional[type]]: |
52 | | - key_type = None |
53 | | - value_ann = None |
54 | | - |
55 | | - # Legacy syntax: {str: ts[T]} / {int: ts[T]} |
56 | | - if isinstance(ann, dict): |
57 | | - if len(ann) != 1: |
58 | | - return True, None, None |
59 | | - key_type, value_ann = next(iter(ann.items())) |
60 | | - # Modern syntax: dict[str, ts[T]] / typing.Dict[str, ts[T]] |
61 | | - elif get_origin(ann) is dict: |
62 | | - args = get_args(ann) |
63 | | - if len(args) != 2: |
64 | | - return True, None, None |
65 | | - key_type, value_ann = args |
66 | | - else: |
67 | | - return False, None, None |
68 | | - |
69 | | - if key_type not in (str, int): |
70 | | - return False, None, None |
71 | | - |
72 | | - return True, key_type, _extract_ts_inner_type(value_ann) |
73 | | - |
74 | | - |
75 | | -def _validate_edge_type(param_name: str, edge: Edge, expected_type: type) -> Edge: |
76 | | - actual_type = edge.tstype.typ |
77 | | - if actual_type is not expected_type and not (expected_type is float and actual_type is int): |
78 | | - raise TypeError( |
79 | | - f"Argument '{param_name}' expected ts[{expected_type.__name__}], got ts[{actual_type.__name__}]" |
80 | | - ) |
81 | | - return edge |
82 | | - |
83 | | - |
84 | | -class TsInputHandler(InputTypeHandler): |
85 | | - """Handles ts[type] input annotations.""" |
86 | 15 |
|
| 16 | +class CspInputHandler(InputTypeHandler): |
87 | 17 | def try_parse(self, param: inspect.Parameter, ann: Any) -> Optional[ParameterInfo]: |
88 | | - inner_type = _extract_ts_inner_type(ann) |
89 | | - if inner_type is None: |
| 18 | + if not isinstance(ann, CspInputMetadata): |
90 | 19 | return None |
91 | | - |
92 | | - return ParameterInfo(expected_type=inner_type, category="signal") |
| 20 | + return ParameterInfo(expected_type=ann, category=ann.category) |
93 | 21 |
|
94 | 22 | def validate_value(self, param_name: str, value: Any, expected_type: Any) -> Any: |
95 | | - if not isinstance(value, Edge): |
96 | | - raise TypeError(f"Argument '{param_name}' must be an Edge, got {type(value).__name__}") |
97 | | - return _validate_edge_type(param_name, value, expected_type) |
98 | | - |
99 | | - |
100 | | -class ListBasketInputHandler(InputTypeHandler): |
101 | | - """Handles [ts[type]] and list[ts[type]] basket input annotations.""" |
102 | | - |
103 | | - def try_parse(self, param: inspect.Parameter, ann: Any) -> Optional[ParameterInfo]: |
104 | | - matched, inner_type = _parse_list_basket_annotation(ann) |
105 | | - if not matched: |
106 | | - return None |
107 | | - if inner_type is None: |
108 | | - raise TypeError(f"List basket '{param.name}' element ts[type] is missing type argument") |
109 | | - |
110 | | - return ParameterInfo(expected_type=inner_type, category="signal_set") |
111 | | - |
112 | | - def validate_value(self, param_name: str, value: Any, expected_type: Any) -> Any: |
113 | | - if not isinstance(value, (list, tuple)): |
114 | | - raise TypeError(f"Argument '{param_name}' must be a list, got {type(value).__name__}") |
115 | | - if not value: |
116 | | - raise ValueError(f"List basket '{param_name}' must not be empty") |
117 | | - |
118 | | - result = {} |
119 | | - for i, edge in enumerate(value): |
120 | | - if not isinstance(edge, Edge): |
121 | | - raise TypeError(f"List basket '{param_name}[{i}]' must be an Edge, got {type(edge).__name__}") |
122 | | - result[i] = _validate_edge_type(f"{param_name}[{i}]", edge, expected_type) |
123 | | - return result |
124 | | - |
125 | | - |
126 | | -class DictBasketInputHandler(InputTypeHandler): |
127 | | - """Handles {key_type: ts[type]} and dict[key_type, ts[type]] basket annotations.""" |
128 | | - |
129 | | - def try_parse(self, param: inspect.Parameter, ann: Any) -> Optional[ParameterInfo]: |
130 | | - matched, key_type, inner_type = _parse_dict_basket_annotation(ann) |
131 | | - if not matched: |
132 | | - return None |
133 | | - if inner_type is None: |
134 | | - raise TypeError(f"Dict basket '{param.name}' element ts[type] is missing type argument") |
135 | | - |
136 | | - return ParameterInfo( |
137 | | - expected_type=_DictBasketExpectedType(key_type=key_type, element_type=inner_type), |
138 | | - category="signal_set", |
139 | | - ) |
140 | | - |
141 | | - def validate_value(self, param_name: str, value: Any, expected_type: Any) -> Any: |
142 | | - if not isinstance(value, dict): |
143 | | - raise TypeError(f"Argument '{param_name}' must be a dict, got {type(value).__name__}") |
144 | | - if not value: |
145 | | - raise ValueError(f"Dict basket '{param_name}' must not be empty") |
146 | | - |
147 | | - result = {} |
148 | | - for key, edge in value.items(): |
149 | | - if not isinstance(key, expected_type.key_type): |
150 | | - raise TypeError( |
151 | | - f"Dict basket '{param_name}' key {key!r} must be " |
152 | | - f"{expected_type.key_type.__name__}, got {type(key).__name__}" |
153 | | - ) |
154 | | - if not isinstance(edge, Edge): |
155 | | - raise TypeError(f"Dict basket '{param_name}[{key!r}]' must be an Edge, got {type(edge).__name__}") |
156 | | - result[key] = _validate_edge_type(f"{param_name}[{key!r}]", edge, expected_type.element_type) |
157 | | - return result |
| 23 | + return value |
158 | 24 |
|
159 | 25 |
|
160 | 26 | def register(): |
161 | 27 | from numba_cfunc_compiler.function_analyzer import FunctionAnalyzer |
162 | 28 |
|
163 | | - FunctionAnalyzer.register_input_handler(TsInputHandler()) |
164 | | - FunctionAnalyzer.register_input_handler(ListBasketInputHandler()) |
165 | | - FunctionAnalyzer.register_input_handler(DictBasketInputHandler()) |
| 29 | + FunctionAnalyzer.register_input_handler(CspInputHandler()) |
0 commit comments