Skip to content

Commit

Permalink
Fix missing type exceptions and asynchronous defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
KaQuMiQ authored Oct 28, 2024
1 parent 4594248 commit 6a91100
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 34 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "haiway"
description = "Framework for dependency injection and state management within structured concurrency model."
version = "0.3.0"
version = "0.3.1"
readme = "README.md"
maintainers = [
{ name = "Kacper Kaliński", email = "[email protected]" },
Expand Down
4 changes: 2 additions & 2 deletions src/haiway/helpers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from haiway.helpers.asynchronous import asynchronous
from haiway.helpers.cached import cache
from haiway.helpers.asynchrony import asynchronous
from haiway.helpers.caching import cache
from haiway.helpers.retries import retry
from haiway.helpers.throttling import throttle
from haiway.helpers.timeouted import timeout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from concurrent.futures import Executor
from contextvars import Context, copy_context
from functools import partial
from typing import Any, Literal, cast, overload
from typing import Any, cast, overload

from haiway.types.missing import MISSING, Missing, not_missing
from haiway.types.missing import MISSING, Missing

__all__ = [
"asynchronous",
Expand All @@ -25,7 +25,7 @@ def asynchronous[**Args, Result]() -> (
def asynchronous[**Args, Result](
*,
loop: AbstractEventLoop | None = None,
executor: Executor | Literal["default"],
executor: Executor,
) -> Callable[
[Callable[Args, Result]],
Callable[Args, Coroutine[None, None, Result]],
Expand All @@ -43,7 +43,7 @@ def asynchronous[**Args, Result](
function: Callable[Args, Result] | None = None,
/,
loop: AbstractEventLoop | None = None,
executor: Executor | Literal["default"] | Missing = MISSING,
executor: Executor | Missing = MISSING,
) -> (
Callable[
[Callable[Args, Result]],
Expand All @@ -63,10 +63,9 @@ def asynchronous[**Args, Result](
loop: AbstractEventLoop | None
loop used to call the function. When None was provided the loop currently running while \
executing the function will be used. Default is None.
executor: Executor | Literal["default"] | Missing
executor used to run the function. Specifying "default" uses a default loop executor.
When not provided (Missing) no executor will be used \
(function will by just wrapped as an async function without any executor)
executor: Executor | Missing
executor used to run the function. When not provided (Missing) default loop executor\
will be used.
Returns
-------
Expand All @@ -79,26 +78,11 @@ def wrap(
) -> Callable[Args, Coroutine[None, None, Result]]:
assert not iscoroutinefunction(wrapped), "Cannot wrap async function in executor" # nosec: B101

if not_missing(executor):
return _ExecutorWrapper(
wrapped,
loop=loop,
executor=cast(Executor | None, None if executor == "default" else executor),
)

else:

async def wrapper(
*args: Args.args,
**kwargs: Args.kwargs,
) -> Result:
return wrapped(
*args,
**kwargs,
)

_mimic_async(wrapped, within=wrapper)
return wrapper
return _ExecutorWrapper(
wrapped,
loop=loop,
executor=cast(Executor | None, None if executor is MISSING else executor),
)

if function := function:
return wrap(wrapped=function)
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions src/haiway/types/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ def __getattribute__(
self,
name: str,
) -> Any:
raise RuntimeError("Missing has no attributes")
raise AttributeError("Missing has no attributes")

def __setattr__(
self,
__name: str,
__value: Any,
) -> None:
raise RuntimeError("Missing can't be modified")
raise AttributeError("Missing can't be modified")

def __delattr__(
self,
__name: str,
) -> None:
raise RuntimeError("Missing can't be modified")
raise AttributeError("Missing can't be modified")


MISSING: Final[Missing] = Missing()
Expand Down

0 comments on commit 6a91100

Please sign in to comment.