Skip to content

Commit 6ec44c3

Browse files
committed
fix: tests for python 3.9
1 parent 37a21b2 commit 6ec44c3

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
lines changed

workflowai/core/utils/_tools.py

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import contextlib
2-
import datetime
32
import inspect
4-
from enum import Enum
5-
from typing import Any, Callable, NamedTuple, Optional, cast, get_type_hints
3+
from typing import Any, Callable, NamedTuple, Optional, get_type_hints
64

7-
from pydantic import BaseModel, TypeAdapter
5+
from pydantic import TypeAdapter
86

97
from workflowai.core.utils._schema_generator import JsonSchemaGenerator
108

@@ -17,14 +15,6 @@ class SchemaDeserializer(NamedTuple):
1715
deserializer: Optional[Callable[[Any], Any]] = None
1816

1917

20-
def _serialize_datetime(x: datetime.datetime) -> str:
21-
return x.isoformat()
22-
23-
24-
def _deserialize_datetime(x: str) -> datetime.datetime:
25-
return datetime.datetime.fromisoformat(x)
26-
27-
2818
def _get_type_schema(param_type: type):
2919
"""Convert a Python type to its corresponding JSON schema type.
3020
@@ -47,31 +37,11 @@ def _get_type_schema(param_type: type):
4737
if param_type is bool:
4838
return SchemaDeserializer({"type": "boolean"})
4939

50-
if param_type is datetime.datetime:
51-
return SchemaDeserializer(
52-
{"type": "string", "format": "date-time"},
53-
serializer=_serialize_datetime,
54-
deserializer=_deserialize_datetime,
55-
)
56-
57-
if inspect.isclass(param_type):
58-
if issubclass(param_type, BaseModel):
59-
return SchemaDeserializer(
60-
schema=param_type.model_json_schema(by_alias=True, schema_generator=JsonSchemaGenerator),
61-
serializer=lambda x: cast(BaseModel, x).model_dump(mode="json"), # pyright: ignore [reportUnknownLambdaType]
62-
deserializer=param_type.model_validate,
63-
)
64-
65-
if issubclass(param_type, Enum):
66-
if not issubclass(param_type, str):
67-
raise ValueError(f"Non string enums are not supported: {param_type}")
68-
return SchemaDeserializer({"type": "string", "enum": [e.value for e in param_type]})
69-
7040
# Attempting to build a type adapter with pydantic
7141
with contextlib.suppress(Exception):
7242
adapter = TypeAdapter[Any](param_type)
7343
return SchemaDeserializer(
74-
schema=adapter.json_schema(),
44+
schema=adapter.json_schema(schema_generator=JsonSchemaGenerator),
7545
deserializer=adapter.validate_python, # pyright: ignore [reportUnknownLambdaType]
7646
serializer=lambda x: adapter.dump_python(x, mode="json"), # pyright: ignore [reportUnknownLambdaType]
7747
)

workflowai/core/utils/_tools_test.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111

1212

1313
class TestGetTypeSchema:
14+
class _BasicEnum(str, Enum):
15+
A = "a"
16+
B = "b"
17+
18+
class _BasicModel(BaseModel):
19+
a: int
20+
b: str
21+
1422
@pytest.mark.parametrize(
1523
("param_type", "value"),
1624
[
@@ -22,6 +30,10 @@ class TestGetTypeSchema:
2230
(ZoneInfo, ZoneInfo("UTC")),
2331
(list[int], [1, 2, 3]),
2432
(dict[str, int], {"a": 1, "b": 2}),
33+
(_BasicEnum, _BasicEnum.A),
34+
(_BasicModel, _BasicModel(a=1, b="test")),
35+
(list[_BasicModel], [_BasicModel(a=1, b="test"), _BasicModel(a=2, b="test2")]),
36+
(tuple[int, str], (1, "test")),
2537
],
2638
)
2739
def test_get_type_schema(self, param_type: Any, value: Any):
@@ -50,6 +62,7 @@ def sample_func(
5062
age: int,
5163
height: float,
5264
is_active: bool,
65+
date: datetime,
5366
mode: TestMode = TestMode.FAST,
5467
) -> bool:
5568
"""Sample function for testing"""
@@ -77,8 +90,12 @@ def sample_func(
7790
"type": "string",
7891
"enum": ["fast", "slow"],
7992
},
93+
"date": {
94+
"type": "string",
95+
"format": "date-time",
96+
},
8097
},
81-
"required": ["name", "age", "height", "is_active"], # 'mode' is not required
98+
"required": ["name", "age", "height", "is_active", "date"], # 'mode' is not required
8299
}
83100
assert output_schema.schema == {
84101
"type": "boolean",

0 commit comments

Comments
 (0)