Skip to content

Commit 6e18006

Browse files
committed
chore: Rollback types for backward compatability
1 parent 30f6591 commit 6e18006

File tree

2 files changed

+36
-40
lines changed

2 files changed

+36
-40
lines changed

function_schema/core.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22
import inspect
33
import platform
44
import packaging.version
5-
from typing import Annotated, Optional, Union, Callable, Literal, Any, get_args, get_origin
6-
from .types import OpenAIFunctionSchema, ClaudeFunctionSchema
5+
from typing import (
6+
Annotated,
7+
Optional,
8+
Union,
9+
Callable,
10+
Literal,
11+
Any,
12+
get_args,
13+
get_origin,
14+
)
15+
from .types import FunctionSchema
716

817
current_version = packaging.version.parse(platform.python_version())
918
py_310 = packaging.version.parse("3.10")
@@ -19,28 +28,35 @@
1928
try:
2029
from typing_extensions import Doc
2130
except ImportError:
31+
2232
class Doc:
2333
def __init__(self, documentation: str, /):
2434
self.documentation = documentation
2535

36+
2637
__all__ = ("get_function_schema", "guess_type", "Doc", "Annotated")
2738

2839

29-
def is_doc_meta(obj: Annotated[Any, Doc("The object to be checked.")]) -> Annotated[
30-
bool, Doc("True if the object is a documentation object, False otherwise.")]:
40+
def is_doc_meta(
41+
obj: Annotated[Any, Doc("The object to be checked.")],
42+
) -> Annotated[
43+
bool, Doc("True if the object is a documentation object, False otherwise.")
44+
]:
3145
"""
3246
Check if the given object is a documentation object.
3347
3448
Example:
3549
>>> is_doc_meta(Doc("This is a documentation object"))
3650
True
3751
"""
38-
return getattr(obj, '__class__') == Doc and hasattr(obj, 'documentation')
52+
return getattr(obj, "__class__") == Doc and hasattr(obj, "documentation")
3953

4054

41-
def unwrap_doc(obj: Annotated[Union[Doc, str],
42-
Doc("The object to get the documentation string from.")]) -> Annotated[
43-
str, Doc("The documentation string.")]:
55+
def unwrap_doc(
56+
obj: Annotated[
57+
Union[Doc, str], Doc("The object to get the documentation string from.")
58+
],
59+
) -> Annotated[str, Doc("The documentation string.")]:
4460
"""
4561
Get the documentation string from the given object.
4662
@@ -61,8 +77,7 @@ def get_function_schema(
6177
Optional[Literal["openai", "claude"]],
6278
Doc("The format of the schema to return"),
6379
] = "openai",
64-
) -> Annotated[Union[OpenAIFunctionSchema, ClaudeFunctionSchema],
65-
Doc("The JSON schema for the given function")]:
80+
) -> Annotated[FunctionSchema, Doc("The JSON schema for the given function")]:
6681
"""
6782
Returns a JSON schema for the given function.
6883
@@ -127,13 +142,13 @@ def get_function_schema(
127142
# find description in param_args tuple
128143
try:
129144
description = next(
130-
unwrap_doc(arg)
131-
for arg in param_args if isinstance(arg, Doc)
145+
unwrap_doc(arg) for arg in param_args if isinstance(arg, Doc)
132146
)
133147
except StopIteration:
134148
try:
135149
description = next(
136-
arg for arg in param_args if isinstance(arg, str))
150+
arg for arg in param_args if isinstance(arg, str)
151+
)
137152
except StopIteration:
138153
description = "The {name} parameter"
139154

@@ -163,8 +178,7 @@ def get_function_schema(
163178
}
164179

165180
if enum_ is not None:
166-
schema["properties"][name]["enum"] = [
167-
t for t in enum_ if t is not None]
181+
schema["properties"][name]["enum"] = [t for t in enum_ if t is not None]
168182

169183
if default_value is not inspect._empty:
170184
schema["properties"][name]["default"] = default_value
@@ -194,8 +208,7 @@ def get_function_schema(
194208
def guess_type(
195209
T: Annotated[type, Doc("The type to guess the JSON schema type for")],
196210
) -> Annotated[
197-
Union[str, list[str]], Doc(
198-
"str | list of str that representing JSON schema type")
211+
Union[str, list[str]], Doc("str | list of str that representing JSON schema type")
199212
]:
200213
"""Guesses the JSON schema type for the given python type."""
201214

function_schema/types.py

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TypedDict, Literal, TypeVar, Union, Generic
1+
from typing import TypedDict, Literal
22

33
try:
44
from typing import NotRequired
@@ -37,23 +37,7 @@ class RootProperty(TypedDict):
3737
properties: dict[str, ParamSchema]
3838

3939

40-
T = TypeVar("T", bound=Union["WithParameters", "WithInputSchema"])
41-
42-
43-
class WithParameters(TypedDict):
44-
parameters: RootProperty
45-
46-
47-
class WithInputSchema(TypedDict):
48-
input_schema: RootProperty
49-
50-
51-
class FunctionSchemaBase(TypedDict):
52-
name: str
53-
description: str
54-
55-
56-
class FunctionSchema(FunctionSchemaBase, Generic[T]):
40+
class FunctionSchema(TypedDict):
5741
"""
5842
Represents the schema of a function.
5943
Attributes:
@@ -63,8 +47,7 @@ class FunctionSchema(FunctionSchemaBase, Generic[T]):
6347
input_schema (ParamSchema): The schema for the function parameters if format is "claude".
6448
"""
6549

66-
pass
67-
68-
69-
OpenAIFunctionSchema = FunctionSchema[WithParameters]
70-
ClaudeFunctionSchema = FunctionSchema[WithInputSchema]
50+
name: str
51+
description: str
52+
parameters: NotRequired[RootProperty]
53+
input_schema: NotRequired[RootProperty]

0 commit comments

Comments
 (0)