Fix typing

This commit is contained in:
Andras Schmelczer 2022-06-25 15:32:20 +02:00
parent 803811f4e3
commit 2b2e4e0b28
2 changed files with 36 additions and 8 deletions

View file

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

View file

@ -24,7 +24,8 @@ T = TypeVar("T")
class TracingContext(Generic[T]): class TracingContext(Generic[T]):
_contexts: DefaultDict[int, List["TracingContext"]] = defaultdict(lambda: []) _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._models: List[Model] = []
self._values: Dict[str, Any] = {} self._values: Dict[str, Any] = {}
self._trace: Optional[Trace[T]] = None self._trace: Optional[Trace[T]] = None
@ -90,6 +91,7 @@ class TracingContext(Generic[T]):
) )
assert self._trace is not None 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 return False