Merge pull request 'clan_cli: Add typeAlias support for api.py' (#5509) from Qubasa/clan-core:fix_jsonschema_gen into main
Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/5509
This commit is contained in:
@@ -13,6 +13,7 @@ from typing import (
|
|||||||
NewType,
|
NewType,
|
||||||
NotRequired,
|
NotRequired,
|
||||||
Required,
|
Required,
|
||||||
|
TypeAliasType,
|
||||||
TypeVar,
|
TypeVar,
|
||||||
Union,
|
Union,
|
||||||
get_args,
|
get_args,
|
||||||
@@ -239,6 +240,12 @@ def type_to_dict(
|
|||||||
origtype = t.__supertype__
|
origtype = t.__supertype__
|
||||||
return type_to_dict(origtype, scope, type_map)
|
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
|
if hasattr(t, "__origin__"): # Check if it's a generic type
|
||||||
origin = get_origin(t)
|
origin = get_origin(t)
|
||||||
args = get_args(t)
|
args = get_args(t)
|
||||||
|
|||||||
@@ -320,3 +320,62 @@ def test_open_typed_dict() -> None:
|
|||||||
"additionalProperties": False,
|
"additionalProperties": False,
|
||||||
"required": ["name"],
|
"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"],
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user