Skip to content

Commit 32ef26e

Browse files
committed
Tighten up some typing in Eth and AsyncEth methods
1 parent 53658a4 commit 32ef26e

File tree

4 files changed

+92
-53
lines changed

4 files changed

+92
-53
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

@@ -1679,7 +1680,7 @@ def test_eth_get_storage_at_ens_name(
16791680
self, w3: "Web3", emitter_contract_address: ChecksumAddress
16801681
) -> None:
16811682
with ens_addresses(w3, {"emitter.eth": emitter_contract_address}):
1682-
storage = w3.eth.get_storage_at("emitter.eth", 0)
1683+
storage = w3.eth.get_storage_at(ENS("emitter.eth"), 0)
16831684
assert isinstance(storage, HexBytes)
16841685

16851686
def test_eth_get_storage_at_invalid_address(self, w3: "Web3") -> None:
@@ -1703,7 +1704,7 @@ def test_eth_get_transaction_count_ens_name(
17031704
w3, {"unlocked-acct-dual-type.eth": unlocked_account_dual_type}
17041705
):
17051706
transaction_count = w3.eth.get_transaction_count(
1706-
"unlocked-acct-dual-type.eth"
1707+
ENS("unlocked-acct-dual-type.eth")
17071708
)
17081709
assert is_integer(transaction_count)
17091710
assert transaction_count >= 0
@@ -1774,7 +1775,7 @@ def test_eth_get_code_ens_address(
17741775
self, w3: "Web3", math_contract_address: ChecksumAddress
17751776
) -> None:
17761777
with ens_addresses(w3, {"mathcontract.eth": math_contract_address}):
1777-
code = w3.eth.get_code("mathcontract.eth")
1778+
code = w3.eth.get_code(ENS("mathcontract.eth"))
17781779
assert isinstance(code, HexBytes)
17791780
assert len(code) > 0
17801781

web3/eth/async_eth.py

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

145145
# eth_maxPriorityFeePerGas
146146

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

190190
# eth_feeHistory
191191

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

