Skip to content

Commit

Permalink
fix: Avoid loosing type hints because of decorators (#379)
Browse files Browse the repository at this point in the history
Co-authored-by: Diwank Singh Tomer <[email protected]>
  • Loading branch information
whiterabbit1983 and creatorrr authored May 31, 2024
1 parent bbb4544 commit 0d29820
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions sdks/python/julep/managers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
from functools import wraps
from typing import Callable
from uuid import UUID
from typing_extensions import ParamSpec
from pydantic.main import Model
from ..api.types import ResourceCreatedResponse


P = ParamSpec("P")


class NotSet:
pass

Expand Down Expand Up @@ -33,19 +38,20 @@ def is_valid_uuid4(uuid_to_test: str) -> bool:
return True


def rewrap_in_class(cls):
def decorator(func: Callable[..., ResourceCreatedResponse]):
@wraps(func)
def rewrap_in_class(cls: type[Model]):
def decorator(func: Callable[P, ResourceCreatedResponse]):
# This wrapper is used for asynchronous functions to ensure they are properly awaited and their results are processed by `cls.construct`.
@wraps(func)
async def async_wrapper(*args, **kwargs):
result = await func(*args, **kwargs)
return cls.construct(**kwargs, **result.dict())
return cls.model_construct(**kwargs, **result.dict())

# This wrapper handles synchronous functions, directly calling them and processing their results with `cls.construct`.
@wraps(func)
def sync_wrapper(*args, **kwargs):
# Logging at this point might be useful for debugging, but should use a proper logging framework instead of print statements for production code.
result = func(*args, **kwargs)
return cls.construct(**kwargs, **result.dict())
return cls.model_construct(**kwargs, **result.dict())

return async_wrapper if iscoroutinefunction(func) else sync_wrapper

Expand Down

0 comments on commit 0d29820

Please sign in to comment.