From 321a33ce89cd36e259e91d611fc7a4ffe3723578 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Fri, 1 Nov 2024 21:59:07 -0400 Subject: [PATCH] fix(mypy): fix type hints (#261) --- dank_mids/_demo_mode.py | 1 - dank_mids/_exceptions.py | 2 +- dank_mids/brownie_patch/__init__.py | 4 ++-- dank_mids/brownie_patch/call.py | 4 ++-- dank_mids/brownie_patch/overloaded.py | 4 ++-- dank_mids/brownie_patch/types.py | 2 +- dank_mids/stats.py | 6 ++++-- 7 files changed, 12 insertions(+), 11 deletions(-) diff --git a/dank_mids/_demo_mode.py b/dank_mids/_demo_mode.py index d6b4d93e..ecafa56d 100644 --- a/dank_mids/_demo_mode.py +++ b/dank_mids/_demo_mode.py @@ -20,7 +20,6 @@ def info(self, *args: Any, **kwargs: Any) -> None: *args: Positional arguments (ignored). **kwargs: Keyword arguments (ignored). """ - ... # Choose between a real logger and a dummy logger based on the demo mode setting demo_logger: logging.Logger = logging.getLogger("dank_mids.demo") if ENVIRONMENT_VARIABLES.DEMO_MODE else DummyLogger() # type: ignore [attr-defined, assignment] diff --git a/dank_mids/_exceptions.py b/dank_mids/_exceptions.py index 932200d3..6374fc47 100644 --- a/dank_mids/_exceptions.py +++ b/dank_mids/_exceptions.py @@ -47,7 +47,7 @@ def __init__( """ super().__init__(exc.request_info, exc.history, status=exc.status, message=exc.message, headers=exc.headers) - self.args = (*self.args, request) + self.args = (*self.args, request) # type: ignore [has-type] self._exception = exc """ diff --git a/dank_mids/brownie_patch/__init__.py b/dank_mids/brownie_patch/__init__.py index d49a5276..abb73e6d 100644 --- a/dank_mids/brownie_patch/__init__.py +++ b/dank_mids/brownie_patch/__init__.py @@ -8,7 +8,7 @@ __all__ = ["DankContractCall", "DankContractMethod", "DankContractTx", "DankOverloadedMethod"] -from dank_mids.helpers._helpers import DankWeb3 +from dank_mids.helpers._helpers import DankEth, DankWeb3 dank_web3: DankWeb3 """ @@ -17,7 +17,7 @@ If you do not have brownie or it is not connected to an rpc when this module is loaded, this object will not exist. """ -dank_eth: AsyncEth +dank_eth: DankEth """ If you have brownie installed and connected, an instance of :py:class:`~AsyncEth`, providing asynchronous access to Ethereum blockchain methods via :py:const:`dank_web3`. diff --git a/dank_mids/brownie_patch/call.py b/dank_mids/brownie_patch/call.py index 408e5005..812f4b31 100644 --- a/dank_mids/brownie_patch/call.py +++ b/dank_mids/brownie_patch/call.py @@ -65,7 +65,7 @@ def _get_coroutine_fn(w3: DankWeb3, len_inputs: int): async def coroutine( self: ContractCall, - *args: Tuple[Any,...], + *args: Any, block_identifier: Optional[BlockIdentifier] = None, decimals: Optional[int] = None, override: Optional[Dict[str, str]] = None @@ -147,7 +147,7 @@ async def _request_data_no_args(call: ContractCall) -> HexStr: __eth_abi_encode = eth_abi.encode if hasattr(eth_abi, 'encode') else eth_abi.encode_abi __eth_abi_decode = eth_abi.decode if hasattr(eth_abi, 'decode') else eth_abi.decode_abi -def __encode_input(abi: Dict[str, Any], signature: str, *args: Tuple[Any,...]) -> Union[HexStr, Exception]: +def __encode_input(abi: Dict[str, Any], signature: str, *args: Any) -> Union[HexStr, Exception]: try: data = format_input(abi, args) types_list = get_type_strings(abi["inputs"]) diff --git a/dank_mids/brownie_patch/overloaded.py b/dank_mids/brownie_patch/overloaded.py index dabd80cc..a8193a35 100644 --- a/dank_mids/brownie_patch/overloaded.py +++ b/dank_mids/brownie_patch/overloaded.py @@ -1,6 +1,6 @@ import functools from types import MethodType -from typing import Any, Dict, Optional, Tuple, Union +from typing import Any, Dict, Optional, Union from brownie import Contract from brownie.network.contract import ContractCall, ContractTx, OverloadedMethod @@ -21,7 +21,7 @@ def _patch_overloaded_method(call: OverloadedMethod, w3: DankWeb3) -> None: @functools.wraps(call) async def coroutine( self: Contract, - *args: Tuple[Any,...], + *args: Any, block_identifier: Optional[Union[int, str, bytes]] = None, decimals: Optional[int] = None, override: Optional[Dict[str, str]] = None, diff --git a/dank_mids/brownie_patch/types.py b/dank_mids/brownie_patch/types.py index 8c9e3e6a..1f116f2a 100644 --- a/dank_mids/brownie_patch/types.py +++ b/dank_mids/brownie_patch/types.py @@ -38,7 +38,7 @@ class DankOverloadedMethod(OverloadedMethod, _DankMethodMixin): You can await this object directly to call the contract method with no arguments at the latest block. """ - methods: Dict[Tuple[str], _NonOverloaded] + methods: Dict[Tuple[str, ...], _NonOverloaded] __slots__ = "_address", "_name", "_owner", "methods", "natspec" async def coroutine( # type: ignore [empty-body] self, diff --git a/dank_mids/stats.py b/dank_mids/stats.py index ce4a0105..da4cb071 100644 --- a/dank_mids/stats.py +++ b/dank_mids/stats.py @@ -23,7 +23,7 @@ from concurrent.futures import ProcessPoolExecutor from time import time from typing import (TYPE_CHECKING, Any, Callable, DefaultDict, Deque, Set, - Type, Union) + Type, TypeVar) import msgspec from typed_envs.registry import _ENVIRONMENT_VARIABLES_SET_BY_USER @@ -37,6 +37,8 @@ _LogLevel = int +T = TypeVar("T") + # New logging levels: # DEBUG=10, INFO=20, STATS = 13 @@ -128,7 +130,7 @@ def _log_nocheck(self, level: _LogLevel, *args, **kwargs) -> None: except IndexError: raise ValueError("Both a level and a message are required.") from None - def _log_fn_result(self, level: _LogLevel, callable: Callable[[], str], *callable_args, **logging_kwargs) -> None: + def _log_fn_result(self, level: _LogLevel, callable: Callable[[T], str], *callable_args: T, **logging_kwargs) -> None: """If `self.isEnabledFor(level)` is True, will call `callable` with your args and log the output.""" if self.isEnabledFor(level): return self._log_nocheck(level, callable(*callable_args), (), **logging_kwargs)