Skip to content

Commit d38ef78

Browse files
committed
feat: enable performance checking with ruff
- Added `PERF` to ruff linting rules in `pyproject.toml` - Refactored code to comply with new performance rules - Updated exception handling to include `# noqa: PERF203` - Optimized list comprehensions and `extend` usage
1 parent a32f9f2 commit d38ef78

File tree

12 files changed

+73
-68
lines changed

12 files changed

+73
-68
lines changed

cardano_node_tests/tests/test_reconnect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def _assert() -> None:
268268
for check_no in range(1, 11):
269269
try:
270270
_assert()
271-
except AssertionError:
271+
except AssertionError: # noqa: PERF203
272272
if check_no == 10:
273273
raise
274274
LOGGER.info(f"AssertionError on check {check_no}")

cardano_node_tests/tests/test_staking_rewards.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,7 @@ def _get_rew_amount_for_cred_hash(key_hash: str, rec: dict[str, list[dict]]) ->
244244
def _get_rew_type_for_cred_hash(key_hash: str, rec: dict[str, list[dict]]) -> list[str]:
245245
"""Get reward types for credential hash in ledger state snapshot record."""
246246
r = rec.get(key_hash) or []
247-
rew_types = []
248-
for sr in r:
249-
rew_types.append(sr["rewardType"])
247+
rew_types = [sr["rewardType"] for sr in r]
250248
return rew_types
251249

252250

