From 24b8cb799a3f1ce475fb20bf4de99bb8cb1ad5d1 Mon Sep 17 00:00:00 2001 From: Qubasa Date: Fri, 4 Jul 2025 13:17:01 +0700 Subject: [PATCH] clan-cli: Print function name to raise for unsupported types by API.register --- pkgs/clan-cli/api.py | 13 +++++++++++-- pkgs/clan-cli/clan_lib/api/__init__.py | 18 ++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/pkgs/clan-cli/api.py b/pkgs/clan-cli/api.py index 94f36b6bd..76fe2a18e 100755 --- a/pkgs/clan-cli/api.py +++ b/pkgs/clan-cli/api.py @@ -23,12 +23,21 @@ def import_all_modules_from_package(pkg: ModuleType) -> None: importlib.import_module(module_name) -def main() -> None: +def load_in_all_api_functions() -> None: + """ + For the global API object, to have all functions available. + We have to make sure python loads every wrapped function at least once. + This is done by importing all modules from the clan_lib and clan_cli packages. + """ import clan_cli import clan_lib - import_all_modules_from_package(clan_cli) import_all_modules_from_package(clan_lib) + import_all_modules_from_package(clan_cli) + + +def main() -> None: + load_in_all_api_functions() from clan_lib.api import API diff --git a/pkgs/clan-cli/clan_lib/api/__init__.py b/pkgs/clan-cli/clan_lib/api/__init__.py index ed67fe6f2..da4c475d4 100644 --- a/pkgs/clan-cli/clan_lib/api/__init__.py +++ b/pkgs/clan-cli/clan_lib/api/__init__.py @@ -12,6 +12,8 @@ from typing import ( get_type_hints, ) +from clan_lib.api.util import JSchemaTypeError + log = logging.getLogger(__name__) from .serde import dataclass_to_dict, from_dict, sanitize_string @@ -217,12 +219,16 @@ API.register(open_file) for name, func in self._registry.items(): hints = get_type_hints(func) - serialized_hints = { - key: type_to_dict( - value, scope=name + " argument" if key != "return" else "return" - ) - for key, value in hints.items() - } + try: + serialized_hints = { + key: type_to_dict( + value, scope=name + " argument" if key != "return" else "return" + ) + for key, value in hints.items() + } + except JSchemaTypeError as e: + msg = f"Error serializing type hints for function '{name}': {e}" + raise JSchemaTypeError(msg) from e return_type = serialized_hints.pop("return")