Skip to content

Commit a803dc0

Browse files
authored
[CHIA-3633] August 2025 pass of Ruff linting rules (#19929)
* Enable Ruff `PLR1704` (`redefined-argument-from-local`) * Enable Ruff `PLR5501` (`collapsible-else-if`) * Enable Ruff `PLW2901` (`redefined-loop-name`) * bad change * Enable Ruff `RUF043` (`pytest-raises-ambiguous-pattern`) * Enable Ruff `RUF046` (`unnecessary-cast-to-int`) * Enable Ruff `RUF052` (`used-dummy-variable`) * Remove unused ignore `UP006` * One mis-signaled regex * Comments by @altendky * bad change, my bad
1 parent b95e8d9 commit a803dc0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+390
-433
lines changed

chia/_tests/blockchain/blockchain_test_utils.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,10 @@ async def _validate_and_add_block(
116116
if err is not None:
117117
# Got an error
118118
raise AssertionError(err)
119-
else:
120-
# Here we will enforce checking of the exact error
121-
if err != expected_error:
122-
# Did not get the right error, or did not get an error
123-
raise AssertionError(f"Expected {expected_error} but got {err}")
119+
# Here we will enforce checking of the exact error
120+
elif err != expected_error:
121+
# Did not get the right error, or did not get an error
122+
raise AssertionError(f"Expected {expected_error} but got {err}")
124123

125124
if expected_result is not None and expected_result != result:
126125
raise AssertionError(f"Expected {expected_result} but got {result}")

chia/_tests/blockchain/test_augmented_chain.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import re
34
from dataclasses import dataclass, field
45
from typing import TYPE_CHECKING, ClassVar, Optional, cast
56

@@ -93,7 +94,7 @@ async def test_augmented_chain(default_10000_blocks: list[FullBlock]) -> None:
9394
with pytest.raises(KeyError):
9495
await abc.prev_block_hash([blocks[2].header_hash])
9596

96-
with pytest.raises(ValueError, match="Err.GENERATOR_REF_HAS_NO_GENERATOR"):
97+
with pytest.raises(ValueError, match=re.escape(Err.GENERATOR_REF_HAS_NO_GENERATOR.name)):
9798
await abc.lookup_block_generators(blocks[3].header_hash, {uint32(3)})
9899

99100
block_records = []
@@ -105,11 +106,11 @@ async def test_augmented_chain(default_10000_blocks: list[FullBlock]) -> None:
105106

106107
assert abc.height_to_block_record(uint32(1)) == block_records[1]
107108

108-
with pytest.raises(ValueError, match="Err.GENERATOR_REF_HAS_NO_GENERATOR"):
109+
with pytest.raises(ValueError, match=re.escape(Err.GENERATOR_REF_HAS_NO_GENERATOR.name)):
109110
await abc.lookup_block_generators(blocks[10].header_hash, {uint32(3), uint32(10)})
110111

111112
# block 1 exists in the chain, but it doesn't have a generator
112-
with pytest.raises(ValueError, match="Err.GENERATOR_REF_HAS_NO_GENERATOR"):
113+
with pytest.raises(ValueError, match=re.escape(Err.GENERATOR_REF_HAS_NO_GENERATOR.name)):
113114
await abc.lookup_block_generators(blocks[1].header_hash, {uint32(1)})
114115

115116
expect_gen = blocks[2].transactions_generator

chia/_tests/blockchain/test_blockchain.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import copy
55
import logging
66
import random
7+
import re
78
import time
89
from collections.abc import AsyncIterator, Awaitable
910
from contextlib import asynccontextmanager
@@ -4158,24 +4159,24 @@ async def test_lookup_block_generators(
41584159
# make sure we don't cross the forks
41594160
if clear_cache:
41604161
b.clean_block_records()
4161-
with pytest.raises(ValueError, match="Err.GENERATOR_REF_HAS_NO_GENERATOR"):
4162+
with pytest.raises(ValueError, match=re.escape(Err.GENERATOR_REF_HAS_NO_GENERATOR.name)):
41624163
await b.lookup_block_generators(peak_1.prev_header_hash, {uint32(516)})
41634164

41644165
if clear_cache:
41654166
b.clean_block_records()
4166-
with pytest.raises(ValueError, match="Err.GENERATOR_REF_HAS_NO_GENERATOR"):
4167+
with pytest.raises(ValueError, match=re.escape(Err.GENERATOR_REF_HAS_NO_GENERATOR.name)):
41674168
await b.lookup_block_generators(peak_2.prev_header_hash, {uint32(503)})
41684169

41694170
# make sure we fail when looking up a non-transaction block from the main
41704171
# chain, regardless of which chain we start at
41714172
if clear_cache:
41724173
b.clean_block_records()
4173-
with pytest.raises(ValueError, match="Err.GENERATOR_REF_HAS_NO_GENERATOR"):
4174+
with pytest.raises(ValueError, match=re.escape(Err.GENERATOR_REF_HAS_NO_GENERATOR.name)):
41744175
await b.lookup_block_generators(peak_1.prev_header_hash, {uint32(8)})
41754176

41764177
if clear_cache:
41774178
b.clean_block_records()
4178-
with pytest.raises(ValueError, match="Err.GENERATOR_REF_HAS_NO_GENERATOR"):
4179+
with pytest.raises(ValueError, match=re.escape(Err.GENERATOR_REF_HAS_NO_GENERATOR.name)):
41794180
await b.lookup_block_generators(peak_2.prev_header_hash, {uint32(8)})
41804181

41814182
# if we try to look up generators starting from a disconnected block, we

chia/_tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ def blockchain_constants(consensus_mode: ConsensusMode) -> ConsensusConstants:
230230
@pytest.fixture(scope="session", name="bt")
231231
async def block_tools_fixture(get_keychain, blockchain_constants, anyio_backend) -> BlockTools:
232232
# Note that this causes a lot of CPU and disk traffic - disk, DB, ports, process creation ...
233-
_shared_block_tools = await create_block_tools_async(constants=blockchain_constants, keychain=get_keychain)
234-
return _shared_block_tools
233+
shared_block_tools = await create_block_tools_async(constants=blockchain_constants, keychain=get_keychain)
234+
return shared_block_tools
235235

236236

237237
# if you have a system that has an unusual hostname for localhost and you want

chia/_tests/core/data_layer/test_data_rpc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2278,7 +2278,7 @@ async def test_unsubscribe_unknown(
22782278
bare_data_layer_api: DataLayerRpcApi,
22792279
seeded_random: random.Random,
22802280
) -> None:
2281-
with pytest.raises(RuntimeError, match="No subscription found for the given store_id."):
2281+
with pytest.raises(RuntimeError, match="No subscription found for the given store_id"):
22822282
await bare_data_layer_api.unsubscribe(request={"id": bytes32.random(seeded_random).hex(), "retain": False})
22832283

22842284

chia/_tests/core/data_layer/test_data_store.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ async def test_ancestor_table_unique_inserts(data_store: DataStore, store_id: by
594594
hash_2 = bytes32.from_hexstr("924be8ff27e84cba17f5bc918097f8410fab9824713a4668a21c8e060a8cab40")
595595
await data_store._insert_ancestor_table(hash_1, hash_2, store_id, 2)
596596
await data_store._insert_ancestor_table(hash_1, hash_2, store_id, 2)
597-
with pytest.raises(Exception, match="^Requested insertion of ancestor"):
597+
with pytest.raises(Exception, match=r"^Requested insertion of ancestor"):
598598
await data_store._insert_ancestor_table(hash_1, hash_1, store_id, 2)
599599
await data_store._insert_ancestor_table(hash_1, hash_2, store_id, 2)
600600

@@ -2385,11 +2385,10 @@ async def test_get_leaf_at_minimum_height(
23852385
if isinstance(node, InternalNode):
23862386
heights[node.left_hash] = heights[node.hash] + 1
23872387
heights[node.right_hash] = heights[node.hash] + 1
2388+
elif min_leaf_height is not None:
2389+
min_leaf_height = min(min_leaf_height, heights[node.hash])
23882390
else:
2389-
if min_leaf_height is not None:
2390-
min_leaf_height = min(min_leaf_height, heights[node.hash])
2391-
else:
2392-
min_leaf_height = heights[node.hash]
2391+
min_leaf_height = heights[node.hash]
23932392

23942393
assert min_leaf_height is not None
23952394
if pre > 0:

chia/_tests/core/full_node/stores/test_block_store.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,8 @@ def generator(i: int) -> SerializedProgram:
418418
store = await BlockStore.create(db_wrapper, use_cache=use_cache)
419419

420420
new_blocks = []
421-
for i, block in enumerate(blocks):
422-
block = block.replace(transactions_generator=generator(i))
421+
for i, original_block in enumerate(blocks):
422+
block = original_block.replace(transactions_generator=generator(i))
423423
block_record = header_block_to_sub_block_record(
424424
DEFAULT_CONSTANTS, uint64(0), block, uint64(0), False, uint8(0), uint32(max(0, block.height - 1)), None
425425
)

chia/_tests/core/full_node/test_address_manager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ async def check_retrieved_peers(self, wanted_peers: list[ExtendedPeerInfo], addr
560560
# use tmp_path pytest fixture to create a temporary directory
561561
async def test_serialization(self, tmp_path: Path):
562562
addrman = AddressManagerTest()
563-
now = int(math.floor(time.time()))
563+
now = math.floor(time.time())
564564
t_peer1 = TimestampedPeerInfo("250.7.1.1", uint16(8333), uint64(now - 10000))
565565
t_peer2 = TimestampedPeerInfo("1050:0000:0000:0000:0005:0600:300c:326b", uint16(9999), uint64(now - 20000))
566566
t_peer3 = TimestampedPeerInfo("250.7.3.3", uint16(9999), uint64(now - 30000))
@@ -587,7 +587,7 @@ async def test_serialization(self, tmp_path: Path):
587587
@pytest.mark.anyio
588588
async def test_bad_ip_encoding(self, tmp_path: Path):
589589
addrman = AddressManagerTest()
590-
now = int(math.floor(time.time()))
590+
now = math.floor(time.time())
591591
t_peer1 = TimestampedPeerInfo("250.7.1.1", uint16(8333), uint64(now - 10000))
592592
t_peer2 = TimestampedPeerInfo("1050:0000:0000:0000:0005:0600:300c:326b", uint16(9999), uint64(now - 20000))
593593
t_peer3 = TimestampedPeerInfo("250.7.3.3", uint16(9999), uint64(now - 30000))
@@ -725,7 +725,7 @@ async def old_serialize(address_manager: AddressManager, peers_file_path: Path)
725725

726726
# create a file with the old serialization, then migrate to new serialization
727727
addrman = AddressManagerTest()
728-
now = int(math.floor(time.time()))
728+
now = math.floor(time.time())
729729
t_peer1 = TimestampedPeerInfo("250.7.1.1", uint16(8333), uint64(now - 10000))
730730
t_peer2 = TimestampedPeerInfo("1050:0000:0000:0000:0005:0600:300c:326b", uint16(9999), uint64(now - 20000))
731731
t_peer3 = TimestampedPeerInfo("250.7.3.3", uint16(9999), uint64(now - 30000))

chia/_tests/core/full_node/test_full_node.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,16 +2678,15 @@ async def test_long_reorg_nodes(
26782678
blocks = default_10000_blocks[: 1600 - chain_length]
26792679
reorg_blocks = test_long_reorg_blocks_light[: 1600 - chain_length]
26802680
reorg_height = 2000
2681-
else:
2682-
if fork_point == 1500:
2683-
blocks = default_10000_blocks[: 1900 - chain_length]
2684-
reorg_blocks = test_long_reorg_1500_blocks[: 1900 - chain_length]
2685-
reorg_height = 2300
2686-
else: # pragma: no cover
2687-
pytest.skip("We rely on the light-blocks test for a 0 forkpoint")
2688-
blocks = default_10000_blocks[: 1100 - chain_length]
2689-
# reorg_blocks = test_long_reorg_blocks[: 1100 - chain_length]
2690-
reorg_height = 1600
2681+
elif fork_point == 1500:
2682+
blocks = default_10000_blocks[: 1900 - chain_length]
2683+
reorg_blocks = test_long_reorg_1500_blocks[: 1900 - chain_length]
2684+
reorg_height = 2300
2685+
else: # pragma: no cover
2686+
pytest.skip("We rely on the light-blocks test for a 0 forkpoint")
2687+
blocks = default_10000_blocks[: 1100 - chain_length]
2688+
# reorg_blocks = test_long_reorg_blocks[: 1100 - chain_length]
2689+
reorg_height = 1600
26912690

26922691
# this is a pre-requisite for a reorg to happen
26932692
assert default_10000_blocks[reorg_height].weight > reorg_blocks[-1].weight
@@ -3163,15 +3162,14 @@ async def declare_pos_unfinished_block(
31633162
challenge_chain_sp = block.reward_chain_block.challenge_chain_sp_vdf.output.get_hash()
31643163
if block.reward_chain_block.reward_chain_sp_vdf is not None:
31653164
reward_chain_sp = block.reward_chain_block.reward_chain_sp_vdf.output.get_hash()
3165+
elif len(block.finished_sub_slots) > 0:
3166+
reward_chain_sp = block.finished_sub_slots[-1].reward_chain.get_hash()
31663167
else:
3167-
if len(block.finished_sub_slots) > 0:
3168-
reward_chain_sp = block.finished_sub_slots[-1].reward_chain.get_hash()
3169-
else:
3170-
curr = blockchain.block_record(block.prev_header_hash)
3171-
while not curr.first_in_sub_slot:
3172-
curr = blockchain.block_record(curr.prev_hash)
3173-
assert curr.finished_reward_slot_hashes is not None
3174-
reward_chain_sp = curr.finished_reward_slot_hashes[-1]
3168+
curr = blockchain.block_record(block.prev_header_hash)
3169+
while not curr.first_in_sub_slot:
3170+
curr = blockchain.block_record(curr.prev_hash)
3171+
assert curr.finished_reward_slot_hashes is not None
3172+
reward_chain_sp = curr.finished_reward_slot_hashes[-1]
31753173
farmer_reward_address = block.foliage.foliage_block_data.farmer_reward_puzzle_hash
31763174
pool_target = block.foliage.foliage_block_data.pool_target
31773175
pool_target_signature = block.foliage.foliage_block_data.pool_signature

chia/_tests/core/mempool/test_mempool_fee_estimator.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ async def test_basics() -> None:
2323

2424
cost = uint64(5000000)
2525
for i in range(300, 700):
26-
i = uint32(i)
2726
items = []
2827
for _ in range(2, 100):
2928
fee = uint64(10000000)
@@ -50,7 +49,7 @@ async def test_basics() -> None:
5049
)
5150
items.append(mempool_item2)
5251

53-
fee_tracker.process_block(i, items)
52+
fee_tracker.process_block(uint32(i), items)
5453

5554
short, med, long = fee_tracker.estimate_fees()
5655

@@ -72,7 +71,6 @@ async def test_fee_increase() -> None:
7271
estimator = SmartFeeEstimator(fee_tracker, uint64(test_constants.MAX_BLOCK_COST_CLVM))
7372
random = Random(x=1)
7473
for i in range(300, 700):
75-
i = uint32(i)
7674
items = []
7775
for _ in range(20):
7876
fee = uint64(0)
@@ -85,7 +83,7 @@ async def test_fee_increase() -> None:
8583
)
8684
items.append(mempool_item)
8785

88-
fee_tracker.process_block(i, items)
86+
fee_tracker.process_block(uint32(i), items)
8987

9088
short, med, long = fee_tracker.estimate_fees()
9189
mempool_info = mempool_manager.mempool.fee_estimator.get_mempool_info()

0 commit comments

Comments
 (0)