Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use TypedDict for **kwargs type checking #2849

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions web3/_utils/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
from typing_extensions import (
NotRequired, # py311
Self, # py311
Unpack, # py311
)
4 changes: 2 additions & 2 deletions web3/_utils/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,8 @@ def deploy(self, w3: "Web3") -> "LogFilter":
if not isinstance(w3, web3.Web3):
raise Web3ValueError(f"Invalid web3 argument: got: {w3!r}")

for arg in AttributeDict.values(self.args):
arg._immutable = True # type: ignore[attr-defined]
for arg in self.args.values():
arg._immutable = True
self._immutable = True

log_filter = cast("LogFilter", w3.eth.filter(self.filter_params))
Expand Down
16 changes: 8 additions & 8 deletions web3/_utils/module_testing/eth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ async def test_eth_modify_transaction_legacy(
txn_hash = await async_w3.eth.send_transaction(txn_params)

modified_txn_hash = await async_w3.eth.modify_transaction(
txn_hash, gasPrice=(cast(int, txn_params["gasPrice"]) * 2), value=2
txn_hash, gasPrice=(cast(Wei, txn_params["gasPrice"] * 2)), value=Wei(2)
)
modified_txn = await async_w3.eth.get_transaction(modified_txn_hash)

Expand Down Expand Up @@ -252,9 +252,9 @@ async def test_eth_modify_transaction(

modified_txn_hash = await async_w3.eth.modify_transaction(
txn_hash,
value=2,
maxPriorityFeePerGas=(cast(Wei, txn_params["maxPriorityFeePerGas"]) * 2),
maxFeePerGas=(cast(Wei, txn_params["maxFeePerGas"]) * 2),
value=Wei(2),
maxPriorityFeePerGas=(cast(Wei, txn_params["maxPriorityFeePerGas"] * 2)),
maxFeePerGas=(cast(Wei, txn_params["maxFeePerGas"] * 2)),
)
modified_txn = await async_w3.eth.get_transaction(modified_txn_hash)

Expand Down Expand Up @@ -3657,7 +3657,7 @@ def test_eth_modify_transaction_legacy(
txn_hash = w3.eth.send_transaction(txn_params)

modified_txn_hash = w3.eth.modify_transaction(
txn_hash, gasPrice=(cast(int, txn_params["gasPrice"]) * 2), value=2
txn_hash, gasPrice=(cast(Wei, txn_params["gasPrice"] * 2)), value=Wei(2)
)
modified_txn = w3.eth.get_transaction(modified_txn_hash)

Expand Down Expand Up @@ -3686,9 +3686,9 @@ def test_eth_modify_transaction(

modified_txn_hash = w3.eth.modify_transaction(
txn_hash,
value=2,
maxPriorityFeePerGas=(cast(Wei, txn_params["maxPriorityFeePerGas"]) * 2),
maxFeePerGas=(cast(Wei, txn_params["maxFeePerGas"]) * 2),
value=Wei(2),
maxPriorityFeePerGas=(cast(Wei, txn_params["maxPriorityFeePerGas"] * 2)),
maxFeePerGas=(cast(Wei, txn_params["maxFeePerGas"] * 2)),
)
modified_txn = w3.eth.get_transaction(modified_txn_hash)

Expand Down
7 changes: 4 additions & 3 deletions web3/eth/async_eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
from web3._utils.blocks import (
select_method_for_block_identifier,
)
from web3._utils.compat import (
Unpack,
)
from web3._utils.fee_utils import (
async_fee_history_priority_fee,
)
Expand Down Expand Up @@ -594,10 +597,8 @@ async def replace_transaction(
self.w3, current_transaction, new_transaction
)

# todo: Update Any to stricter kwarg checking with TxParams
# https://github.com/python/mypy/issues/4441
async def modify_transaction(
self, transaction_hash: _Hash32, **transaction_params: Any
self, transaction_hash: _Hash32, **transaction_params: Unpack[TxParams]
) -> HexBytes:
assert_valid_transaction_params(cast(TxParams, transaction_params))

Expand Down
7 changes: 4 additions & 3 deletions web3/eth/eth.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
from web3._utils.blocks import (
select_method_for_block_identifier,
)
from web3._utils.compat import (
Unpack,
)
from web3._utils.fee_utils import (
fee_history_priority_fee,
)
Expand Down Expand Up @@ -596,10 +599,8 @@ def replace_transaction(
current_transaction = get_required_transaction(self.w3, transaction_hash)
return replace_transaction(self.w3, current_transaction, new_transaction)

# todo: Update Any to stricter kwarg checking with TxParams
# https://github.com/python/mypy/issues/4441
def modify_transaction(
self, transaction_hash: _Hash32, **transaction_params: Any
self, transaction_hash: _Hash32, **transaction_params: Unpack[TxParams]
) -> HexBytes:
assert_valid_transaction_params(cast(TxParams, transaction_params))
current_transaction = get_required_transaction(self.w3, transaction_hash)
Expand Down