Skip to content

Commit 2ee9322

Browse files
committed
Tighten up some typing in Eth and AsyncEth methods
1 parent 3aaceec commit 2ee9322

File tree

4 files changed

+94
-54
lines changed

4 files changed

+94
-54
lines changed

web3/_utils/module_testing/eth_module.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
)
7777
from web3.types import ( # noqa: F401
7878
BlockData,
79+
ENS,
7980
FilterParams,
8081
LogReceipt,
8182
Nonce,
@@ -1374,7 +1375,7 @@ async def test_async_eth_get_storage_at_ens_name(
13741375
) -> None:
13751376
with ens_addresses(async_w3, {"emitter.eth": emitter_contract_address}):
13761377
storage = await async_w3.eth.get_storage_at( # type: ignore
1377-
"emitter.eth", 0
1378+
ENS("emitter.eth"), 0
13781379
)
13791380
assert isinstance(storage, HexBytes)
13801381

@@ -1621,7 +1622,7 @@ def test_eth_get_storage_at_ens_name(
16211622
self, w3: "Web3", emitter_contract_address: ChecksumAddress
16221623
) -> None:
16231624
with ens_addresses(w3, {"emitter.eth": emitter_contract_address}):
1624-
storage = w3.eth.get_storage_at("emitter.eth", 0)
1625+
storage = w3.eth.get_storage_at(ENS("emitter.eth"), 0)
16251626
assert isinstance(storage, HexBytes)
16261627

16271628
def test_eth_get_storage_at_invalid_address(self, w3: "Web3") -> None:
@@ -1645,7 +1646,7 @@ def test_eth_get_transaction_count_ens_name(
16451646
w3, {"unlocked-acct-dual-type.eth": unlocked_account_dual_type}
16461647
):
16471648
transaction_count = w3.eth.get_transaction_count(
1648-
"unlocked-acct-dual-type.eth"
1649+
ENS("unlocked-acct-dual-type.eth")
16491650
)
16501651
assert is_integer(transaction_count)
16511652
assert transaction_count >= 0
@@ -1716,7 +1717,7 @@ def test_eth_get_code_ens_address(
17161717
self, w3: "Web3", math_contract_address: ChecksumAddress
17171718
) -> None:
17181719
with ens_addresses(w3, {"mathcontract.eth": math_contract_address}):
1719-
code = w3.eth.get_code("mathcontract.eth")
1720+
code = w3.eth.get_code(ENS("mathcontract.eth"))
17201721
assert isinstance(code, HexBytes)
17211722
assert len(code) > 0
17221723

web3/eth/async_eth.py

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ async def gas_price(self) -> Wei:
143143

144144
# eth_maxPriorityFeePerGas
145145

146-
_max_priority_fee: Method[Callable[..., Awaitable[Wei]]] = Method(
146+
_max_priority_fee: Method[Callable[[], Awaitable[Wei]]] = Method(
147147
RPC.eth_maxPriorityFeePerGas,
148148
is_property=True,
149149
)
@@ -188,9 +188,12 @@ async def syncing(self) -> Union[SyncStatus, bool]:
188188

189189
# eth_feeHistory
190190

191-
_fee_history: Method[Callable[..., Awaitable[FeeHistory]]] = Method(
192-
RPC.eth_feeHistory, mungers=[default_root_munger]
193-
)
191+
_fee_history: Method[
192+
Callable[
193+
[int, Union[BlockParams, BlockNumber], Optional[List[float]]],
194+
Awaitable[FeeHistory],
195+
]
196+
] = Method(RPC.eth_feeHistory, mungers=[default_root_munger])
194197

195198
async def fee_history(
196199
self,
@@ -200,12 +203,19 @@ async def fee_history(
200203
) -> FeeHistory:
201204
return await self._fee_history(block_count, newest_block, reward_percentiles)
202205

203-
_call: Method[Callable[..., Awaitable[Union[bytes, bytearray]]]] = Method(
204-
RPC.eth_call, mungers=[BaseEth.call_munger]
205-
)
206-
207206
# eth_call
208207

208+
_call: Method[
209+
Callable[
210+
[
211+
TxParams,
212+
Optional[BlockIdentifier],
213+
Optional[CallOverride],
214+
],
215+
Awaitable[Union[bytes, bytearray]],
216+
]
217+
] = Method(RPC.eth_call, mungers=[BaseEth.call_munger])
218+
209219
async def call(
210220
self,
211221
transaction: TxParams,
@@ -254,9 +264,9 @@ async def _durin_call(
254264

255265
# eth_estimateGas
256266

257-
_estimate_gas: Method[Callable[..., Awaitable[int]]] = Method(
258-
RPC.eth_estimateGas, mungers=[BaseEth.estimate_gas_munger]
259-
)
267+
_estimate_gas: Method[
268+
Callable[[TxParams, Optional[BlockIdentifier]], Awaitable[int]]
269+
] = Method(RPC.eth_estimateGas, mungers=[BaseEth.estimate_gas_munger])
260270

261271
async def estimate_gas(
262272
self, transaction: TxParams, block_identifier: Optional[BlockIdentifier] = None
@@ -357,7 +367,9 @@ async def send_raw_transaction(self, transaction: Union[HexStr, bytes]) -> HexBy
357367
# eth_getBlockByHash
358368
# eth_getBlockByNumber
359369

360-
_get_block: Method[Callable[..., Awaitable[BlockData]]] = Method(
370+
_get_block: Method[
371+
Callable[[BlockIdentifier, bool], Awaitable[BlockData]]
372+
] = Method(
361373
method_choice_depends_on_args=select_method_for_block_identifier(
362374
if_predefined=RPC.eth_getBlockByNumber,
363375
if_hash=RPC.eth_getBlockByHash,
@@ -373,7 +385,12 @@ async def get_block(
373385

374386
# eth_getBalance
375387

376-
_get_balance: Method[Callable[..., Awaitable[Wei]]] = Method(
388+
_get_balance: Method[
389+
Callable[
390+
[Union[Address, ChecksumAddress, ENS], Optional[BlockIdentifier]],
391+
Awaitable[Wei],
392+
]
393+
] = Method(
377394
RPC.eth_getBalance,
378395
mungers=[BaseEth.block_id_munger],
379396
)
@@ -387,9 +404,12 @@ async def get_balance(
387404

388405
# eth_getCode
389406

390-
_get_code: Method[Callable[..., Awaitable[HexBytes]]] = Method(
391-
RPC.eth_getCode, mungers=[BaseEth.block_id_munger]
392-
)
407+
_get_code: Method[
408+
Callable[
409+
[Union[Address, ChecksumAddress, ENS], Optional[BlockIdentifier]],
410+
Awaitable[HexBytes],
411+
]
412+
] = Method(RPC.eth_getCode, mungers=[BaseEth.block_id_munger])
393413

394414
async def get_code(
395415
self,
@@ -412,7 +432,12 @@ async def get_logs(
412432

413433
# eth_getTransactionCount
414434

415-
_get_transaction_count: Method[Callable[..., Awaitable[Nonce]]] = Method(
435+
_get_transaction_count: Method[
436+
Callable[
437+
[Union[Address, ChecksumAddress, ENS], Optional[BlockIdentifier]],
438+
Awaitable[Nonce],
439+
]
440+
] = Method(
416441
RPC.eth_getTransactionCount,
417442
mungers=[BaseEth.block_id_munger],
418443
)
@@ -462,7 +487,12 @@ async def _wait_for_tx_receipt_with_timeout(
462487

463488
# eth_getStorageAt
464489

465-
_get_storage_at: Method[Callable[..., Awaitable[HexBytes]]] = Method(
490+
_get_storage_at: Method[
491+
Callable[
492+
[Union[Address, ChecksumAddress, ENS], int, Optional[BlockIdentifier]],
493+
Awaitable[HexBytes],
494+
]
495+
] = Method(
466496
RPC.eth_getStorageAt,
467497
mungers=[BaseEth.get_storage_at_munger],
468498
)

web3/eth/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ def generate_gas_price(
8989
return self._gas_price_strategy(self.w3, transaction_params)
9090
return None
9191

92-
def set_gas_price_strategy(self, gas_price_strategy: GasPriceStrategy) -> None:
92+
def set_gas_price_strategy(
93+
self, gas_price_strategy: Optional[GasPriceStrategy]
94+
) -> None:
9395
self._gas_price_strategy = gas_price_strategy
9496

9597
def estimate_gas_munger(

web3/eth/main.py

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
fee_history_priority_fee,
3838
)
3939
from web3._utils.filters import (
40+
Filter,
4041
select_filter_method,
4142
)
4243
from web3._utils.rpc_abi import (
@@ -164,7 +165,7 @@ def gas_price(self) -> Wei:
164165

165166
# eth_maxPriorityFeePerGas
166167

167-
_max_priority_fee: Method[Callable[..., Wei]] = Method(
168+
_max_priority_fee: Method[Callable[[], Wei]] = Method(
168169
RPC.eth_maxPriorityFeePerGas,
169170
is_property=True,
170171
)
@@ -209,9 +210,11 @@ def syncing(self) -> Union[SyncStatus, bool]:
209210

210211
# eth_feeHistory
211212

212-
_fee_history: Method[Callable[..., FeeHistory]] = Method(
213-
RPC.eth_feeHistory, mungers=[default_root_munger]
214-
)
213+
_fee_history: Method[
214+
Callable[
215+
[int, Union[BlockParams, BlockNumber], Optional[List[float]]], FeeHistory
216+
]
217+
] = Method(RPC.eth_feeHistory, mungers=[default_root_munger])
215218

216219
def fee_history(
217220
self,
@@ -223,9 +226,12 @@ def fee_history(
223226

224227
# eth_call
225228

226-
_call: Method[Callable[..., Union[bytes, bytearray]]] = Method(
227-
RPC.eth_call, mungers=[BaseEth.call_munger]
228-
)
229+
_call: Method[
230+
Callable[
231+
[TxParams, Optional[BlockIdentifier], Optional[CallOverride]],
232+
Union[bytes, bytearray],
233+
]
234+
] = Method(RPC.eth_call, mungers=[BaseEth.call_munger])
229235

230236
def call(
231237
self,
@@ -274,9 +280,9 @@ def _durin_call(
274280

275281
# eth_estimateGas
276282

277-
_estimate_gas: Method[Callable[..., int]] = Method(
278-
RPC.eth_estimateGas, mungers=[BaseEth.estimate_gas_munger]
279-
)
283+
_estimate_gas: Method[
284+
Callable[[TxParams, Optional[BlockIdentifier]], int]
285+
] = Method(RPC.eth_estimateGas, mungers=[BaseEth.estimate_gas_munger])
280286

281287
def estimate_gas(
282288
self, transaction: TxParams, block_identifier: Optional[BlockIdentifier] = None
@@ -366,7 +372,7 @@ def send_raw_transaction(self, transaction: Union[HexStr, bytes]) -> HexBytes:
366372
# eth_getBlockByHash
367373
# eth_getBlockByNumber
368374

369-
_get_block: Method[Callable[..., BlockData]] = Method(
375+
_get_block: Method[Callable[[BlockIdentifier, bool], BlockData]] = Method(
370376
method_choice_depends_on_args=select_method_for_block_identifier(
371377
if_predefined=RPC.eth_getBlockByNumber,
372378
if_hash=RPC.eth_getBlockByHash,
@@ -382,7 +388,9 @@ def get_block(
382388

383389
# eth_getBalance
384390

385-
_get_balance: Method[Callable[..., Wei]] = Method(
391+
_get_balance: Method[
392+
Callable[[Union[Address, ChecksumAddress, ENS], Optional[BlockIdentifier]], Wei]
393+
] = Method(
386394
RPC.eth_getBalance,
387395
mungers=[BaseEth.block_id_munger],
388396
)
@@ -396,9 +404,11 @@ def get_balance(
396404

397405
# eth_getCode
398406

399-
_get_code: Method[Callable[..., HexBytes]] = Method(
400-
RPC.eth_getCode, mungers=[BaseEth.block_id_munger]
401-
)
407+
_get_code: Method[
408+
Callable[
409+
[Union[Address, ChecksumAddress, ENS], Optional[BlockIdentifier]], HexBytes
410+
]
411+
] = Method(RPC.eth_getCode, mungers=[BaseEth.block_id_munger])
402412

403413
def get_code(
404414
self,
@@ -421,7 +431,11 @@ def get_logs(
421431

422432
# eth_getTransactionCount
423433

424-
_get_transaction_count: Method[Callable[..., Nonce]] = Method(
434+
_get_transaction_count: Method[
435+
Callable[
436+
[Union[Address, ChecksumAddress, ENS], Optional[BlockIdentifier]], Nonce
437+
]
438+
] = Method(
425439
RPC.eth_getTransactionCount,
426440
mungers=[BaseEth.block_id_munger],
427441
)
@@ -465,19 +479,13 @@ def wait_for_transaction_receipt(
465479

466480
# eth_getStorageAt
467481

468-
_get_storage_at: Method[Callable[..., HexBytes]] = Method(
482+
get_storage_at: Method[
483+
Callable[[Union[Address, ChecksumAddress, ENS], int], HexBytes]
484+
] = Method(
469485
RPC.eth_getStorageAt,
470486
mungers=[BaseEth.get_storage_at_munger],
471487
)
472488

473-
def get_storage_at(
474-
self,
475-
account: Union[Address, ChecksumAddress, ENS],
476-
position: int,
477-
block_identifier: Optional[BlockIdentifier] = None,
478-
) -> HexBytes:
479-
return self._get_storage_at(account, position, block_identifier)
480-
481489
# eth_getProof
482490

483491
def get_proof_munger(
@@ -563,10 +571,7 @@ def sign_munger(
563571
message_hex = to_hex(data, hexstr=hexstr, text=text)
564572
return (account, message_hex)
565573

566-
sign: Method[Callable[..., HexStr]] = Method(
567-
RPC.eth_sign,
568-
mungers=[sign_munger],
569-
)
574+
sign: Method[Callable[..., HexStr]] = Method(RPC.eth_sign, mungers=[sign_munger])
570575

571576
# eth_signTransaction
572577

@@ -577,7 +582,9 @@ def sign_munger(
577582

578583
# eth_signTypedData
579584

580-
sign_typed_data: Method[Callable[..., HexStr]] = Method(
585+
sign_typed_data: Method[
586+
Callable[[Union[Address, ChecksumAddress, ENS], str], HexStr]
587+
] = Method(
581588
RPC.eth_signTypedData,
582589
mungers=[default_root_munger],
583590
)
@@ -613,7 +620,7 @@ def filter_munger(
613620
"or hex."
614621
)
615622

616-
filter: Method[Callable[..., Any]] = Method(
623+
filter: Method[Callable[[Union[str, FilterParams, HexStr]], Filter]] = Method(
617624
method_choice_depends_on_args=select_filter_method(
618625
if_new_block_filter=RPC.eth_newBlockFilter,
619626
if_new_pending_transaction_filter=RPC.eth_newPendingTransactionFilter,

0 commit comments

Comments
 (0)