Skip to content

Commit af33015

Browse files
aleexharrisfselmokclowes
authored andcommitted
Make AsyncWeb3 Generic (ethereum#3761)
* fix: Made function params Optional, as None is the default input * feat: Made AsyncWeb3 class Generic to fix type errors when importing Web3 * refactor: remove unnecessary noqa: E501 uses * fix: remove `Optional` where it isn't optional * fix: Use `Optional` where appropriate * chore: add newsfragment for ethereum#3761 * fix: Removed where it isn't optional * Remove unnecessary changes --------- Co-authored-by: fselmo <[email protected]> Co-authored-by: kclowes <[email protected]>
1 parent 8849102 commit af33015

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+376
-327
lines changed

faster_ens/async_ens.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@
7373
AsyncContract,
7474
AsyncContractFunction,
7575
)
76-
from faster_web3.main import AsyncWeb3 # noqa: F401
76+
from faster_web3.main import ( # noqa: F401
77+
AsyncWeb3,
78+
)
7779
from faster_web3.middleware.base import ( # noqa: F401
7880
Middleware,
7981
)
@@ -97,7 +99,7 @@ class AsyncENS(BaseENS):
9799
"""
98100

99101
# mypy types
100-
w3: "AsyncWeb3"
102+
w3: "AsyncWeb3[Any]"
101103

102104
def __init__(
103105
self,
@@ -124,7 +126,11 @@ def __init__(
124126
)
125127

126128
@classmethod
127-
def from_web3(cls, w3: "AsyncWeb3", addr: Optional[ChecksumAddress] = None) -> "AsyncENS":
129+
def from_web3(
130+
cls,
131+
w3: "AsyncWeb3[Any]",
132+
addr: Optional[ChecksumAddress] = None,
133+
) -> "AsyncENS":
128134
"""
129135
Generate an AsyncENS instance with web3
130136

faster_ens/base_ens.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
@mypyc_attr(allow_interpreted_subclasses=True)
4343
class BaseENS:
44-
w3: Union["AsyncWeb3", "Web3"]
44+
w3: Union["AsyncWeb3[Any]", "Web3"]
4545
ens: Optional[Union["Contract", "AsyncContract"]] = None
4646
_resolver_contract: Optional[Union[Type["Contract"], Type["AsyncContract"]]] = None
4747
_reverse_resolver_contract: Optional[Union[Type["Contract"], Type["AsyncContract"]]] = None

faster_ens/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def is_valid_ens_name(ens_name: str) -> bool:
300300
def init_async_web3(
301301
provider: Optional["AsyncBaseProvider"] = None,
302302
middleware: Optional[Sequence[Tuple["Middleware", str]]] = (),
303-
) -> "AsyncWeb3":
303+
) -> "AsyncWeb3[Any]":
304304
from faster_web3 import (
305305
AsyncWeb3 as AsyncWeb3Main,
306306
)

