Skip to content

Commit

Permalink
🏷️ chore: refine some typing
Browse files Browse the repository at this point in the history
  • Loading branch information
SigureMo committed Oct 4, 2023
1 parent 8ef04d1 commit 797f879
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 6 additions & 3 deletions yutto/utils/funcutils/as_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
from functools import wraps
from typing import Any, Callable, TypeVar

T = TypeVar("T")
from typing_extensions import ParamSpec

R = TypeVar("R")
P = ParamSpec("P")

def as_sync(async_func: Callable[..., Coroutine[Any, Any, T]]) -> Callable[..., T]:

def as_sync(async_func: Callable[P, Coroutine[Any, Any, R]]) -> Callable[P, R]:
"""将异步函数变成同步函数,避免在调用时需要显式使用 asyncio.run
### Examples
Expand All @@ -30,7 +33,7 @@ async def itoa(a: int) -> str:
"""

@wraps(async_func)
def sync_func(*args: Any, **kwargs: Any):
def sync_func(*args: P.args, **kwargs: P.kwargs) -> R:
return asyncio.run(async_func(*args, **kwargs))

return sync_func
Expand Down
9 changes: 6 additions & 3 deletions yutto/utils/funcutils/singleton.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# type: ignore
from __future__ import annotations

from typing import Any, TypeVar

T = TypeVar("T")


class Singleton(type):
"""单例模式元类
Expand All @@ -21,9 +24,9 @@ class MyClass(BaseClass, metaclass=Singleton):
```
"""

_instances = {}
_instances: dict[Any, Any] = {}

def __call__(cls, *args, **kwargs):
def __call__(cls, *args: Any, **kwargs: Any):
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
return cls._instances[cls]

0 comments on commit 797f879

Please sign in to comment.