Skip to content

Commit 44eebd9

Browse files
jomuelrakanalh
authored andcommitted
Exclude runtime typechecks from unit tests
1 parent cc61aa8 commit 44eebd9

18 files changed

+114
-207
lines changed

.unittest-coveragerc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ exclude_lines =
55
if TYPE_CHECKING:
66
def __repr__
77
raise NotImplementedError
8+
assert not isinstance
9+
typecheck
810

911
[run]
1012
branch = True

raiden/api/python.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def get_transfer_from_task(
130130
transfer = transfer_task.mediator_state.waiting_transfer.transfer
131131
elif isinstance(transfer_task, TargetTask):
132132
transfer = transfer_task.target_state.transfer
133-
else:
133+
else: # pragma: no unittest
134134
raise ValueError("get_tranfer_from_task for a non TransferTask argument")
135135

136136
return transfer, role
@@ -160,9 +160,8 @@ def transfer_tasks_view(
160160
return view
161161

162162

163-
class RaidenAPI:
163+
class RaidenAPI: # pragma: no unittest
164164
# pylint: disable=too-many-public-methods
165-
# pragma: no unittest
166165

167166
def __init__(self, raiden):
168167
self.raiden = raiden
@@ -729,7 +728,7 @@ def transfer_async(
729728
current_state = views.state_from_raiden(self.raiden)
730729
payment_network_address = self.raiden.default_registry.address
731730

732-
if not isinstance(amount, int):
731+
if not isinstance(amount, int): # pragma: no unittest
733732
raise InvalidAmount("Amount not a number")
734733

735734
if amount <= 0:

raiden/encoding/encoders.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from raiden.utils.typing import typecheck
2+
13
__all__ = ("integer",)
24

35

@@ -10,8 +12,7 @@ def __init__(self, minimum: int, maximum: int):
1012

1113
def validate(self, value: int):
1214
""" Validates the integer is in the value range. """
13-
if not isinstance(value, int):
14-
raise ValueError("value is not an integer")
15+
typecheck(value, int)
1516

1617
if self.minimum > value or self.maximum < value:
1718
msg = ("{} is outside the valide range [{},{}]").format(

raiden/messages.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
TokenAmount,
6666
TokenNetworkAddress,
6767
Type,
68+
typecheck,
6869
)
6970

7071
__all__ = (
@@ -796,10 +797,7 @@ def __post_init__(self):
796797
def from_balance_proof_signed_state(
797798
cls, balance_proof: BalanceProofSignedState
798799
) -> "SignedBlindedBalanceProof":
799-
if not isinstance(balance_proof, BalanceProofSignedState):
800-
raise ValueError(
801-
"balance_proof is not an instance of the type BalanceProofSignedState"
802-
)
800+
typecheck(balance_proof, BalanceProofSignedState)
803801

804802
# pylint: disable=unexpected-keyword-arg
805803
return cls(
@@ -852,11 +850,7 @@ class RequestMonitoring(SignedMessage):
852850
non_closing_signature: Optional[Signature] = None
853851

854852
def __post_init__(self):
855-
if self.balance_proof is None:
856-
raise ValueError("no balance proof given")
857-
858-
if not isinstance(self.balance_proof, SignedBlindedBalanceProof):
859-
raise ValueError("onchain_balance_proof is not a SignedBlindedBalanceProof")
853+
typecheck(self.balance_proof, SignedBlindedBalanceProof)
860854

861855
@classmethod
862856
def from_balance_proof_signed_state(
@@ -865,10 +859,7 @@ def from_balance_proof_signed_state(
865859
reward_amount: TokenAmount,
866860
monitoring_service_contract_address: Address,
867861
) -> "RequestMonitoring":
868-
if not isinstance(balance_proof, BalanceProofSignedState):
869-
raise ValueError(
870-
"balance_proof is not an instance of the type BalanceProofSignedState"
871-
)
862+
typecheck(balance_proof, BalanceProofSignedState)
872863

873864
onchain_balance_proof = SignedBlindedBalanceProof.from_balance_proof_signed_state(
874865
balance_proof=balance_proof

raiden/network/blockchain_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
TokenAddress,
2424
TokenNetworkAddress,
2525
Tuple,
26+
typecheck,
2627
)
2728
from raiden_contracts.contract_manager import ContractManager
2829

@@ -199,8 +200,7 @@ def payment_channel(self, canonical_identifier: CanonicalIdentifier) -> PaymentC
199200

200201
if not is_binary_address(token_network_address):
201202
raise ValueError("address must be a valid address")
202-
if not isinstance(channel_id, T_ChannelID):
203-
raise ValueError("channel identifier must be of type T_ChannelID")
203+
typecheck(channel_id, T_ChannelID)
204204

205205
with self._payment_channel_creation_lock:
206206
dict_key = (token_network_address, channel_id)

raiden/network/proxies/token_network.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
TokenNetworkAddress,
7272
Tuple,
7373
cast,
74+
typecheck,
7475
)
7576
from raiden_contracts.constants import (
7677
CONTRACT_TOKEN_NETWORK,
@@ -420,7 +421,7 @@ def _detail_channel(
420421
participant2=participant2,
421422
block_identifier=block_identifier,
422423
)
423-
elif not isinstance(channel_identifier, T_ChannelID):
424+
elif not isinstance(channel_identifier, T_ChannelID): # pragma: no unittest
424425
raise InvalidChannelID("channel_identifier must be of type T_ChannelID")
425426
elif channel_identifier <= 0 or channel_identifier > UINT256_MAX:
426427
raise InvalidChannelID(
@@ -464,7 +465,7 @@ def detail_participants(
464465
participant2=participant2,
465466
block_identifier=block_identifier,
466467
)
467-
elif not isinstance(channel_identifier, T_ChannelID):
468+
elif not isinstance(channel_identifier, T_ChannelID): # pragma: no unittest
468469
raise InvalidChannelID("channel_identifier must be of type T_ChannelID")
469470
elif channel_identifier <= 0 or channel_identifier > UINT256_MAX:
470471
raise InvalidChannelID(
@@ -703,8 +704,7 @@ def set_total_deposit(
703704
RuntimeError: If the token address is empty.
704705
ValueError: If an argument is of the invalid type.
705706
"""
706-
if not isinstance(total_deposit, int):
707-
raise ValueError("total_deposit needs to be an integer number.")
707+
typecheck(total_deposit, int)
708708

709709
with self.channel_operations_lock[partner], self.deposit_lock:
710710
previous_total_deposit = self._detail_participant(
@@ -1840,8 +1840,7 @@ def _get_channel_state(
18401840
channel_identifier=channel_identifier,
18411841
)
18421842

1843-
if not isinstance(channel_data.state, T_ChannelState):
1844-
raise ValueError("channel state must be of type ChannelState")
1843+
typecheck(channel_data.state, T_ChannelState)
18451844

18461845
return channel_data.state
18471846

raiden/network/proxies/token_network_registry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
T_TargetAddress,
3636
TokenAddress,
3737
TokenAmount,
38+
typecheck,
3839
)
3940
from raiden_contracts.constants import CONTRACT_TOKEN_NETWORK_REGISTRY, EVENT_TOKEN_NETWORK_CREATED
4041
from raiden_contracts.contract_manager import ContractManager
@@ -82,8 +83,7 @@ def get_token_network(
8283
""" Return the token network address for the given token or None if
8384
there is no correspoding address.
8485
"""
85-
if not isinstance(token_address, T_TargetAddress):
86-
raise ValueError("token_address must be an address")
86+
typecheck(token_address, T_TargetAddress)
8787

8888
address = self.proxy.contract.functions.token_to_token_networks(
8989
to_checksum_address(token_address)

raiden/network/proxies/user_deposit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
TokenAddress,
2222
TokenAmount,
2323
Tuple,
24+
typecheck,
2425
)
2526
from raiden_contracts.constants import CONTRACT_USER_DEPOSIT, GAS_REQUIRED_FOR_UDC_DEPOSIT
2627
from raiden_contracts.contract_manager import ContractManager
@@ -158,8 +159,7 @@ def _deposit_preconditions(
158159
token: Token,
159160
block_identifier: BlockSpecification,
160161
) -> Tuple[TokenAmount, Dict]:
161-
if not isinstance(total_deposit, int):
162-
raise ValueError("total_deposit needs to be an integer number.")
162+
typecheck(total_deposit, int)
163163

164164
previous_total_deposit = self.get_total_deposit(
165165
address=beneficiary, block_identifier=block_identifier

raiden/tests/utils/transfer.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
TokenAddress,
4545
TokenAmount,
4646
TokenNetworkAddress,
47+
typecheck,
4748
)
4849

4950

@@ -513,8 +514,7 @@ def make_receive_transfer_mediated(
513514
chain_id: Optional[ChainID] = None,
514515
) -> LockedTransferSignedState:
515516

516-
if not isinstance(lock, HashTimeLockState):
517-
raise ValueError("lock must be of type HashTimeLockState")
517+
typecheck(lock, HashTimeLockState)
518518

519519
signer = LocalSigner(privkey)
520520
address = signer.address
@@ -584,8 +584,7 @@ def make_receive_expired_lock(
584584
chain_id: ChainID = None,
585585
) -> ReceiveLockExpired:
586586

587-
if not isinstance(lock, HashTimeLockState):
588-
raise ValueError("lock must be of type HashTimeLockState")
587+
typecheck(lock, HashTimeLockState)
589588

590589
signer = LocalSigner(privkey)
591590
address = signer.address

raiden/transfer/architecture.py

Lines changed: 16 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
TransactionHash,
3535
Tuple,
3636
TypeVar,
37+
typecheck,
3738
)
3839

3940
# Quick overview
@@ -159,8 +160,7 @@ class ContractSendEvent(Event):
159160
triggered_by_block_hash: BlockHash
160161

161162
def __post_init__(self) -> None:
162-
if not isinstance(self.triggered_by_block_hash, T_BlockHash):
163-
raise ValueError("triggered_by_block_hash must be of type block_hash")
163+
typecheck(self.triggered_by_block_hash, T_BlockHash)
164164

165165

166166
@dataclass
@@ -181,10 +181,8 @@ class ContractReceiveStateChange(StateChange):
181181
block_hash: BlockHash
182182

183183
def __post_init__(self) -> None:
184-
if not isinstance(self.block_number, T_BlockNumber):
185-
raise ValueError("block_number must be of type block_number")
186-
if not isinstance(self.block_hash, T_BlockHash):
187-
raise ValueError("block_hash must be of type block_hash")
184+
typecheck(self.block_number, T_BlockNumber)
185+
typecheck(self.block_hash, T_BlockHash)
188186

189187

190188
ST = TypeVar("ST", bound=State)
@@ -208,7 +206,7 @@ def __init__(
208206
state_transition: function that can apply a StateChange message.
209207
current_state: current application state.
210208
"""
211-
if not callable(state_transition):
209+
if not callable(state_transition): # pragma: no unittest
212210
raise ValueError("state_transition must be a callable")
213211

214212
self.state_transition = state_transition
@@ -291,17 +289,10 @@ class BalanceProofUnsignedState(State):
291289
balance_hash: BalanceHash = field(default=EMPTY_BALANCE_HASH)
292290

293291
def __post_init__(self) -> None:
294-
if not isinstance(self.nonce, int):
295-
raise ValueError("nonce must be int")
296-
297-
if not isinstance(self.transferred_amount, T_TokenAmount):
298-
raise ValueError("transferred_amount must be a token_amount instance")
299-
300-
if not isinstance(self.locked_amount, T_TokenAmount):
301-
raise ValueError("locked_amount must be a token_amount instance")
302-
303-
if not isinstance(self.locksroot, T_Keccak256):
304-
raise ValueError("locksroot must be a keccak256 instance")
292+
typecheck(self.nonce, int)
293+
typecheck(self.transferred_amount, T_TokenAmount)
294+
typecheck(self.locked_amount, T_TokenAmount)
295+
typecheck(self.locksroot, T_Keccak256)
305296

306297
if self.nonce <= 0:
307298
raise ValueError("nonce cannot be zero or negative")
@@ -356,26 +347,13 @@ class BalanceProofSignedState(State):
356347
balance_hash: BalanceHash = field(default=EMPTY_BALANCE_HASH)
357348

358349
def __post_init__(self) -> None:
359-
if not isinstance(self.nonce, int):
360-
raise ValueError("nonce must be int")
361-
362-
if not isinstance(self.transferred_amount, T_TokenAmount):
363-
raise ValueError("transferred_amount must be a token_amount instance")
364-
365-
if not isinstance(self.locked_amount, T_TokenAmount):
366-
raise ValueError("locked_amount must be a token_amount instance")
367-
368-
if not isinstance(self.locksroot, T_Keccak256):
369-
raise ValueError("locksroot must be a keccak256 instance")
370-
371-
if not isinstance(self.message_hash, T_Keccak256):
372-
raise ValueError("message_hash must be a keccak256 instance")
373-
374-
if not isinstance(self.signature, T_Signature):
375-
raise ValueError("signature must be a signature instance")
376-
377-
if not isinstance(self.sender, T_Address):
378-
raise ValueError("sender must be an address instance")
350+
typecheck(self.nonce, int)
351+
typecheck(self.transferred_amount, T_TokenAmount)
352+
typecheck(self.locked_amount, T_TokenAmount)
353+
typecheck(self.locksroot, T_Keccak256)
354+
typecheck(self.message_hash, T_Keccak256)
355+
typecheck(self.signature, T_Signature)
356+
typecheck(self.sender, T_Address)
379357

380358
if self.nonce <= 0:
381359
raise ValueError("nonce cannot be zero or negative")

0 commit comments

Comments
 (0)