Source code for ska_oso_pdm._shared.python_arguments

from typing import Any

from pydantic import Field, field_serializer

from .pdm_object import PdmObject


def _make_json_safe(value: Any) -> Any:
    """Recursively convert values to JSON-serializable types.

    Unknown types (e.g. astropy.time.Time, numpy arrays) are converted to
    their repr() string so that arbitrary Python objects can round-trip through
    JSON without raising a PydanticSerializationError.
    """
    if isinstance(value, (str, int, float, bool, type(None))):
        return value
    if isinstance(value, dict):
        return {k: _make_json_safe(v) for k, v in value.items()}
    if isinstance(value, (list, tuple)):
        return [_make_json_safe(v) for v in value]
    return repr(value)


[docs] class PythonArguments(PdmObject): """Represents the arguments for a Python callable.""" args: list = Field(default_factory=list) kwargs: dict = Field(default_factory=dict) @field_serializer("args") def _serialize_args(self, value: list) -> list: return [_make_json_safe(v) for v in value] @field_serializer("kwargs") def _serialize_kwargs(self, value: dict) -> dict: return {k: _make_json_safe(v) for k, v in value.items()}
# Do we need this? FunctionArgs seems redundant. ~bjmc 2024-02-20
[docs] class FunctionArgs(PdmObject): """Represents a Python function & its arguments""" function_name: str | None = None function_args: PythonArguments | None = None