From 168eeae6a0393f69a75eeb450b9ba7423ec6563d Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Sun, 11 Feb 2024 16:45:01 -0500 Subject: [PATCH] chore: refactor TypeVars (#111) --- a_sync/_typing.py | 14 ++++++++++---- a_sync/future.py | 10 +--------- a_sync/iter.py | 3 +-- a_sync/modified.py | 2 +- a_sync/primitives/executor.py | 9 ++------- a_sync/primitives/locks/semaphore.py | 7 +------ a_sync/primitives/queue.py | 3 +-- 7 files changed, 17 insertions(+), 31 deletions(-) diff --git a/a_sync/_typing.py b/a_sync/_typing.py index 893659d1..61975886 100644 --- a/a_sync/_typing.py +++ b/a_sync/_typing.py @@ -1,16 +1,20 @@ import asyncio from concurrent.futures._base import Executor -from typing import (TYPE_CHECKING, Any, Awaitable, Callable, DefaultDict, Dict, - Generic, Iterator, List, Literal, Optional, Tuple, - Type, TypedDict, TypeVar, Union, overload) +from decimal import Decimal +from typing import (TYPE_CHECKING, Any, AsyncIterable, AsyncIterator, Awaitable, + Callable, DefaultDict, Dict, Generic, Iterable, Iterator, + List, Literal, Optional, Set, Tuple, Type, TypedDict, TypeVar, + Union, final, overload) -from typing_extensions import Concatenate, ParamSpec, Unpack +from typing_extensions import Concatenate, ParamSpec, Self, Unpack if TYPE_CHECKING: from a_sync.abstract import ASyncABC T = TypeVar("T") +K = TypeVar("K") +V = TypeVar("V") P = ParamSpec("P") MaybeAwaitable = Union[Awaitable[T], T] @@ -46,3 +50,5 @@ class ModifierKwargs(TypedDict, total=False): semaphore: SemaphoreSpec # sync modifiers executor: Executor + +AnyIterable = Union[AsyncIterable[K], Iterable[K]] diff --git a/a_sync/future.py b/a_sync/future.py index 4ee0070b..35e70595 100644 --- a/a_sync/future.py +++ b/a_sync/future.py @@ -2,19 +2,11 @@ import asyncio import concurrent.futures -from decimal import Decimal from functools import partial, wraps from inspect import isawaitable -from typing import (Any, Awaitable, Callable, Generic, List, Optional, Set, - TypeVar, Union, final, overload) -from typing_extensions import ParamSpec, Self, Unpack +from a_sync._typing import * -from a_sync._typing import ModifierKwargs - -T = TypeVar('T') -P = ParamSpec('P') -MaybeAwaitable = Union[T, Awaitable[T]] def future(callable: Union[Callable[P, Awaitable[T]], Callable[P, T]] = None, **kwargs: Unpack[ModifierKwargs]) -> Callable[P, Union[T, "ASyncFuture[T]"]]: return _ASyncFutureWrappedFn(callable, **kwargs) diff --git a/a_sync/iter.py b/a_sync/iter.py index 44e68c9f..ba2729f1 100644 --- a/a_sync/iter.py +++ b/a_sync/iter.py @@ -1,8 +1,7 @@ import asyncio -from typing import AsyncIterable, AsyncIterator, Iterable, Iterator, TypeVar +from a_sync._typing import * -T = TypeVar('T') class ASyncIterable(AsyncIterable[T], Iterable[T]): """An abstract iterable implementation that can be used in both a `for` loop and an `async for` loop.""" diff --git a/a_sync/modified.py b/a_sync/modified.py index af1b6775..d76eb099 100644 --- a/a_sync/modified.py +++ b/a_sync/modified.py @@ -3,7 +3,7 @@ import functools import sys -from a_sync import _helpers, _kwargs, exceptions +from a_sync import _helpers, _kwargs from a_sync._typing import * from a_sync.modifiers.manager import ModifierManager diff --git a/a_sync/primitives/executor.py b/a_sync/primitives/executor.py index 6864411f..c6c1c32f 100644 --- a/a_sync/primitives/executor.py +++ b/a_sync/primitives/executor.py @@ -13,17 +13,12 @@ import weakref from concurrent.futures import _base, thread from functools import cached_property -from typing import Any, Callable, Optional, Tuple, TypeVar - -from typing_extensions import ParamSpec +from a_sync._typing import * from a_sync.primitives._debug import _DebugDaemonMixin -TEN_MINUTES = 60 * 10 - -T = TypeVar('T') -P = ParamSpec('P') +TEN_MINUTES = 60 * 10 class _AsyncExecutorMixin(cf.Executor, _DebugDaemonMixin): _max_workers: int diff --git a/a_sync/primitives/locks/semaphore.py b/a_sync/primitives/locks/semaphore.py index 1cf7ef5f..3a3cca76 100644 --- a/a_sync/primitives/locks/semaphore.py +++ b/a_sync/primitives/locks/semaphore.py @@ -3,17 +3,12 @@ import logging from collections import defaultdict from threading import Thread, current_thread -from typing import Callable, Literal, Optional, TypeVar - -from typing_extensions import ParamSpec +from a_sync._typing import * from a_sync.primitives._debug import _DebugDaemonMixin logger = logging.getLogger(__name__) -T = TypeVar('T') -P = ParamSpec('P') - class Semaphore(asyncio.Semaphore, _DebugDaemonMixin): def __init__(self, value: int, name=None, **kwargs) -> None: """ diff --git a/a_sync/primitives/queue.py b/a_sync/primitives/queue.py index bfbcf3bc..0388dee4 100644 --- a/a_sync/primitives/queue.py +++ b/a_sync/primitives/queue.py @@ -1,8 +1,7 @@ import asyncio import sys -from typing import Generic, List, TypeVar, Union, overload -T = TypeVar('T') +from a_sync._typing import * if sys.version_info < (3, 9): bases = (asyncio.Queue, Generic[T])