From f1747079c85989cbcb7082a7a129519e31989b77 Mon Sep 17 00:00:00 2001 From: Qubasa Date: Tue, 14 Oct 2025 11:30:48 +0200 Subject: [PATCH 1/2] clan_cli: Add typeAlias support for api.py --- pkgs/clan-cli/clan_lib/api/type_to_jsonschema.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pkgs/clan-cli/clan_lib/api/type_to_jsonschema.py b/pkgs/clan-cli/clan_lib/api/type_to_jsonschema.py index b4dda26a1..04bd49de6 100644 --- a/pkgs/clan-cli/clan_lib/api/type_to_jsonschema.py +++ b/pkgs/clan-cli/clan_lib/api/type_to_jsonschema.py @@ -13,6 +13,7 @@ from typing import ( NewType, NotRequired, Required, + TypeAliasType, TypeVar, Union, get_args, @@ -239,6 +240,12 @@ def type_to_dict( origtype = t.__supertype__ return type_to_dict(origtype, scope, type_map) + if isinstance(t, TypeAliasType): + # Handle PEP 695 type aliases (type X = Y syntax) + return type_to_dict( + t.__value__, scope, type_map, narrow_unsupported_union_types + ) + if hasattr(t, "__origin__"): # Check if it's a generic type origin = get_origin(t) args = get_args(t) From 23008d1f7330f3b35d08bc48bc88db031a3f3823 Mon Sep 17 00:00:00 2001 From: Qubasa Date: Tue, 14 Oct 2025 12:06:04 +0200 Subject: [PATCH 2/2] openapi: Add a test for TypeAliasing --- .../clan_lib/api/type_to_jsonschema_test.py | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/pkgs/clan-cli/clan_lib/api/type_to_jsonschema_test.py b/pkgs/clan-cli/clan_lib/api/type_to_jsonschema_test.py index 511dee4fa..0ac57a616 100644 --- a/pkgs/clan-cli/clan_lib/api/type_to_jsonschema_test.py +++ b/pkgs/clan-cli/clan_lib/api/type_to_jsonschema_test.py @@ -320,3 +320,62 @@ def test_open_typed_dict() -> None: "additionalProperties": False, "required": ["name"], } + + +def test_type_alias() -> None: + # Test simple type alias + type UserId = int + + assert type_to_dict(UserId) == { + "type": "integer", + } + + # Test union type alias + type InputName = str | None + + assert type_to_dict(InputName) == { + "oneOf": [ + {"type": "string"}, + {"type": "null"}, + ], + } + + # Test complex type alias with list + type ServiceName = str + type ServiceNames = list[ServiceName] + + assert type_to_dict(ServiceNames) == { + "type": "array", + "items": {"type": "string"}, + } + + # Test type alias in a dataclass field + type Readme = str | None + + @dataclass + class ServiceReadmeCollection: + input_name: str | None + readmes: dict[str, Readme] + + assert type_to_dict(ServiceReadmeCollection) == { + "type": "object", + "properties": { + "input_name": { + "oneOf": [ + {"type": "string"}, + {"type": "null"}, + ], + }, + "readmes": { + "type": "object", + "additionalProperties": { + "oneOf": [ + {"type": "string"}, + {"type": "null"}, + ], + }, + }, + }, + "additionalProperties": False, + "required": ["input_name", "readmes"], + }