Skip to content

Commit

Permalink
Makes use of Self Type in Contract factory methods
Browse files Browse the repository at this point in the history
1. Uses the recently added `Self` Type for better type inference whenever Contract is subclassed

2. Fixes the return type of `Contract.factory` to indicate that a class is returned instead of an instance thereof
  • Loading branch information
konichuvak committed Jun 22, 2023
1 parent 5c6c657 commit 82beef8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
14 changes: 9 additions & 5 deletions web3/contract/async_contract.py
Expand Up @@ -9,6 +9,7 @@
List,
Optional,
Sequence,
Type,
cast,
)

Expand All @@ -33,6 +34,9 @@
from web3._utils.async_transactions import (
fill_transaction_defaults as async_fill_transaction_defaults,
)
from web3._utils.compat import (
Self,
)
from web3._utils.contracts import (
async_parse_block_identifier,
parse_block_identifier_no_extra_call,
Expand Down Expand Up @@ -239,7 +243,7 @@ def __call__(self, *args: Any, **kwargs: Any) -> "AsyncContractFunction":
return clone

@classmethod
def factory(cls, class_name: str, **kwargs: Any) -> "AsyncContractFunction":
def factory(cls, class_name: str, **kwargs: Any) -> Self:
return PropertyCheckingFactory(class_name, (cls,), kwargs)(kwargs.get("abi"))

async def call(
Expand Down Expand Up @@ -449,7 +453,7 @@ def __init__(self, address: Optional[ChecksumAddress] = None) -> None:
@classmethod
def factory(
cls, w3: "AsyncWeb3", class_name: Optional[str] = None, **kwargs: Any
) -> "AsyncContract":
) -> Type[Self]:
kwargs["w3"] = w3

normalizers = {
Expand All @@ -460,7 +464,7 @@ def factory(
}

contract = cast(
AsyncContract,
Type[Self],
PropertyCheckingFactory(
class_name or cls.__name__,
(cls,),
Expand Down Expand Up @@ -491,7 +495,7 @@ def factory(
return contract

@classmethod
def constructor(cls, *args: Any, **kwargs: Any) -> "AsyncContractConstructor":
def constructor(cls, *args: Any, **kwargs: Any) -> Self:
"""
:param args: The contract constructor arguments as positional arguments
:param kwargs: The contract constructor arguments as keyword arguments
Expand Down Expand Up @@ -549,7 +553,7 @@ def __init__(

self._functions = filter_by_type("function", self.abi)
for func in self._functions:
fn: AsyncContractFunction = AsyncContractFunction.factory(
fn = AsyncContractFunction.factory(
func["name"],
w3=self.w3,
contract_abi=self.abi,
Expand Down
14 changes: 9 additions & 5 deletions web3/contract/contract.py
Expand Up @@ -8,6 +8,7 @@
List,
Optional,
Sequence,
Type,
cast,
)

Expand All @@ -29,6 +30,9 @@
filter_by_type,
receive_func_abi_exists,
)
from web3._utils.compat import (
Self,
)
from web3._utils.contracts import (
parse_block_identifier,
)
Expand Down Expand Up @@ -235,7 +239,7 @@ def __call__(self, *args: Any, **kwargs: Any) -> "ContractFunction":
return clone

@classmethod
def factory(cls, class_name: str, **kwargs: Any) -> "ContractFunction":
def factory(cls, class_name: str, **kwargs: Any) -> Self:
return PropertyCheckingFactory(class_name, (cls,), kwargs)(kwargs.get("abi"))

def call(
Expand Down Expand Up @@ -448,7 +452,7 @@ def __init__(self, address: Optional[ChecksumAddress] = None) -> None:
@classmethod
def factory(
cls, w3: "Web3", class_name: Optional[str] = None, **kwargs: Any
) -> "Contract":
) -> Type[Self]:
kwargs["w3"] = w3

normalizers = {
Expand All @@ -459,7 +463,7 @@ def factory(
}

contract = cast(
Contract,
Type[Self],
PropertyCheckingFactory(
class_name or cls.__name__,
(cls,),
Expand Down Expand Up @@ -491,7 +495,7 @@ def factory(
return contract

@classmethod
def constructor(cls, *args: Any, **kwargs: Any) -> "ContractConstructor":
def constructor(cls, *args: Any, **kwargs: Any) -> Self:
"""
:param args: The contract constructor arguments as positional arguments
:param kwargs: The contract constructor arguments as keyword arguments
Expand Down Expand Up @@ -549,7 +553,7 @@ def __init__(

self._functions = filter_by_type("function", self.abi)
for func in self._functions:
fn: ContractFunction = ContractFunction.factory(
fn = ContractFunction.factory(
func["name"],
w3=self.w3,
contract_abi=self.abi,
Expand Down

0 comments on commit 82beef8

Please sign in to comment.