-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Refactor for duplicate code in contract internal implementations #3579
base: main
Are you sure you want to change the base?
Conversation
11b15fa
to
02d1a5e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These updates are looking great, thanks for your contributions. I've mentioned a few changes to additional cleanup and suggested exception messages. Should be good to go after that!
web3/contract/base_contract.py
Outdated
] | ||
|
||
# Check that arguments in call match a function ABI | ||
num_attempts = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe these num_attempts
variables can be removed. They were mistakenly left in from a previous change.
@@ -785,6 +938,46 @@ def __hasattr__(self, function_name: str) -> bool: | |||
except ABIFunctionNotFound: | |||
return False | |||
|
|||
def __iter__(self) -> Iterable[TContractFn]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The corresponding function __iter__
in ContractFunction
still exists and should be removed.
for func in self._functions: | ||
yield self[abi_to_signature(func)] | ||
|
||
def __getattr__(self, function_name: str) -> TContractFn: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The corresponding function __getattr__
in ContractFunction
still exists and should be removed.
function_name, | ||
) | ||
|
||
def __getitem__(self, function_name: str) -> TContractFn: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The corresponding function __getitem__
in ContractFunction
still exists and should be removed.
web3/contract/base_contract.py
Outdated
ccip_read_enabled: Optional[bool] = None, | ||
) -> Any: | ||
""" | ||
Should be implemented by child class. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be implemented by child class. | |
Implementation of ``call`` should create a callable contract function and execute it using the `eth_call` interface. |
@@ -122,7 +106,7 @@ class AsyncContractEvent(BaseContractEvent): | |||
# mypy types | |||
w3: "AsyncWeb3" | |||
|
|||
def __call__(self, *args: Any, **kwargs: Any) -> "AsyncContractEvent": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm inclined to leave this as is since the returned object is a new copy of the event.
def __call__(self, *args: Any, **kwargs: Any) -> "AsyncContractEvent": | |
def __call__(self, *args: Any, **kwargs: Any) -> "AsyncContractEvent": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they are nearly the same and Self can keep consistency. Self is used to indicate type hints, but did not imply if it is self
or a copy of it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fine with me. Should this __call__
and the one in the ContractEvent
class move to base_contract? If not, I noticed the ContractEvent.__call__
has the type "ContractEvent"
rather than self
, probably best to make them consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Moved into BaseContractEvent
Co-authored-by: Stuart Reed <[email protected]>
Co-authored-by: Stuart Reed <[email protected]>
@reedsa Thanks for reviewing. Changes already made except for |
web3/contract/base_contract.py
Outdated
contract_function = None | ||
for abi in function_abis_with_arg_count: | ||
try: | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't need pass
here.
@reedsa I think the above mentioned issues are resolved now |
What was wrong?
Closes #3561
In internal contract implementations, there might be duplicate implementations repeating same code for async and non-async functions, for example,
ContractFunctions
andAsyncContractFunctions
.How was it fixed?
This case is because of:
As for previous one, they can be resolved using self.class to other techiniques to dynamically get the right class.
As for latter one,
Self
combined withGeneric
would help solve the issueTodo:
Cute Animal Picture