Skip to content

Commit 180a57d

Browse files
committed
Tighten up some typing in Eth and AsyncEth methods
1 parent f45b3cf commit 180a57d

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
@@ -165,7 +165,7 @@ def gas_price(self) -> Wei:
165165

166166
# eth_maxPriorityFeePerGas
167167

168-
_max_priority_fee: Method[Callable[..., Wei]] = Method(
168+
_max_priority_fee: Method[Callable[[], Wei]] = Method(
169169
RPC.eth_maxPriorityFeePerGas,
170170
is_property=True,
171171
)
@@ -210,9 +210,11 @@ def syncing(self) -> Union[SyncStatus, bool]:
210210

211211
# eth_feeHistory
212212

213-
_fee_history: Method[Callable[..., FeeHistory]] = Method(
214-
RPC.eth_feeHistory, mungers=[default_root_munger]
215-
)
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])
216218

217219
def fee_history(
218220
self,
@@ -224,9 +226,12 @@ def fee_history(
224226

225227
# eth_call
226228

227-
_call: Method[Callable[..., Union[bytes, bytearray]]] = Method(
228-
RPC.eth_call, mungers=[BaseEth.call_munger]
229-
)
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])
230235

231236
def call(
232237
self,
@@ -275,9 +280,9 @@ def _durin_call(
275280

276281
# eth_estimateGas
277282

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

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

370-
_get_block: Method[Callable[..., BlockData]] = Method(
375+
_get_block: Method[Callable[[BlockIdentifier, bool], BlockData]] = Method(
371376
method_choice_depends_on_args=select_method_for_block_identifier(
372377
if_predefined=RPC.eth_getBlockByNumber,
373378
if_hash=RPC.eth_getBlockByHash,
@@ -383,7 +388,9 @@ def get_block(
383388

384389
# eth_getBalance
385390

386-
_get_balance: Method[Callable[..., Wei]] = Method(
391+
_get_balance: Method[
392+
Callable[[Union[Address, ChecksumAddress, ENS], Optional[BlockIdentifier]], Wei]
393+
] = Method(
387394
RPC.eth_getBalance,
388395
mungers=[BaseEth.block_id_munger],
389396
)
@@ -397,9 +404,11 @@ def get_balance(
397404

398405
# eth_getCode
399406

400-
_get_code: Method[Callable[..., HexBytes]] = Method(
401-
RPC.eth_getCode, mungers=[BaseEth.block_id_munger]
402-
)
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])
403412

404413
def get_code(
405414
self,
@@ -422,7 +431,11 @@ def get_logs(
422431

423432
# eth_getTransactionCount
424433

425-
_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(
426439
RPC.eth_getTransactionCount,
427440
mungers=[BaseEth.block_id_munger],
428441
)
@@ -466,19 +479,13 @@ def wait_for_transaction_receipt(
466479

467480
# eth_getStorageAt
468481

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

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

484491
def get_proof_munger(
@@ -564,10 +571,7 @@ def sign_munger(
564571
message_hex = to_hex(data, hexstr=hexstr, text=text)
565572
return (account, message_hex)
566573

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

572576
# eth_signTransaction
573577

@@ -578,7 +582,9 @@ def sign_munger(
578582

579583
# eth_signTypedData
580584

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

0 commit comments

Comments
 (0)