Fix typing

This commit is contained in:
Andras Schmelczer 2022-06-25 15:32:20 +02:00
parent 2b39482e28
commit f40c60a82e
No known key found for this signature in database
GPG key ID: 0EA1BC97D0AB076E
2 changed files with 36 additions and 8 deletions

View file

@ -9,8 +9,8 @@ from typing import (
Optional,
Type,
TypeVar,
Union,
cast,
overload,
)
from fastapi import APIRouter, FastAPI, status
@ -45,8 +45,12 @@ class GreatAI(Generic[T]):
self._func = func
def func_in_tracing_context(*args: Any, **kwargs: Any) -> Trace[T]:
with TracingContext[T](func.__name__) as t:
def func_in_tracing_context(
*args: Any, do_not_persist_traces: bool = False, **kwargs: Any
) -> Trace[T]:
with TracingContext[T](
func.__name__, do_not_persist_traces=do_not_persist_traces
) as t:
result = func(*args, **kwargs)
output = t.finalise(output=result)
return output
@ -68,15 +72,32 @@ class GreatAI(Generic[T]):
redoc_url=None,
)
@overload
@staticmethod
def deploy(
def create(
func: Optional[Callable[..., T]] = None,
) -> "GreatAI[T]":
...
@overload
@staticmethod
def create(
version: str = "0.0.1",
disable_rest_api: bool = False,
disable_docs: bool = False,
disable_dashboard: bool = False,
) -> Callable[[Callable[..., T]], "GreatAI[T]"]:
...
@staticmethod
def create(
func: Optional[Callable[..., T]] = None,
*,
version: str = "0.0.1",
disable_rest_api: bool = False,
disable_docs: bool = False,
disable_dashboard: bool = False,
) -> Union[Callable[[Callable[..., T]], "GreatAI[T]"], "GreatAI[T]"]:
):
if func is None:
return cast(
Callable[[Callable[..., T]], GreatAI[T]],
@ -105,9 +126,14 @@ class GreatAI(Generic[T]):
self,
batch: Iterable[Any],
concurrency: Optional[int] = None,
do_not_persist_traces: bool = False,
) -> List[Trace[T]]:
return parallel_map(
freeze_arguments(self._cached_func), batch, concurrency=concurrency
freeze_arguments(
partial(self._cached_func, do_not_persist_traces=do_not_persist_traces)
),
batch,
concurrency=concurrency,
)
@property

View file

@ -24,7 +24,8 @@ T = TypeVar("T")
class TracingContext(Generic[T]):
_contexts: DefaultDict[int, List["TracingContext"]] = defaultdict(lambda: [])
def __init__(self, function_name: str) -> None:
def __init__(self, function_name: str, do_not_persist_traces: bool) -> None:
self._do_not_persist_traces = do_not_persist_traces
self._models: List[Model] = []
self._values: Dict[str, Any] = {}
self._trace: Optional[Trace[T]] = None
@ -90,6 +91,7 @@ class TracingContext(Generic[T]):
)
assert self._trace is not None
get_context().tracing_database.save(self._trace)
if not self._do_not_persist_traces:
get_context().tracing_database.save(self._trace)
return False