cardano_node_tests/tests/test_tx_mempool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def test_query_mempool_txin(
8888
for r in range(5):
8989
try:
9090
cluster.g_transaction.submit_tx_bare(tx_file=out_file_signed)
91-
except clusterlib.CLIError as exc:
91+
except clusterlib.CLIError as exc: # noqa: PERF203
9292
if r == 0 or "(BadInputsUTxO" not in str(exc):
9393
raise
9494
break

cardano_node_tests/tests/tests_plutus/spend_build.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,18 @@ def _build_fund_script(
6464
clusterlib.TxOut(address=dst_addr.address, amount=redeem_cost.collateral),
6565
]
6666

67-
for token in stokens:
68-
txouts.append(dataclasses.replace(script_txout, amount=token.amount, coin=token.coin))
67+
txouts.extend(
68+
dataclasses.replace(script_txout, amount=token.amount, coin=token.coin) for token in stokens
69+
)
6970

70-
for token in ctokens:
71-
txouts.append(
72-
clusterlib.TxOut(
73-
address=dst_addr.address,
74-
amount=token.amount,
75-
coin=token.coin,
76-
)
71+
txouts.extend(
72+
clusterlib.TxOut(
73+
address=dst_addr.address,
74+
amount=token.amount,
75+
coin=token.coin,
7776
)
77+
for token in ctokens
78+
)
7879

7980
tx_output = cluster_obj.g_transaction.build_tx(
8081
src_address=payment_addr.address,

cardano_node_tests/tests/tests_plutus/spend_raw.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,18 @@ def _fund_script(
6767
clusterlib.TxOut(address=dst_addr.address, amount=redeem_cost.collateral),
6868
]
6969

70-
for token in stokens:
71-
txouts.append(dataclasses.replace(script_txout, amount=token.amount, coin=token.coin))
70+
txouts.extend(
71+
dataclasses.replace(script_txout, amount=token.amount, coin=token.coin) for token in stokens
72+
)
7273

73-
for token in ctokens:
74-
txouts.append(
75-
clusterlib.TxOut(
76-
address=dst_addr.address,
77-
amount=token.amount,
78-
coin=token.coin,
79-
)
74+
txouts.extend(
75+
clusterlib.TxOut(
76+
address=dst_addr.address,
77+
amount=token.amount,
78+
coin=token.coin,
8079
)
80+
for token in ctokens
81+
)
8182

8283
tx_raw_output = cluster_obj.g_transaction.send_tx(
8384
src_address=payment_addr.address,

cardano_node_tests/tests/tests_plutus_v2/spend_build.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,14 @@ def _build_fund_script(
114114
)
115115
)
116116

117-
for token in tokens_collateral or []:
118-
txouts.append(
119-
clusterlib.TxOut(
120-
address=dst_addr.address,
121-
amount=token.amount,
122-
coin=token.coin,
123-
)
117+
txouts.extend(
118+
clusterlib.TxOut(
119+
address=dst_addr.address,
120+
amount=token.amount,
121+
coin=token.coin,
124122
)
123+
for token in tokens_collateral or []
124+
)
125125

126126
tx_output = cluster.g_transaction.build_tx(
127127
src_address=payment_addr.address,

cardano_node_tests/tests/tests_plutus_v2/spend_raw.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,14 @@ def _fund_script(
106106
)
107107
)
108108

109-
for token in tokens_collateral or []:
110-
txouts.append(
111-
clusterlib.TxOut(
112-
address=dst_addr.address,
113-
amount=token.amount,
114-
coin=token.coin,
115-
)
109+
txouts.extend(
110+
clusterlib.TxOut(
111+
address=dst_addr.address,
112+
amount=token.amount,
113+
coin=token.coin,
116114
)
115+
for token in tokens_collateral or []
116+
)
117117

118118
tx_raw_output = cluster.g_transaction.send_tx(
119119
src_address=payment_addr.address,

cardano_node_tests/utils/cluster_nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def services_action(service_names: list[str], action: str, instance_num: int | N
410410
helpers.run_command(
411411
f"supervisorctl -s http://localhost:{supervisor_port} {action} {service_name}"
412412
)
413-
except Exception as exc:
413+
except Exception as exc: # noqa: PERF203
414414
msg = f"Failed to {action} service `{service_name}`"
415415
raise Exception(msg) from exc
416416

cardano_node_tests/utils/dbsync_utils.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,42 @@ def get_address_reward(
2626
2727
The `epoch_from` and `epoch_to` are epochs where the reward can be spent.
2828
"""
29-
rewards = []
30-
for db_row in dbsync_queries.query_address_reward(
31-
address=address, epoch_from=epoch_from, epoch_to=epoch_to
32-
):
33-
rewards.append(
34-
dbsync_types.RewardEpochRecord(
35-
amount=int(db_row.amount),
36-
earned_epoch=db_row.earned_epoch,
37-
spendable_epoch=db_row.spendable_epoch,
38-
type=db_row.type,
39-
pool_id=db_row.pool_id or "",
40-
)
29+
rewards: list[dbsync_types.RewardEpochRecord] = []
30+
rewards.extend(
31+
dbsync_types.RewardEpochRecord(
32+
amount=int(db_row.amount),
33+
earned_epoch=db_row.earned_epoch,
34+
spendable_epoch=db_row.spendable_epoch,
35+
type=db_row.type,
36+
pool_id=db_row.pool_id or "",
37+
)
38+
for db_row in dbsync_queries.query_address_reward(
39+
address=address, epoch_from=epoch_from, epoch_to=epoch_to
4140
)
41+
)
4242

43-
for db_row in dbsync_queries.query_address_reward_rest(
44-
address=address, epoch_from=epoch_from, epoch_to=epoch_to
45-
):
46-
rewards.append(
47-
dbsync_types.RewardEpochRecord(
48-
amount=int(db_row.amount),
49-
earned_epoch=db_row.earned_epoch,
50-
spendable_epoch=db_row.spendable_epoch,
51-
type=db_row.type,
52-
pool_id=db_row.pool_id or "",
53-
)
43+
reward_rest = list(
44+
dbsync_queries.query_address_reward_rest(
45+
address=address, epoch_from=epoch_from, epoch_to=epoch_to
5446
)
47+
)
48+
rewards.extend(
49+
dbsync_types.RewardEpochRecord(
50+
amount=int(db_row.amount),
51+
earned_epoch=db_row.earned_epoch,
52+
spendable_epoch=db_row.spendable_epoch,
53+
type=db_row.type,
54+
pool_id=db_row.pool_id or "",
55+
)
56+
for db_row in reward_rest
57+
)
5558
if not rewards:
5659
return dbsync_types.RewardRecord(address=address, reward_sum=0, rewards=[])
5760

5861
reward_sum = functools.reduce(lambda x, y: x + y.amount, rewards, 0)
59-
return dbsync_types.RewardRecord(address=db_row.address, reward_sum=reward_sum, rewards=rewards)
62+
return dbsync_types.RewardRecord(
63+
address=reward_rest[-1].address, reward_sum=reward_sum, rewards=rewards
64+
)
6065

6166

6267
def check_address_reward(
@@ -796,7 +801,7 @@ def check_plutus_costs(
796801
for db_record, cost_record in zip(sorted_db, sorted_costs):
797802
try:
798803
check_plutus_cost(redeemer_record=db_record, cost_record=cost_record)
799-
except AssertionError as err:
804+
except AssertionError as err: # noqa: PERF203
800805
errors.append(f"{db_record.script_hash}:\n{err}")
801806

802807
if errors:

cardano_node_tests/utils/governance_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,9 @@ def _get_cvkey_hash(member: clusterlib.CCMember) -> str:
448448
cvkey_hash = _get_cvkey_hash(member=_m)
449449
added_members[f"keyHash-{cvkey_hash}"] = _m.epoch
450450

451-
removed_members = []
452-
for _m in action_data.rem_cc_members:
453-
removed_members.append({"keyHash": _get_cvkey_hash(member=_m)})
451+
removed_members = [
452+
{"keyHash": _get_cvkey_hash(member=_m)} for _m in action_data.rem_cc_members
453+
]
454454

455455
gov_action = {
456456
"contents": [

0 commit comments

Comments
 (0)