MCPcopy Create free account
hub / github.com/anomalyco/opencode-sdk-python / _transform_recursive

Function _transform_recursive

src/opencode_ai/_utils/_transform.py:152–227  ·  view source on GitHub ↗

Transform the given data against the expected type. Args: annotation: The direct type annotation given to the particular piece of data. This may or may not be wrapped in metadata types, e.g. `Required[T]`, `Annotated[T, ...]` etc inner_type: If applicable, this is t

(
    data: object,
    *,
    annotation: type,
    inner_type: type | None = None,
)

Source from the content-addressed store, hash-verified

150
151
152def _transform_recursive(
153 data: object,
154 *,
155 annotation: type,
156 inner_type: type | None = None,
157) -> object:
158 """Transform the given data against the expected type.
159
160 Args:
161 annotation: The direct type annotation given to the particular piece of data.
162 This may or may not be wrapped in metadata types, e.g. `Required[T]`, `Annotated[T, ...]` etc
163
164 inner_type: If applicable, this is the "inside" type. This is useful in certain cases where the outside type
165 is a container type such as `List[T]`. In that case `inner_type` should be set to `T` so that each entry in
166 the list can be transformed using the metadata from the container type.
167
168 Defaults to the same value as the `annotation` argument.
169 """
170 if inner_type is None:
171 inner_type = annotation
172
173 stripped_type = strip_annotated_type(inner_type)
174 origin = get_origin(stripped_type) or stripped_type
175 if is_typeddict(stripped_type) and is_mapping(data):
176 return _transform_typeddict(data, stripped_type)
177
178 if origin == dict and is_mapping(data):
179 items_type = get_args(stripped_type)[1]
180 return {key: _transform_recursive(value, annotation=items_type) for key, value in data.items()}
181
182 if (
183 # List[T]
184 (is_list_type(stripped_type) and is_list(data))
185 # Iterable[T]
186 or (is_iterable_type(stripped_type) and is_iterable(data) and not isinstance(data, str))
187 ):
188 # dicts are technically iterable, but it is an iterable on the keys of the dict and is not usually
189 # intended as an iterable, so we don't transform it.
190 if isinstance(data, dict):
191 return cast(object, data)
192
193 inner_type = extract_type_arg(stripped_type, 0)
194 if _no_transform_needed(inner_type):
195 # for some types there is no need to transform anything, so we can get a small
196 # perf boost from skipping that work.
197 #
198 # but we still need to convert to a list to ensure the data is json-serializable
199 if is_list(data):
200 return data
201 return list(data)
202
203 return [_transform_recursive(d, annotation=annotation, inner_type=inner_type) for d in data]
204
205 if is_union_type(stripped_type):
206 # For union types we run the transformation against all subtypes to ensure that everything is transformed.
207 #
208 # TODO: there may be edge cases where the same normalized field name will transform to two different names
209 # in different subtypes.

Callers 3

transformFunction · 0.85
_transform_typeddictFunction · 0.85

Calls 15

strip_annotated_typeFunction · 0.85
get_originFunction · 0.85
is_typeddictFunction · 0.85
is_mappingFunction · 0.85
_transform_typeddictFunction · 0.85
get_argsFunction · 0.85
is_list_typeFunction · 0.85
is_listFunction · 0.85
is_iterable_typeFunction · 0.85
is_iterableFunction · 0.85
extract_type_argFunction · 0.85
_no_transform_neededFunction · 0.85

Tested by

no test coverage detected