196199
async def fee_history(
197200
self,
@@ -201,12 +204,19 @@ async def fee_history(
201204
) -> FeeHistory:
202205
return await self._fee_history(block_count, newest_block, reward_percentiles)
203206

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

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

256266
# eth_estimateGas
257267

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

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

361-
_get_block: Method[Callable[..., Awaitable[BlockData]]] = Method(
371+
_get_block: Method[
372+
Callable[[BlockIdentifier, bool], Awaitable[BlockData]]
373+
] = Method(
362374
method_choice_depends_on_args=select_method_for_block_identifier(
363375
if_predefined=RPC.eth_getBlockByNumber,
364376
if_hash=RPC.eth_getBlockByHash,
@@ -374,7 +386,12 @@ async def get_block(
374386

375387
# eth_getBalance
376388

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

389406
# eth_getCode
390407

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

395415
async def get_code(
396416
self,
@@ -413,7 +433,12 @@ async def get_logs(
413433

414434
# eth_getTransactionCount
415435

416-
_get_transaction_count: Method[Callable[..., Awaitable[Nonce]]] = Method(
436+
_get_transaction_count: Method[
437+
Callable[
438+
[Union[Address, ChecksumAddress, ENS], Optional[BlockIdentifier]],
439+
Awaitable[Nonce],
440+
]
441+
] = Method(
417442
RPC.eth_getTransactionCount,
418443
mungers=[BaseEth.block_id_munger],
419444
)
@@ -463,7 +488,12 @@ async def _wait_for_tx_receipt_with_timeout(
463488

464489
# eth_getStorageAt
465490

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

web3/eth/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ def generate_gas_price(
9393
return self._gas_price_strategy(self.w3, transaction_params)
9494
return None
9595

96-
def set_gas_price_strategy(self, gas_price_strategy: GasPriceStrategy) -> None:
96+
def set_gas_price_strategy(
97+
self, gas_price_strategy: Optional[GasPriceStrategy]
98+
) -> None:
9799
self._gas_price_strategy = gas_price_strategy
98100

99101
def estimate_gas_munger(

web3/eth/main.py

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def gas_price(self) -> Wei:
164164

165165
# eth_maxPriorityFeePerGas
166166

167-
_max_priority_fee: Method[Callable[..., Wei]] = Method(
167+
_max_priority_fee: Method[Callable[[], Wei]] = Method(
168168
RPC.eth_maxPriorityFeePerGas,
169169
is_property=True,
170170
)
@@ -209,9 +209,11 @@ def syncing(self) -> Union[SyncStatus, bool]:
209209

210210
# eth_feeHistory
211211

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

216218
def fee_history(
217219
self,
@@ -223,9 +225,12 @@ def fee_history(
223225

224226
# eth_call
225227

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

230235
def call(
231236
self,
@@ -274,9 +279,9 @@ def _durin_call(
274279

275280
# eth_estimateGas
276281

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

281286
def estimate_gas(
282287
self, transaction: TxParams, block_identifier: Optional[BlockIdentifier] = None
@@ -366,7 +371,7 @@ def send_raw_transaction(self, transaction: Union[HexStr, bytes]) -> HexBytes:
366371
# eth_getBlockByHash
367372
# eth_getBlockByNumber
368373

369-
_get_block: Method[Callable[..., BlockData]] = Method(
374+
_get_block: Method[Callable[[BlockIdentifier, bool], BlockData]] = Method(
370375
method_choice_depends_on_args=select_method_for_block_identifier(
371376
if_predefined=RPC.eth_getBlockByNumber,
372377
if_hash=RPC.eth_getBlockByHash,
@@ -382,7 +387,9 @@ def get_block(
382387

383388
# eth_getBalance
384389

385-
_get_balance: Method[Callable[..., Wei]] = Method(
390+
_get_balance: Method[
391+
Callable[[Union[Address, ChecksumAddress, ENS], Optional[BlockIdentifier]], Wei]
392+
] = Method(
386393
RPC.eth_getBalance,
387394
mungers=[BaseEth.block_id_munger],
388395
)
@@ -396,9 +403,11 @@ def get_balance(
396403

397404
# eth_getCode
398405

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

403412
def get_code(
404413
self,
@@ -421,7 +430,11 @@ def get_logs(
421430

422431
# eth_getTransactionCount
423432

424-
_get_transaction_count: Method[Callable[..., Nonce]] = Method(
433+
_get_transaction_count: Method[
434+
Callable[
435+
[Union[Address, ChecksumAddress, ENS], Optional[BlockIdentifier]], Nonce
436+
]
437+
] = Method(
425438
RPC.eth_getTransactionCount,
426439
mungers=[BaseEth.block_id_munger],
427440
)
@@ -465,19 +478,13 @@ def wait_for_transaction_receipt(
465478

466479
# eth_getStorageAt
467480

468-
_get_storage_at: Method[Callable[..., HexBytes]] = Method(
481+
get_storage_at: Method[
482+
Callable[[Union[Address, ChecksumAddress, ENS], int], HexBytes]
483+
] = Method(
469484
RPC.eth_getStorageAt,
470485
mungers=[BaseEth.get_storage_at_munger],
471486
)
472487

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-
481488
# eth_getProof
482489

483490
def get_proof_munger(
@@ -563,10 +570,7 @@ def sign_munger(
563570
message_hex = to_hex(data, hexstr=hexstr, text=text)
564571
return (account, message_hex)
565572

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

571575
# eth_signTransaction
572576

@@ -577,7 +581,9 @@ def sign_munger(
577581

578582
# eth_signTypedData
579583

580-
sign_typed_data: Method[Callable[..., HexStr]] = Method(
584+
sign_typed_data: Method[
585+
Callable[[Union[Address, ChecksumAddress, ENS], str], HexStr]
586+
] = Method(
581587
RPC.eth_signTypedData,
582588
mungers=[default_root_munger],
583589
)

0 commit comments

Comments
 (0)