Skip to content

Commit f7b396f

Browse files
committed
Small improvements with typing using Generics
1 parent 1b38f9f commit f7b396f

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

matt/manager.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
from enum import Enum
1717
from io import BytesIO
18-
from typing import Callable, Dict, List, Optional, Tuple, Union
18+
from typing import Callable, Dict, Generic, List, Optional, Tuple, TypeVar, Union
19+
20+
from typing_extensions import TypeGuard
1921

2022
from .argtypes import SignerType
2123
from .btctools import script
@@ -68,14 +70,17 @@ class ContractInstanceStatus(Enum):
6870
SPENT = 2 # Already spent
6971

7072

71-
class ContractInstance:
73+
ContractT = TypeVar('ContractT', bound=Union[StandardP2TR, StandardAugmentedP2TR])
74+
75+
76+
class ContractInstance(Generic[ContractT]):
7277
"""
7378
Represents a specific instance of a Contract. It keeps track of:
7479
- the instance status
7580
- if augmented, the data embedded in the Contract instance.
7681
"""
7782

78-
def __init__(self, contract: Union[StandardP2TR, StandardAugmentedP2TR]):
83+
def __init__(self, contract: ContractT):
7984
"""
8085
Initializes a new ContractInstance with the given contract template.
8186
@@ -88,7 +93,7 @@ def __init__(self, contract: Union[StandardP2TR, StandardAugmentedP2TR]):
8893
self.data: Optional[bytes] = None
8994
self.data_expanded: Optional[ContractState] = None # TODO: figure out a good API for this
9095

91-
self.manager: ContractManager = None
96+
self.manager: Optional[ContractManager] = None
9297

9398
self.last_height = 0
9499

@@ -104,14 +109,14 @@ def __init__(self, contract: Union[StandardP2TR, StandardAugmentedP2TR]):
104109
# the new instances produced by spending this instance
105110
self.next: Optional[List[ContractInstance]] = None
106111

107-
def is_augm(self) -> bool:
112+
def is_augmented(self) -> TypeGuard['ContractInstance[StandardAugmentedP2TR]']:
108113
"""
109114
Checks if the Contract contained in this instance is augmented.
110115
111116
Returns:
112117
bool: True if the contract is augmented, False otherwise.
113118
"""
114-
return isinstance(self.contract, AugmentedP2TR) or isinstance(self.contract, StandardAugmentedP2TR)
119+
return isinstance(self.contract, StandardAugmentedP2TR)
115120

116121
def get_tr_info(self) -> TaprootInfo:
117122
"""
@@ -123,7 +128,7 @@ def get_tr_info(self) -> TaprootInfo:
123128
Raises:
124129
ValueError: If the contract is augmented but no data is set for the instance.
125130
"""
126-
if not self.is_augm():
131+
if not self.is_augmented():
127132
return self.contract.get_tr_info()
128133
else:
129134
if self.data is None:
@@ -168,7 +173,7 @@ def decode_wit_stack(self, stack_elems: List[bytes]) -> Tuple[str, dict]:
168173
Returns:
169174
Tuple[str, dict]: A tuple containing the name of the clause used and a dictionary of the arguments provided to the clause.
170175
"""
171-
if self.is_augm():
176+
if self.is_augmented():
172177
assert self.data is not None
173178

174179
return self.contract.decode_wit_stack(self.data, stack_elems)
@@ -292,7 +297,7 @@ def wait_for_outpoint(self, instance: ContractInstance, txid: Optional[str] = No
292297
"""
293298

294299
self._check_instance(instance, exp_statuses=ContractInstanceStatus.ABSTRACT)
295-
if instance.is_augm():
300+
if instance.is_augmented():
296301
if instance.data is None:
297302
raise ValueError("Data not set in instance")
298303
scriptPubKey = instance.contract.get_tr_info(instance.data).scriptPubKey
@@ -620,7 +625,7 @@ def wait_for_spend(self, instances: Union[ContractInstance, List[ContractInstanc
620625
self.add_instance(instance)
621626
return result
622627

623-
def fund_instance(self, contract: Union[StandardP2TR, StandardAugmentedP2TR], amount: int, data: Optional[ContractState] = None) -> ContractInstance:
628+
def fund_instance(self, contract: ContractT, amount: int, data: Optional[ContractState] = None) -> ContractInstance[ContractT]:
624629
"""
625630
Creates a new contract instance from a specified contract template, funds it with a specified amount of satoshis,
626631
and adds it to the manager.

pyproject.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ authors = [
1010
]
1111
description = "Merkleize All The Things"
1212
readme = "README.md"
13-
requires-python = ">=3.7"
13+
requires-python = ">=3.8"
1414
keywords = ["covenant", "smart contracts", "bitcoin"]
1515
license = { file = "LICENSE" }
16-
dependencies = []
16+
dependencies = [
17+
'typing_extensions >= 4.9.0'
18+
]

0 commit comments

Comments
 (0)