From a5e83c0a1e22b76344816098054eeb87723272e5 Mon Sep 17 00:00:00 2001 From: Martin Kourim Date: Fri, 7 Nov 2025 14:52:37 +0100 Subject: [PATCH] refactor: replace AssertionError with ValueError Replaces all instances of raising AssertionError with ValueError for invalid argument or state errors across the codebase. This improves clarity and aligns with Python's standard exception usage for value validation. FileNotFoundError is used where appropriate for missing files. No functional changes to logic or error messages. --- cardano_clusterlib/address_group.py | 4 ++-- cardano_clusterlib/clusterlib_helpers.py | 2 +- cardano_clusterlib/gov_action_group.py | 8 ++++---- cardano_clusterlib/gov_committee_group.py | 6 +++--- cardano_clusterlib/gov_drep_group.py | 6 +++--- cardano_clusterlib/gov_group.py | 2 +- cardano_clusterlib/gov_vote_group.py | 10 +++++----- cardano_clusterlib/query_group.py | 4 ++-- cardano_clusterlib/stake_address_group.py | 10 +++++----- cardano_clusterlib/stake_pool_group.py | 2 +- cardano_clusterlib/transaction_group.py | 20 ++++++++++---------- cardano_clusterlib/txtools.py | 8 ++++---- 12 files changed, 41 insertions(+), 41 deletions(-) diff --git a/cardano_clusterlib/address_group.py b/cardano_clusterlib/address_group.py index 0db9613..63874e8 100644 --- a/cardano_clusterlib/address_group.py +++ b/cardano_clusterlib/address_group.py @@ -56,7 +56,7 @@ def gen_payment_addr( cli_args = ["--payment-verification-key", str(payment_vkey)] else: msg = "Either `payment_vkey_file`, `payment_script_file` or `payment_vkey` is needed." - raise AssertionError(msg) + raise ValueError(msg) if stake_vkey: cli_args.extend(["--stake-verification-key", str(stake_vkey)]) @@ -137,7 +137,7 @@ def get_payment_vkey_hash( cli_args = ["--payment-verification-key-file", str(payment_vkey_file)] else: msg = "Either `payment_vkey` or `payment_vkey_file` is needed." - raise AssertionError(msg) + raise ValueError(msg) return ( self._clusterlib_obj.cli(["address", "key-hash", *cli_args]) diff --git a/cardano_clusterlib/clusterlib_helpers.py b/cardano_clusterlib/clusterlib_helpers.py index 7965a51..d2a7201 100644 --- a/cardano_clusterlib/clusterlib_helpers.py +++ b/cardano_clusterlib/clusterlib_helpers.py @@ -160,7 +160,7 @@ def get_epoch_for_slot(cluster_obj: "itp.ClusterLib", slot_no: int) -> EpochInfo genesis_byron = cluster_obj.state_dir / "byron" / "genesis.json" if not genesis_byron.exists(): msg = f"File '{genesis_byron}' does not exist." - raise AssertionError(msg) + raise FileNotFoundError(msg) with open(genesis_byron, encoding="utf-8") as in_json: byron_dict = json.load(in_json) diff --git a/cardano_clusterlib/gov_action_group.py b/cardano_clusterlib/gov_action_group.py index 8af29d7..9e29bbb 100644 --- a/cardano_clusterlib/gov_action_group.py +++ b/cardano_clusterlib/gov_action_group.py @@ -42,7 +42,7 @@ def _get_deposit_return_key_args( key_args = ["--deposit-return-stake-key-hash", str(deposit_return_stake_key_hash)] else: msg = "Either stake verification key or stake key hash must be set." - raise AssertionError(msg) + raise ValueError(msg) return key_args @@ -86,7 +86,7 @@ def _get_cc_members_args( ) else: msg = f"Either {arg_action} cold verification key or its hash must be set." - raise AssertionError(msg) + raise ValueError(msg) if not remove: cc_members_args.extend(["--epoch", str(cc_member.epoch)]) @@ -103,7 +103,7 @@ def _get_optional_prev_action_args( if prev_action_txid: if prev_action_ix == -1: msg = "Previous action index must be set." - raise AssertionError(msg) + raise ValueError(msg) prev_action_args = [ "--prev-governance-action-tx-id", str(prev_action_txid), @@ -508,7 +508,7 @@ def create_treasury_withdrawal( ] else: msg = "Either stake verification key or stake key hash must be set." - raise AssertionError(msg) + raise ValueError(msg) deposit_key_args = self._get_deposit_return_key_args( deposit_return_stake_vkey=deposit_return_stake_vkey, diff --git a/cardano_clusterlib/gov_committee_group.py b/cardano_clusterlib/gov_committee_group.py index 5d3f1d6..a81dd62 100644 --- a/cardano_clusterlib/gov_committee_group.py +++ b/cardano_clusterlib/gov_committee_group.py @@ -31,7 +31,7 @@ def _get_cold_vkey_args( key_args = ["--cold-key-hash", str(cold_vkey_hash)] else: msg = "Either `cold_vkey`, `cold_vkey_file` or `cold_vkey_hash` is needed." - raise AssertionError(msg) + raise ValueError(msg) return key_args @@ -99,7 +99,7 @@ def gen_hot_key_auth_cert( hot_key_args = ["--hot-verification-key-hash", str(hot_key_hash)] else: msg = "Either `hot_key`, `hot_key_file` or `hot_key_hash` is needed." - raise AssertionError(msg) + raise ValueError(msg) self._clusterlib_obj.cli( [ @@ -176,7 +176,7 @@ def get_key_hash( key_args = ["--verification-key-file", str(vkey_file)] else: msg = "Either `vkey` or `vkey_file` is needed." - raise AssertionError(msg) + raise ValueError(msg) key_hash = ( self._clusterlib_obj.cli([*self._group_args, "key-hash", *key_args]) diff --git a/cardano_clusterlib/gov_drep_group.py b/cardano_clusterlib/gov_drep_group.py index 549f670..2dec82e 100644 --- a/cardano_clusterlib/gov_drep_group.py +++ b/cardano_clusterlib/gov_drep_group.py @@ -56,7 +56,7 @@ def _get_cred_args( msg = ( "Either `script_hash`, `drep_vkey`, `drep_vkey_file` or `drep_key_hash` is needed." ) - raise AssertionError(msg) + raise ValueError(msg) return cred_args @@ -111,12 +111,12 @@ def get_id( cli_args = ["--drep-verification-key-file", str(drep_vkey_file)] else: msg = "Either `drep_vkey` or `drep_vkey_file` is needed." - raise AssertionError(msg) + raise ValueError(msg) if out_format: if out_format not in ("hex", "bech32"): msg = f"Invalid output format: {out_format} (expected 'hex' or 'bech32')." - raise AssertionError(msg) + raise ValueError(msg) if self._has_output_hex: cli_args.append(f"--output-{out_format}") else: diff --git a/cardano_clusterlib/gov_group.py b/cardano_clusterlib/gov_group.py index fbf6ac1..397d444 100644 --- a/cardano_clusterlib/gov_group.py +++ b/cardano_clusterlib/gov_group.py @@ -77,7 +77,7 @@ def get_anchor_data_hash( content_args = ["--file-text", str(file_text)] else: msg = "Either `text`, `file_binary` or `file_text` is needed." - raise AssertionError(msg) + raise ValueError(msg) out_hash = ( self._clusterlib_obj.cli( diff --git a/cardano_clusterlib/gov_vote_group.py b/cardano_clusterlib/gov_vote_group.py index c613c61..7925c68 100644 --- a/cardano_clusterlib/gov_vote_group.py +++ b/cardano_clusterlib/gov_vote_group.py @@ -31,7 +31,7 @@ def _get_vote_args( vote_args = ["--abstain"] else: msg = "No vote was specified." - raise AssertionError(msg) + raise ValueError(msg) return vote_args @@ -57,7 +57,7 @@ def _get_anchor_args( if anchor_url: if not anchor_data_hash: msg = "Anchor data hash is required when anchor URL is specified." - raise AssertionError(msg) + raise ValueError(msg) anchor_args = [ "--anchor-url", str(anchor_url), @@ -101,7 +101,7 @@ def create_committee( cred_args = ["--cc-hot-script-hash", cc_hot_script_hash] else: msg = "No CC key or script hash was specified." - raise AssertionError(msg) + raise ValueError(msg) self._clusterlib_obj.cli( [ @@ -166,7 +166,7 @@ def create_drep( cred_args = ["--drep-script-hash", drep_script_hash] else: msg = "No DRep key or script hash was specified." - raise AssertionError(msg) + raise ValueError(msg) self._clusterlib_obj.cli( [ @@ -228,7 +228,7 @@ def create_spo( key_args = ["--stake-pool-id", stake_pool_id] else: msg = "No key was specified." - raise AssertionError(msg) + raise ValueError(msg) self._clusterlib_obj.cli( [ diff --git a/cardano_clusterlib/query_group.py b/cardano_clusterlib/query_group.py index 1dc44cb..d1d04ea 100644 --- a/cardano_clusterlib/query_group.py +++ b/cardano_clusterlib/query_group.py @@ -112,7 +112,7 @@ def get_utxo( cli_args.extend(helpers._prepend_flag("--tx-in", utxo_formatted)) else: msg = "Either `address`, `txin`, `utxo` or `tx_raw_output` need to be specified." - raise AssertionError(msg) + raise ValueError(msg) utxo_dict = json.loads(self.query_cli(cli_args)) utxos = txtools.get_utxo(utxo_dict=utxo_dict, address=address_single, coins=coins) @@ -397,7 +397,7 @@ def get_leadership_schedule( ) else: msg = "Either `stake_pool_vkey`, `cold_vkey_file` or `stake_pool_id` is needed." - raise AssertionError(msg) + raise ValueError(msg) args.append("--next" if for_next else "--current") diff --git a/cardano_clusterlib/stake_address_group.py b/cardano_clusterlib/stake_address_group.py index a27f2a3..a58262f 100644 --- a/cardano_clusterlib/stake_address_group.py +++ b/cardano_clusterlib/stake_address_group.py @@ -34,7 +34,7 @@ def _get_stake_vkey_args( stake_args = ["--stake-address", stake_address] else: msg = "Either `stake_vkey_file`, `stake_script_file` or `stake_address` is needed." - raise AssertionError(msg) + raise ValueError(msg) return stake_args @@ -62,7 +62,7 @@ def _get_drep_args( drep_args = ["--drep-key-hash", str(drep_key_hash)] else: msg = "DRep identification, verification key or script hash is needed." - raise AssertionError(msg) + raise ValueError(msg) return drep_args @@ -81,7 +81,7 @@ def _get_pool_key_args( pool_key_args = ["--stake-pool-id", stake_pool_id] else: msg = "No stake pool key was specified." - raise AssertionError(msg) + raise ValueError(msg) return pool_key_args @@ -113,7 +113,7 @@ def gen_stake_addr( cli_args = ["--stake-script-file", str(stake_script_file)] else: msg = "Either `stake_vkey_file` or `stake_script_file` is needed." - raise AssertionError(msg) + raise ValueError(msg) self._clusterlib_obj.cli( [ @@ -517,7 +517,7 @@ def get_stake_vkey_hash( cli_args = ["--stake-verification-key-file", str(stake_vkey_file)] else: msg = "Either `stake_vkey` or `stake_vkey_file` is needed." - raise AssertionError(msg) + raise ValueError(msg) return ( self._clusterlib_obj.cli(["stake-address", "key-hash", *cli_args]) diff --git a/cardano_clusterlib/stake_pool_group.py b/cardano_clusterlib/stake_pool_group.py index 46baa6c..8538bb2 100644 --- a/cardano_clusterlib/stake_pool_group.py +++ b/cardano_clusterlib/stake_pool_group.py @@ -165,7 +165,7 @@ def get_stake_pool_id( key_args = ["--cold-verification-key-file", str(cold_vkey_file)] else: msg = "No key was specified." - raise AssertionError(msg) + raise ValueError(msg) pool_id = ( self._clusterlib_obj.cli(["stake-pool", "id", *key_args]).stdout.strip().decode("ascii") diff --git a/cardano_clusterlib/transaction_group.py b/cardano_clusterlib/transaction_group.py index 3208e7a..5e17779 100644 --- a/cardano_clusterlib/transaction_group.py +++ b/cardano_clusterlib/transaction_group.py @@ -71,7 +71,7 @@ def get_txid(self, tx_body_file: itp.FileType = "", tx_file: itp.FileType = "") cli_args.extend(["--tx-file", str(tx_file)]) else: msg = "Either `tx_body_file` or `tx_file` is needed." - raise AssertionError(msg) + raise ValueError(msg) return ( self._clusterlib_obj.cli(["transaction", "txid", *cli_args]) @@ -97,7 +97,7 @@ def view_tx(self, tx_body_file: itp.FileType = "", tx_file: itp.FileType = "") - cli_args.extend(["--tx-file", str(tx_file)]) else: msg = "Either `tx_body_file` or `tx_file` is needed." - raise AssertionError(msg) + raise ValueError(msg) if self._has_debug: cli_args = ["cardano-cli", "debug", *cli_args] @@ -148,7 +148,7 @@ def get_hash_script_data( "Either `script_data_file`, `script_data_cbor_file` or `script_data_value` " "is needed." ) - raise AssertionError(msg) + raise ValueError(msg) return ( self._clusterlib_obj.cli(["transaction", "hash-script-data", *cli_args]) @@ -277,7 +277,7 @@ def build_raw_tx_bare( # noqa: C901 msg = ( "Both `treasury_donation` and `current_treasury_value` must be specified together." ) - raise AssertionError(msg) + raise ValueError(msg) if tx_files.certificate_files and complex_certs: LOGGER.warning( @@ -769,7 +769,7 @@ def calculate_min_req_utxo( """ if not txouts: msg = "No txout was specified." - raise AssertionError(msg) + raise ValueError(msg) txout_args, __, txouts_count = txtools._join_txouts(txouts=txouts) @@ -778,7 +778,7 @@ def calculate_min_req_utxo( "Accepts `TxOuts` only for a single transaction txout " "(same address, datum, script)." ) - raise AssertionError(msg) + raise ValueError(msg) self._clusterlib_obj.create_pparams_file() stdout = self._clusterlib_obj.cli( @@ -879,7 +879,7 @@ def build_tx( # noqa: C901 if max_txout: if change_address: msg = "Cannot use '-1' amount and change address at the same time." - raise AssertionError(msg) + raise ValueError(msg) change_address = max_txout[0].address else: change_address = change_address or src_address @@ -1144,7 +1144,7 @@ def build_estimate_tx( # noqa: C901 if max_txout: if change_address: msg = "Cannot use '-1' amount and change address at the same time." - raise AssertionError(msg) + raise ValueError(msg) change_address = max_txout[0].address else: change_address = change_address or src_address @@ -1153,7 +1153,7 @@ def build_estimate_tx( # noqa: C901 msg = ( "Both `treasury_donation` and `current_treasury_value` must be specified together." ) - raise AssertionError(msg) + raise ValueError(msg) tx_files = tx_files or structs.TxFiles() if tx_files.certificate_files and complex_certs: @@ -1366,7 +1366,7 @@ def sign_tx( cli_args = ["--tx-file", str(tx_file)] else: msg = "Either `tx_body_file` or `tx_file` is needed." - raise AssertionError(msg) + raise ValueError(msg) self._clusterlib_obj.cli( [ diff --git a/cardano_clusterlib/txtools.py b/cardano_clusterlib/txtools.py index ef130f6..5eb9ea9 100644 --- a/cardano_clusterlib/txtools.py +++ b/cardano_clusterlib/txtools.py @@ -303,7 +303,7 @@ def _balance_txouts( burning_txouts = [r for r in txouts if r.amount < 0 and r.coin != consts.DEFAULT_COIN] if burning_txouts: msg = f"Token burning is not allowed in txouts: {burning_txouts}" - raise AssertionError(msg) + raise ValueError(msg) # Filter out negative amounts (-1 "max" amounts) txouts_result = [r for r in txouts if r.amount > 0] @@ -327,7 +327,7 @@ def _balance_txouts( max_index = [idx for idx, val in enumerate(coin_txouts) if val.amount == -1] if len(max_index) > 1: msg = "Cannot send all remaining funds to more than one address." - raise AssertionError(msg) + raise ValueError(msg) if max_index: # Remove the "-1" record and get its address max_address = coin_txouts.pop(max_index[0]).address @@ -608,7 +608,7 @@ def _get_return_collateral_txout_args(txouts: structs.OptionalTxOuts) -> list[st addresses = {t.address for t in txouts} if len(addresses) > 1: msg = "Accepts `txouts` only for single address." - raise AssertionError(msg) + raise ValueError(msg) txout_records = [ f"{t.amount} {t.coin if t.coin != consts.DEFAULT_COIN else ''}".rstrip() for t in txouts @@ -804,7 +804,7 @@ def collect_data_for_build( script_addresses = {r.address for r in script_txins_records} if src_address in script_addresses: msg = "Source address cannot be a script address." - raise AssertionError(msg) + raise ValueError(msg) # Combine txins and make sure we have enough funds to satisfy all txouts combined_txins = [