commit fb803c96db3ac357850b93730a33154adc719a89 Author: Illia Volochii illia.volochii@gmail.com Date: Sun May 17 17:40:29 2020 +0300
Add type hints to new functions --- stem/descriptor/remote.py | 4 ++-- stem/util/__init__.py | 31 +++++++++++++++++-------------- stem/util/test_tools.py | 8 ++++---- 3 files changed, 23 insertions(+), 20 deletions(-)
diff --git a/stem/descriptor/remote.py b/stem/descriptor/remote.py index daa3b42c..429954cb 100644 --- a/stem/descriptor/remote.py +++ b/stem/descriptor/remote.py @@ -581,8 +581,8 @@ class Query(stem.util.AsyncClassWrapper): **kwargs, )
- def start(self): - return self._call_async_method_soon('start') + def start(self) -> None: + self._call_async_method_soon('start')
def run(self, suppress = False): return self._execute_async_method('run', suppress) diff --git a/stem/util/__init__.py b/stem/util/__init__.py index a230cfbd..7c53730c 100644 --- a/stem/util/__init__.py +++ b/stem/util/__init__.py @@ -8,8 +8,10 @@ Utility functions used by the stem library. import asyncio import datetime import threading +from concurrent.futures import Future
-from typing import Any, Union +from types import TracebackType +from typing import Any, AsyncIterator, Iterator, Optional, Type, Union
__all__ = [ 'conf', @@ -150,36 +152,37 @@ class CombinedReentrantAndAsyncioLock:
__slots__ = ('_r_lock', '_async_lock')
- def __init__(self): + def __init__(self) -> None: self._r_lock = threading.RLock() self._async_lock = asyncio.Lock()
- async def acquire(self): + async def acquire(self) -> bool: await self._async_lock.acquire() self._r_lock.acquire() + return True
- def release(self): + def release(self) -> None: self._r_lock.release() self._async_lock.release()
- async def __aenter__(self): + async def __aenter__(self) -> 'CombinedReentrantAndAsyncioLock': await self.acquire() return self
- async def __aexit__(self, exc_type, exc_val, exc_tb): + async def __aexit__(self, exit_type: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType]) -> None: self.release()
class ThreadForWrappedAsyncClass(threading.Thread): - def __init__(self, *args, **kwargs): + def __init__(self, *args: Any, **kwargs: Any) -> None: super().__init__(*args, *kwargs) self.loop = asyncio.new_event_loop() self.setDaemon(True)
- def run(self): + def run(self) -> None: self.loop.run_forever()
- def join(self, timeout=None): + def join(self, timeout: Optional[float] = None) -> None: self.loop.call_soon_threadsafe(self.loop.stop) super().join(timeout) self.loop.close() @@ -189,7 +192,7 @@ class AsyncClassWrapper: _thread_for_wrapped_class: ThreadForWrappedAsyncClass _wrapped_instance: type
- def _init_async_class(self, async_class, *args, **kwargs): + def _init_async_class(self, async_class: Type, *args: Any, **kwargs: Any) -> Any: thread = self._thread_for_wrapped_class # The asynchronous class should be initialized in the thread where # its methods will be executed. @@ -201,17 +204,17 @@ class AsyncClassWrapper:
return async_class(*args, **kwargs)
- def _call_async_method_soon(self, method_name, *args, **kwargs): + def _call_async_method_soon(self, method_name: str, *args: Any, **kwargs: Any) -> Future: return asyncio.run_coroutine_threadsafe( getattr(self._wrapped_instance, method_name)(*args, **kwargs), self._thread_for_wrapped_class.loop, )
- def _execute_async_method(self, method_name, *args, **kwargs): + def _execute_async_method(self, method_name: str, *args: Any, **kwargs: Any) -> Any: return self._call_async_method_soon(method_name, *args, **kwargs).result()
- def _execute_async_generator_method(self, method_name, *args, **kwargs): - async def convert_async_generator(generator): + def _execute_async_generator_method(self, method_name: str, *args: Any, **kwargs: Any) -> Iterator: + async def convert_async_generator(generator: AsyncIterator) -> Iterator: return iter([d async for d in generator])
return asyncio.run_coroutine_threadsafe( diff --git a/stem/util/test_tools.py b/stem/util/test_tools.py index 455f3da3..f5ae7525 100644 --- a/stem/util/test_tools.py +++ b/stem/util/test_tools.py @@ -46,7 +46,7 @@ import stem.util.conf import stem.util.enum import stem.util.system
-from typing import Any, Callable, Dict, Iterator, List, Mapping, Optional, Sequence, Tuple, Type, Union +from typing import Any, Awaitable, Callable, Dict, Iterator, List, Mapping, Optional, Sequence, Tuple, Type, Union
CONFIG = stem.util.conf.config_dict('test', { 'pycodestyle.ignore': [], @@ -686,7 +686,7 @@ def _is_ignored(config: Mapping[str, Sequence[str]], path: str, issue: str) -> b
def async_test(func: Callable) -> Callable: @functools.wraps(func) - def wrapper(*args, **kwargs): + def wrapper(*args: Any, **kwargs: Any) -> Any: loop = asyncio.new_event_loop() try: result = loop.run_until_complete(func(*args, **kwargs)) @@ -696,13 +696,13 @@ def async_test(func: Callable) -> Callable: return wrapper
-def coro_func_returning_value(return_value): +def coro_func_returning_value(return_value: Any) -> Callable[..., Awaitable]: async def coroutine_func(*args, **kwargs): return return_value return coroutine_func
-def coro_func_raising_exc(exc): +def coro_func_raising_exc(exc: Exception) -> Callable[..., Awaitable]: async def coroutine_func(*args, **kwargs): raise exc return coroutine_func
tor-commits@lists.torproject.org