|
| 1 | +import asyncio |
| 2 | +import weakref |
1 | 3 | from asyncio import ( |
2 | 4 | AbstractEventLoop, |
3 | 5 | Future, |
|
8 | 10 | sleep, |
9 | 11 | ) |
10 | 12 | from time import time |
11 | | -from typing import TYPE_CHECKING, Any, Generator, Optional, Union |
| 13 | +from typing import TYPE_CHECKING, Any, Final, Generator, Optional, Union, final |
12 | 14 | from weakref import ProxyType, proxy |
13 | 15 |
|
14 | 16 | from web3.types import RPCResponse |
15 | 17 |
|
16 | 18 | from dank_mids._logging import DEBUG, getLogger |
17 | 19 | from dank_mids.helpers._errors import error_logger_debug |
| 20 | +from dank_mids.types import T |
18 | 21 |
|
19 | 22 | if TYPE_CHECKING: |
20 | 23 | from dank_mids._requests import _RequestBase, _Response |
21 | 24 |
|
22 | 25 |
|
23 | | -logger = getLogger("dank_mids.future") |
| 26 | +logger: Final = getLogger("dank_mids.future") |
24 | 27 |
|
25 | | -_logger_is_enabled_for = logger.isEnabledFor |
26 | | -_logger_log = logger._log |
| 28 | +_logger_is_enabled_for: Final = logger.isEnabledFor |
| 29 | +_logger_log: Final = logger._log |
27 | 30 |
|
28 | | -_future_init = Future.__init__ |
29 | | -_future_await = Future.__await__ |
30 | | -_future_set_result = Future.set_result |
31 | | -_future_set_exc = Future.set_exception |
| 31 | +_future_init: Final = Future.__init__ |
| 32 | +_future_await: Final = Future.__await__ |
| 33 | +_future_set_result: Final = Future.set_result |
| 34 | +_future_set_exc: Final = Future.set_exception |
32 | 35 |
|
| 36 | +create_task: Final = asyncio.create_task |
| 37 | +get_running_loop: Final = asyncio.get_running_loop |
| 38 | +sleep: Final = asyncio.sleep |
33 | 39 |
|
34 | | -class DebuggableFuture(Future): |
| 40 | +proxy: Final = weakref.proxy |
| 41 | + |
| 42 | + |
| 43 | +@final |
| 44 | +class DebuggableFuture(Future[T]): |
35 | 45 | # default values |
36 | 46 | _debug_logs_enabled: bool = False |
37 | 47 | __debug_daemon_task: Optional["Task[None]"] = None |
38 | 48 |
|
39 | 49 | # type hints |
40 | 50 | _result: Optional[RPCResponse] |
41 | 51 |
|
42 | | - def __init__(self, owner: "_RequestBase", loop: AbstractEventLoop) -> None: |
| 52 | + def __init__(self, owner: "_RequestBase", loop: AbstractEventLoop) -> None: # type: ignore [type-arg] |
43 | 53 | _future_init(self, loop=loop) |
44 | 54 | if _logger_is_enabled_for(DEBUG): |
45 | 55 | self._debug_logs_enabled = True |
46 | | - self._owner: ProxyType["_RequestBase[_Response]"] = proxy(owner) |
| 56 | + self._owner: weakref.ProxyType["_RequestBase[_Response]"] = proxy(owner) # type: ignore [valid-type] |
47 | 57 |
|
48 | 58 | def __await__(self) -> Generator[Any, None, RPCResponse]: |
49 | 59 | if self._debug_logs_enabled and self.__debug_daemon_task is None: |
|
0 commit comments