Skip to content

Commit

Permalink
[chore] add __future__ annotations for 3.8 and 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
synacktraa committed Sep 5, 2024
1 parent 8de2a53 commit 4ed6ad0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
19 changes: 5 additions & 14 deletions tool_parse/_registry.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import json
import typing as t
from pathlib import Path
Expand Down Expand Up @@ -57,13 +59,7 @@ def __register(

self.__entries[key] = ts.Entry(name=key, description=description, obj=obj)

def __setitem__(
self,
key: str,
value: t.Callable[P, R]
| t.Callable[P, t.Awaitable[R]]
| type[ts.PydanticModel | ts.TypedDict | ts.NamedTuple],
) -> None:
def __setitem__(self, key: str, value: type) -> None:
self.__register(obj=value, name=key)

def __getitem__(self, key: str) -> ts.ToolSchema:
Expand Down Expand Up @@ -144,12 +140,7 @@ def decorator(obj: type[t.Any]) -> type[t.Any]:
else:
return decorator(__obj)

def register_multiple(
self,
*__objs: t.Callable[P, R]
| t.Callable[P, t.Awaitable[R]]
| type[ts.PydanticModel | ts.TypedDict | ts.NamedTuple],
):
def register_multiple(self, *__objs: type):
"""
Register multiple objects at once. Overriding name and description is not available when using this method.
Expand Down Expand Up @@ -177,7 +168,7 @@ def marshal(
*,
as_json: bool = False,
persist_at: t.Optional[str | Path] = None,
) -> list[ts.ToolSchema] | str | None:
) -> t.Optional[list[ts.ToolSchema] | str]:
"""
Transform registered tools to schema format.
Expand Down
21 changes: 17 additions & 4 deletions tool_parse/_types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import inspect
import sys
import types as pytypes
Expand Down Expand Up @@ -36,7 +38,7 @@
"""Supported types mapped property types"""


def get_type_repr(_t: type | None) -> str:
def get_type_repr(_t: t.Optional[type]) -> str:
if _t is None:
return "`pydantic.BaseModel`"
if _t.__module__ == "builtins":
Expand All @@ -61,10 +63,21 @@ class Annotation(t.NamedTuple):
is_optional: bool


if sys.version_info >= (3, 9):

def eval_ref(ref: t.ForwardRef) -> type:
ns = getattr(ref, "__globals__", None)
return ref._evaluate(ns, ns, recursive_guard=frozenset()) # type: ignore[return-value]
else:

def eval_ref(ref: t.ForwardRef) -> type:
ns = getattr(ref, "__globals__", None)
return ref._evaluate(ns, ns)


def resolve_annotation(__annot: type | t.ForwardRef) -> Annotation:
if isinstance(__annot, t.ForwardRef):
ns = getattr(__annot, "__globals__", None)
__annot = __annot._evaluate(ns, ns, frozenset()) # type: ignore[assignment]
__annot = eval_ref(__annot)

is_optional = False
_type, args = t.get_origin(__annot) or __annot, list(t.get_args(__annot))
Expand All @@ -76,7 +89,7 @@ def resolve_annotation(__annot: type | t.ForwardRef) -> Annotation:
is_optional = True
_type, args, _ = resolve_annotation(args[1 if args.index(type(None)) == 0 else 0])

return Annotation(type=_type, args=args, is_optional=is_optional) # type: ignore[arg-type]
return Annotation(type=_type, args=args, is_optional=is_optional)


if sys.version_info >= (3, 10):
Expand Down
8 changes: 5 additions & 3 deletions tool_parse/compile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import ast
import asyncio
import inspect
Expand Down Expand Up @@ -141,8 +143,8 @@ def compile_namedtuple_object(__nt: t.Type[NamedTuple], arguments: dict[str, t.A


def compile_value( # noqa: C901
__annotation: t.Type | t.ForwardRef, raw_value: t.Any | None
) -> tuple[t.Any | None, bool]:
__annotation: type | t.ForwardRef, raw_value: t.Optional[t.Any]
) -> tuple[t.Optional[t.Any], bool]:
"""
Compile the raw value as instance of the given annotation.
"""
Expand Down Expand Up @@ -225,7 +227,7 @@ def compile_value( # noqa: C901
)


def compile_object(__obj: t.Any, *, arguments: str | dict[str, t.Any]):
def compile_object(__obj: t.Any, *, arguments: t.Optional[str | dict[str, t.Any]]):
if isinstance(arguments, str):
try:
arguments: dict[str, t.Any] = json.loads(arguments) # type: ignore[no-redef]
Expand Down
4 changes: 3 additions & 1 deletion tool_parse/marshal.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import inspect
import typing as t
from enum import Enum
Expand Down Expand Up @@ -109,7 +111,7 @@ def generate_namedtuple_metadata(__nt: t.Type[NamedTuple], description_map: dict
)


def marshal_annotation(__annotation: t.Type | t.ForwardRef) -> tuple[dict[str, t.Any], bool]: # noqa: C901
def marshal_annotation(__annotation: type | t.ForwardRef) -> tuple[dict[str, t.Any], bool]: # noqa: C901
"""
Marshal the annotation to tool-calling specific property map
"""
Expand Down

0 comments on commit 4ed6ad0

Please sign in to comment.