faster_web3/_utils/abi.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ def _named_subtree(
858858

859859
def recursive_dict_to_namedtuple(data: Dict[str, Any]) -> Tuple[Any, ...]:
860860
def _dict_to_namedtuple(
861-
value: Union[Dict[str, Any], List[Any]]
861+
value: Union[Dict[str, Any], List[Any]],
862862
) -> Union[Tuple[Any, ...], List[Any]]:
863863
if not isinstance(value, dict):
864864
return value
@@ -870,7 +870,7 @@ def _dict_to_namedtuple(
870870

871871

872872
def abi_decoded_namedtuple_factory(
873-
fields: Tuple[Any, ...]
873+
fields: Tuple[Any, ...],
874874
) -> Callable[..., Tuple[Any, ...]]:
875875
class ABIDecodedNamedTuple(namedtuple("ABIDecodedNamedTuple", fields, rename=True)): # type: ignore # noqa: E501
876876
def __new__(self, args: Any) -> "ABIDecodedNamedTuple":
@@ -883,9 +883,9 @@ def __new__(self, args: Any) -> "ABIDecodedNamedTuple":
883883

884884

885885
async def async_data_tree_map(
886-
async_w3: "AsyncWeb3",
886+
async_w3: "AsyncWeb3[Any]",
887887
func: Callable[
888-
["AsyncWeb3", TypeStr, Any], Coroutine[Any, Any, Tuple[TypeStr, Any]]
888+
["AsyncWeb3[Any]", TypeStr, Any], Coroutine[Any, Any, Tuple[TypeStr, Any]]
889889
],
890890
data_tree: Any,
891891
) -> "ABITypedData":
@@ -908,7 +908,7 @@ async def async_map_to_typed_data(elements: Any) -> "ABITypedData":
908908

909909
@reject_recursive_repeats
910910
async def async_recursive_map(
911-
async_w3: "AsyncWeb3",
911+
async_w3: "AsyncWeb3[Any]",
912912
func: Callable[[Any], Coroutine[Any, Any, TReturn]],
913913
data: Any,
914914
) -> TReturn:

faster_web3/_utils/async_transactions.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import (
22
TYPE_CHECKING,
3+
Any,
34
Dict,
45
Final,
56
Optional,
@@ -47,13 +48,13 @@
4748

4849
# unused vars present in these funcs because they all need to have the same signature
4950
async def _estimate_gas(
50-
async_w3: "AsyncWeb3", tx: TxParams, _defaults: Dict[str, Union[bytes, int]]
51+
async_w3: "AsyncWeb3[Any]", tx: TxParams, _defaults: Dict[str, Union[bytes, int]]
5152
) -> int:
5253
return await async_w3.eth.estimate_gas(tx)
5354

5455

5556
async def _max_fee_per_gas(
56-
async_w3: "AsyncWeb3", tx: TxParams, defaults: Dict[str, Union[bytes, int]]
57+
async_w3: "AsyncWeb3[Any]", tx: TxParams, defaults: Dict[str, Union[bytes, int]]
5758
) -> Wei:
5859
block = await async_w3.eth.get_block("latest")
5960
max_priority_fee = tx.get(
@@ -63,13 +64,13 @@ async def _max_fee_per_gas(
6364

6465

6566
async def _max_priority_fee_gas(
66-
async_w3: "AsyncWeb3", _tx: TxParams, _defaults: Dict[str, Union[bytes, int]]
67+
async_w3: "AsyncWeb3[Any]", _tx: TxParams, _defaults: Dict[str, Union[bytes, int]]
6768
) -> Wei:
6869
return await async_w3.eth.max_priority_fee
6970

7071

7172
async def _chain_id(
72-
async_w3: "AsyncWeb3", _tx: TxParams, _defaults: Dict[str, Union[bytes, int]]
73+
async_w3: "AsyncWeb3[Any]", _tx: TxParams, _defaults: Dict[str, Union[bytes, int]]
7374
) -> int:
7475
return await async_w3.eth.chain_id
7576

@@ -93,7 +94,7 @@ async def get_block_gas_limit(
9394

9495

9596
async def get_buffered_gas_estimate(
96-
async_w3: "AsyncWeb3", transaction: TxParams, gas_buffer: int = 100000
97+
async_w3: "AsyncWeb3[Any]", transaction: TxParams, gas_buffer: int = 100000
9798
) -> int:
9899
gas_estimate_transaction = cast(TxParams, dict(**transaction))
99100

@@ -111,7 +112,9 @@ async def get_buffered_gas_estimate(
111112
return min(gas_limit, gas_estimate + gas_buffer)
112113

113114

114-
async def async_fill_nonce(async_w3: "AsyncWeb3", transaction: TxParams) -> TxParams:
115+
async def async_fill_nonce(
116+
async_w3: "AsyncWeb3[Any]", transaction: TxParams
117+
) -> TxParams:
115118
if "from" in transaction and "nonce" not in transaction:
116119
tx_count = await async_w3.eth.get_transaction_count(
117120
cast(ChecksumAddress, transaction["from"]),
@@ -122,7 +125,7 @@ async def async_fill_nonce(async_w3: "AsyncWeb3", transaction: TxParams) -> TxPa
122125

123126

124127
async def async_fill_transaction_defaults(
125-
async_w3: "AsyncWeb3", transaction: TxParams
128+
async_w3: "AsyncWeb3[Any]", transaction: TxParams
126129
) -> TxParams:
127130
"""
128131
If async_w3 is None, fill as much as possible while offline
@@ -166,7 +169,7 @@ async def async_fill_transaction_defaults(
166169

167170

168171
async def async_get_required_transaction(
169-
async_w3: "AsyncWeb3", transaction_hash: _Hash32
172+
async_w3: "AsyncWeb3[Any]", transaction_hash: _Hash32
170173
) -> TxData:
171174
current_transaction = await async_w3.eth.get_transaction(transaction_hash)
172175
if not current_transaction:
@@ -177,7 +180,7 @@ async def async_get_required_transaction(
177180

178181

179182
async def async_replace_transaction(
180-
async_w3: "AsyncWeb3", current_transaction: TxData, new_transaction: TxParams
183+
async_w3: "AsyncWeb3[Any]", current_transaction: TxData, new_transaction: TxParams
181184
) -> HexBytes:
182185
new_transaction = prepare_replacement_transaction(
183186
async_w3, current_transaction, new_transaction

faster_web3/_utils/batching.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575

7676
@final
7777
class RequestBatcher(Generic[TFunc]):
78-
def __init__(self, web3: Union["AsyncWeb3", "Web3"]) -> None:
78+
def __init__(self, web3: Union["AsyncWeb3[Any]", "Web3"]) -> None:
7979
self.web3: Final = web3
8080
self._requests_info: Final[List[BatchRequestInformation]] = []
8181
self._async_requests_info: Final[

faster_web3/_utils/caching/caching_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def _should_cache_response(
254254

255255

256256
def handle_request_caching(
257-
func: Callable[[SYNC_PROVIDER_TYPE, RPCEndpoint, Any], "RPCResponse"]
257+
func: Callable[[SYNC_PROVIDER_TYPE, RPCEndpoint, Any], "RPCResponse"],
258258
) -> Callable[..., "RPCResponse"]:
259259
def wrapper(
260260
provider: SYNC_PROVIDER_TYPE, method: RPCEndpoint, params: Any
@@ -415,7 +415,7 @@ def async_handle_recv_caching(
415415
func: Callable[
416416
["PersistentConnectionProvider", "RPCRequest"],
417417
Coroutine[Any, Any, "RPCResponse"],
418-
]
418+
],
419419
) -> Callable[..., Coroutine[Any, Any, "RPCResponse"]]:
420420
async def wrapper(
421421
provider: "PersistentConnectionProvider",

faster_web3/_utils/contracts.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def find_matching_event_abi(
122122

123123

124124
def encode_abi(
125-
w3: Union["AsyncWeb3", "Web3"],
125+
w3: Union["AsyncWeb3[Any]", "Web3"],
126126
abi: ABIElement,
127127
arguments: Sequence[Any],
128128
data: Optional[Union[HexStr, HexBytes]] = None,
@@ -168,7 +168,7 @@ def encode_abi(
168168

169169
def prepare_transaction(
170170
address: Union[ChecksumAddress, Address, None],
171-
w3: Union["AsyncWeb3", "Web3"],
171+
w3: Union["AsyncWeb3[Any]", "Web3"],
172172
abi_element_identifier: ABIElementIdentifier,
173173
contract_abi: Optional[ABI] = None,
174174
abi_callable: Optional[ABICallable] = None,
@@ -232,7 +232,7 @@ def prepare_transaction(
232232

233233

234234
def encode_transaction_data(
235-
w3: Union["AsyncWeb3", "Web3"],
235+
w3: Union["AsyncWeb3[Any]", "Web3"],
236236
abi_element_identifier: ABIElementIdentifier,
237237
contract_abi: Optional[ABI] = None,
238238
abi_callable: Optional[ABICallable] = None,
@@ -361,7 +361,7 @@ def parse_block_identifier_int(w3: "Web3", block_identifier_int: int) -> BlockNu
361361

362362

363363
async def async_parse_block_identifier(
364-
async_w3: "AsyncWeb3", block_identifier: BlockIdentifier
364+
async_w3: "AsyncWeb3[Any]", block_identifier: BlockIdentifier
365365
) -> BlockIdentifier:
366366
if block_identifier is None:
367367
return async_w3.eth.default_block
@@ -379,7 +379,7 @@ async def async_parse_block_identifier(
379379

380380

381381
async def async_parse_block_identifier_int(
382-
async_w3: "AsyncWeb3", block_identifier_int: int
382+
async_w3: "AsyncWeb3[Any]", block_identifier_int: int
383383
) -> BlockNumber:
384384
if block_identifier_int >= 0:
385385
block_num = block_identifier_int

faster_web3/_utils/ens.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ async def address(self, name: str) -> ChecksumAddress:
7878

7979
@contextmanager
8080
def ens_addresses(
81-
w3: Union["Web3", "AsyncWeb3"], name_addr_pairs: Dict[str, ChecksumAddress]
81+
w3: Union["Web3", "AsyncWeb3[Any]"], name_addr_pairs: Dict[str, ChecksumAddress]
8282
) -> Iterator[None]:
8383
original_ens = w3.ens
8484
if w3.provider.is_async:

faster_web3/_utils/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ def deploy(self, w3: "Web3") -> "LogFilter":
454454

455455

456456
class AsyncEventFilterBuilder(BaseEventFilterBuilder):
457-
async def deploy(self, async_w3: "AsyncWeb3") -> "AsyncLogFilter":
457+
async def deploy(self, async_w3: "AsyncWeb3[Any]") -> "AsyncLogFilter":
458458
if not isinstance(async_w3, faster_web3.AsyncWeb3):
459459
raise Web3ValueError(f"Invalid web3 argument: got: {async_w3!r}")
460460

0 commit comments

Comments
 (0)