Skip to content

Use WalletProtocol to constrain wallet function type signatures #14991

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion chia/data_layer/data_layer_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
from chia.wallet.wallet import Wallet
from chia.wallet.wallet_coin_record import WalletCoinRecord
from chia.wallet.wallet_info import WalletInfo
from chia.wallet.wallet_protocol import WalletProtocol

if TYPE_CHECKING:
from chia.wallet.wallet_state_manager import WalletStateManager
Expand Down Expand Up @@ -102,7 +103,7 @@ def from_json_dict(cls, json_dict: Dict[str, Any]) -> "Mirror":


@final
class DataLayerWallet:
class DataLayerWallet(WalletProtocol):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to repeat what kyle said. This is not how you declare that you implement a protocol, this is how you declare a new protocol that subsumes another.

My understanding is that whether a type implements a protocol is essentially duck-typing in mypy

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also known as structural subtyping, in case you see that floating around. i still intend to get a smaller pattern working someday but at the moment mypy doesn't provide verbose output clarifying in what way the protocol is not satisfied when you do it my 'better' way.

wallet_state_manager: WalletStateManager
log: logging.Logger
wallet_info: WalletInfo
Expand Down
3 changes: 2 additions & 1 deletion chia/pools/pool_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@
from chia.wallet.wallet import Wallet
from chia.wallet.wallet_coin_record import WalletCoinRecord
from chia.wallet.wallet_info import WalletInfo
from chia.wallet.wallet_protocol import WalletProtocol


@final
@dataclasses.dataclass
class PoolWallet:
class PoolWallet(WalletProtocol):
MINIMUM_INITIAL_BALANCE = 1
MINIMUM_RELATIVE_LOCK_HEIGHT = 5
MAXIMUM_RELATIVE_LOCK_HEIGHT = 1000
Expand Down
3 changes: 2 additions & 1 deletion chia/wallet/cat_wallet/cat_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
from chia.wallet.wallet import Wallet
from chia.wallet.wallet_coin_record import WalletCoinRecord
from chia.wallet.wallet_info import WalletInfo
from chia.wallet.wallet_protocol import WalletProtocol

if TYPE_CHECKING:
from chia.wallet.wallet_state_manager import WalletStateManager
Expand All @@ -66,7 +67,7 @@
QUOTED_MOD_HASH = calculate_hash_of_quoted_mod_hash(CAT_MOD_HASH)


class CATWallet:
class CATWallet(WalletProtocol):
wallet_state_manager: WalletStateManager
log: logging.Logger
wallet_info: WalletInfo
Expand Down
3 changes: 2 additions & 1 deletion chia/wallet/did_wallet/did_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@
from chia.wallet.wallet import CHIP_0002_SIGN_MESSAGE_PREFIX, Wallet
from chia.wallet.wallet_coin_record import WalletCoinRecord
from chia.wallet.wallet_info import WalletInfo
from chia.wallet.wallet_protocol import WalletProtocol


class DIDWallet:
class DIDWallet(WalletProtocol):
wallet_state_manager: Any
log: logging.Logger
wallet_info: WalletInfo
Expand Down
3 changes: 2 additions & 1 deletion chia/wallet/nft_wallet/nft_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@
from chia.wallet.wallet_coin_record import WalletCoinRecord
from chia.wallet.wallet_info import WalletInfo
from chia.wallet.wallet_nft_store import WalletNftStore
from chia.wallet.wallet_protocol import WalletProtocol

_T_NFTWallet = TypeVar("_T_NFTWallet", bound="NFTWallet")


class NFTWallet:
class NFTWallet(WalletProtocol):
wallet_state_manager: Any
log: logging.Logger
wallet_info: WalletInfo
Expand Down
3 changes: 2 additions & 1 deletion chia/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from chia.wallet.util.wallet_types import AmountWithPuzzlehash, WalletType
from chia.wallet.wallet_coin_record import WalletCoinRecord
from chia.wallet.wallet_info import WalletInfo
from chia.wallet.wallet_protocol import WalletProtocol

if TYPE_CHECKING:
from chia.server.ws_connection import WSChiaConnection
Expand All @@ -54,7 +55,7 @@
CHIP_0002_SIGN_MESSAGE_PREFIX = "Chia Signed Message"


class Wallet:
class Wallet(WalletProtocol):
wallet_info: WalletInfo
wallet_state_manager: Any
log: logging.Logger
Expand Down