Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHIA-1732] Add Ruff specific linting rules #18845

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions benchmarks/streamable.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def compare_results(
if data not in old:
continue
print(f"\ncompare: {data}, old: {old_commit_hash}, new: {new_commit_hash}")
print_compare_row("mode", "µs/iteration old", "µs/iteration new", "diff %")
print_compare_row("mode", "µs/iteration old", "µs/iteration new", "diff %") # noqa: RUF001
Quexington marked this conversation as resolved.
Show resolved Hide resolved
for mode, results in modes.items():
if mode not in old[data]:
continue
Expand Down Expand Up @@ -245,8 +245,8 @@ def run(data: Data, mode: Mode, runs: int, ms: int, live: bool, output: TextIO,
)
print_row(
mode="mode",
us_per_iteration="µs/iteration",
stdev_us_per_iteration="stdev µs/iteration %",
us_per_iteration="µs/iteration", # noqa: RUF001
stdev_us_per_iteration="stdev µs/iteration %", # noqa: RUF001
avg_iterations="avg iterations/run",
stdev_iterations="stdev iterations/run %",
)
Expand Down
10 changes: 6 additions & 4 deletions chia/_tests/blockchain/test_blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2404,8 +2404,9 @@ async def test_invalid_reward_claims(self, empty_blockchain: Blockchain, bt: Blo

# Too many
h = std_hash(b"")
too_many_reward_claims = block.transactions_info.reward_claims_incorporated + [
Coin(h, h, too_few_reward_claims[0].amount)
too_many_reward_claims = [
*block.transactions_info.reward_claims_incorporated,
Coin(h, h, too_few_reward_claims[0].amount),
]
block_2 = recursive_replace(block, "transactions_info.reward_claims_incorporated", too_many_reward_claims)
assert block_2.transactions_info is not None
Expand All @@ -2424,8 +2425,9 @@ async def test_invalid_reward_claims(self, empty_blockchain: Blockchain, bt: Blo
await _validate_and_add_block(b, block_2, expected_error=Err.INVALID_REWARD_COINS, skip_prevalidation=True)

# Duplicates
duplicate_reward_claims = block.transactions_info.reward_claims_incorporated + [
block.transactions_info.reward_claims_incorporated[-1]
duplicate_reward_claims = [
*block.transactions_info.reward_claims_incorporated,
block.transactions_info.reward_claims_incorporated[-1],
]
block_2 = recursive_replace(block, "transactions_info.reward_claims_incorporated", duplicate_reward_claims)
assert block_2.transactions_info is not None
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/clvm/coin_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def farm_coin(
puzzle_hash: bytes32,
birthday: CoinTimestamp,
amount: int = 1024,
prefix=bytes32.fromhex("ccd5bb71183532bff220ba46c268991a00000000000000000000000000000000"), # noqa
prefix=bytes32.fromhex("ccd5bb71183532bff220ba46c268991a00000000000000000000000000000000"),
) -> Coin:
parent = bytes32(
[
Expand Down
8 changes: 4 additions & 4 deletions chia/_tests/clvm/test_chialisp_deserialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def test_deserialization_simple_list():
def test_deserialization_password_coin():
# (i (= (sha256 2) (q 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824)) (c (q 51) (c 5 (c (q 100) (q ())))) (q "wrong password")) # noqa
b = hexstr_to_bytes(
"ff04ffff0affff0bff0280ffff01ffa02cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b98248080ffff05ffff01ff3380ffff05ff05ffff05ffff01ff6480ffff01ff8080808080ffff01ff8e77726f6e672070617373776f72648080" # noqa
) # noqa
"ff04ffff0affff0bff0280ffff01ffa02cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b98248080ffff05ffff01ff3380ffff05ff05ffff05ffff01ff6480ffff01ff8080808080ffff01ff8e77726f6e672070617373776f72648080"
)
cost, output = DESERIALIZE_MOD.run_with_cost(INFINITE_COST, [b])
print(cost, output)
prog = Program.to(output)
Expand All @@ -75,8 +75,8 @@ def test_deserialization_password_coin():
def test_deserialization_large_numbers():
# '(99999999999999999999999999999999999999999999999999999999999999999 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF -99999999999999999999999999999999999999999999999999999999999999999999999999999)' # noqa
b = hexstr_to_bytes(
"ff9c00f316271c7fc3908a8bef464e3945ef7a253609ffffffffffffffffffb00fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa1ff22ea0179500526edb610f148ec0c614155678491902d6000000000000000000180" # noqa
) # noqa
"ff9c00f316271c7fc3908a8bef464e3945ef7a253609ffffffffffffffffffb00fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa1ff22ea0179500526edb610f148ec0c614155678491902d6000000000000000000180"
)
cost, output = DESERIALIZE_MOD.run_with_cost(INFINITE_COST, [b])
print(cost, output)
prog = Program.to(output)
Expand Down
22 changes: 11 additions & 11 deletions chia/_tests/clvm/test_singletons.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ class TransactionPushError(Exception):


def sign_delegated_puz(del_puz: Program, coin: Coin) -> G2Element:
synthetic_secret_key: PrivateKey = p2_delegated_puzzle_or_hidden_puzzle.calculate_synthetic_secret_key( # noqa
synthetic_secret_key: PrivateKey = p2_delegated_puzzle_or_hidden_puzzle.calculate_synthetic_secret_key(
PrivateKey.from_bytes(
secret_exponent_for_index(1).to_bytes(32, "big"),
),
p2_delegated_puzzle_or_hidden_puzzle.DEFAULT_HIDDEN_PUZZLE_HASH,
)
return AugSchemeMPL.sign(
synthetic_secret_key,
(del_puz.get_tree_hash() + coin.name() + DEFAULT_CONSTANTS.AGG_SIG_ME_ADDITIONAL_DATA), # noqa
(del_puz.get_tree_hash() + coin.name() + DEFAULT_CONSTANTS.AGG_SIG_ME_ADDITIONAL_DATA),
)


Expand Down Expand Up @@ -88,12 +88,12 @@ async def test_singleton_top_layer(version, cost_logger):
# Generate starting info
key_lookup = KeyTool()
pk: G1Element = G1Element.from_bytes(public_key_for_index(1, key_lookup))
starting_puzzle: Program = p2_delegated_puzzle_or_hidden_puzzle.puzzle_for_pk(pk) # noqa
starting_puzzle: Program = p2_delegated_puzzle_or_hidden_puzzle.puzzle_for_pk(pk)

if version == 0:
from chia.wallet.puzzles import singleton_top_layer

adapted_puzzle: Program = singleton_top_layer.adapt_inner_to_singleton(starting_puzzle) # noqa
adapted_puzzle: Program = singleton_top_layer.adapt_inner_to_singleton(starting_puzzle)
else:
from chia.wallet.puzzles import singleton_top_layer_v1_1 as singleton_top_layer

Expand All @@ -110,19 +110,19 @@ async def test_singleton_top_layer(version, cost_logger):
# LAUNCHING
# Try to create an even singleton (driver test)
try:
conditions, launcher_coinsol = singleton_top_layer.launch_conditions_and_coinsol( # noqa
conditions, launcher_coinsol = singleton_top_layer.launch_conditions_and_coinsol(
starting_coin, adapted_puzzle, comment, (START_AMOUNT - 1)
)
raise AssertionError("This should fail due to an even amount")
except ValueError as msg:
assert str(msg) == "Coin amount cannot be even. Subtract one mojo."
conditions, launcher_coinsol = singleton_top_layer.launch_conditions_and_coinsol( # noqa
conditions, launcher_coinsol = singleton_top_layer.launch_conditions_and_coinsol(
starting_coin, adapted_puzzle, comment, START_AMOUNT
)

# Creating solution for standard transaction
delegated_puzzle: Program = p2_conditions.puzzle_for_conditions(conditions) # noqa
full_solution: Program = p2_delegated_puzzle_or_hidden_puzzle.solution_for_conditions(conditions) # noqa
delegated_puzzle: Program = p2_conditions.puzzle_for_conditions(conditions)
full_solution: Program = p2_delegated_puzzle_or_hidden_puzzle.solution_for_conditions(conditions)

starting_coinsol = make_spend(
starting_coin,
Expand Down Expand Up @@ -162,7 +162,7 @@ async def test_singleton_top_layer(version, cost_logger):
)
inner_solution: Program = Program.to([[], delegated_puzzle, []])
# Generate the lineage proof we will need from the launcher coin
lineage_proof: LineageProof = singleton_top_layer.lineage_proof_for_coinsol(launcher_coinsol) # noqa
lineage_proof: LineageProof = singleton_top_layer.lineage_proof_for_coinsol(launcher_coinsol)
puzzle_reveal: Program = singleton_top_layer.puzzle_for_singleton(
launcher_id,
adapted_puzzle,
Expand Down Expand Up @@ -192,7 +192,7 @@ async def test_singleton_top_layer(version, cost_logger):
# POST-EVE
singleton: Coin = (await sim.all_non_reward_coins())[0]
# Same delegated_puzzle / inner_solution. We're just recreating ourself
lineage_proof: LineageProof = singleton_top_layer.lineage_proof_for_coinsol(singleton_eve_coinsol) # noqa
lineage_proof: LineageProof = singleton_top_layer.lineage_proof_for_coinsol(singleton_eve_coinsol)
# Same puzzle_reveal too
full_solution: Program = singleton_top_layer.solution_for_singleton(
lineage_proof,
Expand Down Expand Up @@ -471,7 +471,7 @@ async def test_singleton_top_layer(version, cost_logger):
)
)
inner_solution: Program = Program.to([[], delegated_puzzle, []])
lineage_proof: LineageProof = singleton_top_layer.lineage_proof_for_coinsol(singleton_even_coinsol) # noqa
lineage_proof: LineageProof = singleton_top_layer.lineage_proof_for_coinsol(singleton_even_coinsol)
puzzle_reveal: Program = singleton_top_layer.puzzle_for_singleton(
launcher_id,
adapted_puzzle,
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/cmds/cmd_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def run_cli_command(capsys: object, chia_root: Path, command_list: list[str]) ->
exited_cleanly = True
argv_temp = sys.argv
try:
sys.argv = ["chia", "--root-path", str(chia_root)] + command_list
sys.argv = ["chia", "--root-path", str(chia_root), *command_list]
chia_cli()
except SystemExit as e:
if e.code != 0:
Expand Down
7 changes: 3 additions & 4 deletions chia/_tests/cmds/test_click_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from dataclasses import dataclass, field
from decimal import Decimal
from pathlib import Path
from typing import Any, cast
Expand Down Expand Up @@ -37,11 +38,9 @@
overflow_decimal = Decimal(overflow_decimal_str)


@dataclass
class FakeContext:
obj: dict[Any, Any] = {}

def __init__(self, obj: dict[Any, Any]):
self.obj = obj
obj: dict[Any, Any] = field(default_factory=dict)
Quexington marked this conversation as resolved.
Show resolved Hide resolved


def test_click_tx_fee_type() -> None:
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/cmds/wallet/test_coins.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ async def combine_coins(
"Transactions would combine up to 500 coins",
f"To get status, use command: chia wallet get_transaction -f {FINGERPRINT} -tx 0x{STD_TX.name.hex()}",
]
run_cli_command_and_assert(capsys, root_dir, command_args + ["--override"], assert_list)
run_cli_command_and_assert(capsys, root_dir, [*command_args, "--override"], assert_list)
expected_tx_config = TXConfig(
min_coin_amount=uint64(100_000_000_000),
max_coin_amount=uint64(200_000_000_000),
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/cmds/wallet/test_did.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_did_sign_message(capsys: object, get_test_cli_clients: tuple[TestRpcCli
f"Signature: {bytes([7] * 576).hex()}",
f"Signing Mode: {SigningMode.CHIP_0002.value}",
]
run_cli_command_and_assert(capsys, root_dir, command_args + [f"-i{did_id}"], assert_list)
run_cli_command_and_assert(capsys, root_dir, [*command_args, f"-i{did_id}"], assert_list)
expected_calls: logType = {
"sign_message_by_id": [(did_id, message.hex())], # xch std
}
Expand Down
5 changes: 3 additions & 2 deletions chia/_tests/cmds/wallet/test_nft.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_nft_sign_message(capsys: object, get_test_cli_clients: tuple[TestRpcCli
f"Signature: {bytes([7] * 576).hex()}",
f"Signing Mode: {SigningMode.CHIP_0002.value}",
]
run_cli_command_and_assert(capsys, root_dir, command_args + [f"-i{did_id}"], assert_list)
run_cli_command_and_assert(capsys, root_dir, [*command_args, f"-i{did_id}"], assert_list)
expected_calls: logType = {
"sign_message_by_id": [(did_id, message.hex())], # xch std
}
Expand Down Expand Up @@ -370,7 +370,8 @@ async def list_nfts(self, wallet_id: int, num: int = 50, start_index: int = 0) -
assert_list = [
"https://example.com/data",
"did:chia:1qgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpq4msw0c",
] + launcher_ids
*launcher_ids,
]
run_cli_command_and_assert(capsys, root_dir, command_args, assert_list)
expected_calls: logType = {
"list_nfts": [(4, 10, 50)],
Expand Down
6 changes: 3 additions & 3 deletions chia/_tests/cmds/wallet/test_vcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ async def vc_add_proofs(self, proofs: dict[str, Any]) -> None:
run_cli_command_and_assert(capsys, root_dir, command_args, assert_list)

root_assert_list = ["Proof Hash: 5fdf0dfd1fddc56c0f9f68fdb31390721625321ce79f5606b3d2c6ddebbf2a79"]
run_cli_command_and_assert(capsys, root_dir, command_args + ["-r"], root_assert_list)
run_cli_command_and_assert(capsys, root_dir, [*command_args, "-r"], root_assert_list)

expected_calls: logType = {"vc_add_proofs": [({new_proof: "1"},)]}
test_rpc_clients.wallet_rpc_client.check_log(expected_calls)
Expand Down Expand Up @@ -278,8 +278,8 @@ async def vc_revoke(
]
# these are various things that should be in the output
assert_list = ["VC successfully revoked!", f"Transaction {get_bytes32(2).hex()}"]
run_cli_command_and_assert(capsys, root_dir, command_args + [f"-p{parent_id.hex()}"], assert_list)
run_cli_command_and_assert(capsys, root_dir, command_args + [f"-l{vc_id.hex()}"], assert_list)
run_cli_command_and_assert(capsys, root_dir, [*command_args, f"-p{parent_id.hex()}"], assert_list)
run_cli_command_and_assert(capsys, root_dir, [*command_args, f"-l{vc_id.hex()}"], assert_list)
expected_calls: logType = {
"vc_get": [(vc_id,)],
"vc_revoke": [
Expand Down
30 changes: 15 additions & 15 deletions chia/_tests/cmds/wallet/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ def test_get_transaction(capsys: object, get_test_cli_clients: tuple[TestRpcClie
"Amount sent: 12345.678 test1",
"To address: xch1qyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqs0wg4qq",
]
run_cli_command_and_assert(capsys, root_dir, command_args + [FINGERPRINT_ARG], assert_list)
run_cli_command_and_assert(capsys, root_dir, command_args + [FINGERPRINT_ARG, "-v"], v_assert_list)
run_cli_command_and_assert(capsys, root_dir, command_args + [CAT_FINGERPRINT_ARG], cat_assert_list)
run_cli_command_and_assert(capsys, root_dir, [*command_args, FINGERPRINT_ARG], assert_list)
run_cli_command_and_assert(capsys, root_dir, [*command_args, FINGERPRINT_ARG, "-v"], v_assert_list)
run_cli_command_and_assert(capsys, root_dir, [*command_args, CAT_FINGERPRINT_ARG], cat_assert_list)
# these are various things that should be in the output
expected_calls: logType = {
"get_wallets": [(None,), (None,), (None,)],
Expand Down Expand Up @@ -183,7 +183,7 @@ async def get_coin_records(self, request: GetCoinRecords) -> dict[str, Any]:
"'type': 7", # clawback tx
]
run_cli_command_and_assert(capsys, root_dir, command_args, assert_list)
run_cli_command_and_assert(capsys, root_dir, command_args + ["-v"], v_assert_list)
run_cli_command_and_assert(capsys, root_dir, [*command_args, "-v"], v_assert_list)
# these are various things that should be in the output
expected_coin_id = Coin(get_bytes32(4), get_bytes32(5), uint64(12345678)).name()
expected_calls: logType = {
Expand Down Expand Up @@ -288,7 +288,7 @@ async def get_connections(
"47482/47482 01010101... May 12",
]
run_cli_command_and_assert(capsys, root_dir, command_args, assert_list)
run_cli_command_and_assert(capsys, root_dir, command_args + ["--wallet_type", "cat"], other_assert_list)
run_cli_command_and_assert(capsys, root_dir, [*command_args, "--wallet_type", "cat"], other_assert_list)
# these are various things that should be in the output
expected_calls: logType = {
"get_wallets": [(None,), (WalletType.CAT,)],
Expand Down Expand Up @@ -409,10 +409,10 @@ async def cat_spend(
]
with CliRunner().isolated_filesystem():
run_cli_command_and_assert(
capsys, root_dir, command_args + [FINGERPRINT_ARG] + ["--transaction-file=temp"], assert_list
capsys, root_dir, [*command_args, FINGERPRINT_ARG, "--transaction-file=temp"], assert_list
)
run_cli_command_and_assert(
capsys, root_dir, command_args + [CAT_FINGERPRINT_ARG] + ["--transaction-file=temp2"], cat_assert_list
capsys, root_dir, [*command_args, CAT_FINGERPRINT_ARG, "--transaction-file=temp2"], cat_assert_list
)

with open("temp", "rb") as file:
Expand Down Expand Up @@ -490,8 +490,8 @@ async def get_next_address(self, wallet_id: int, new_address: bool) -> str:
WALLET_ID_ARG,
FINGERPRINT_ARG,
]
run_cli_command_and_assert(capsys, root_dir, command_args + ["-n"], [addr1])
run_cli_command_and_assert(capsys, root_dir, command_args + ["-l"], [addr2])
run_cli_command_and_assert(capsys, root_dir, [*command_args, "-n"], [addr1])
run_cli_command_and_assert(capsys, root_dir, [*command_args, "-l"], [addr2])
# these are various things that should be in the output
expected_calls: logType = {
"get_next_address": [(1, True), (1, False)],
Expand Down Expand Up @@ -616,7 +616,7 @@ def test_sign_message(capsys: object, get_test_cli_clients: tuple[TestRpcClients
f"Signature: {bytes([6] * 576).hex()}",
f"Signing Mode: {SigningMode.CHIP_0002.value}",
]
run_cli_command_and_assert(capsys, root_dir, command_args + [f"-a{xch_addr}"], assert_list)
run_cli_command_and_assert(capsys, root_dir, [*command_args, f"-a{xch_addr}"], assert_list)
expected_calls: logType = {
"sign_message_by_address": [(xch_addr, message.hex())], # xch std
}
Expand Down Expand Up @@ -660,8 +660,8 @@ async def set_cat_name(self, wallet_id: int, name: str) -> None:
command_args = ["wallet", "add_token", FINGERPRINT_ARG, "-nexamplecat"]
assert_list = [f"Successfully renamed test1 with wallet_id 2 on key {FINGERPRINT} to examplecat"]
other_assert_list = [f"Successfully added examplecat with wallet id 3 on key {FINGERPRINT}"]
run_cli_command_and_assert(capsys, root_dir, command_args + ["--asset-id", get_bytes32(1).hex()], assert_list)
run_cli_command_and_assert(capsys, root_dir, command_args + ["--asset-id", get_bytes32(3).hex()], other_assert_list)
run_cli_command_and_assert(capsys, root_dir, [*command_args, "--asset-id", get_bytes32(1).hex()], assert_list)
run_cli_command_and_assert(capsys, root_dir, [*command_args, "--asset-id", get_bytes32(3).hex()], other_assert_list)
# these are various things that should be in the output

expected_calls: logType = {
Expand All @@ -685,7 +685,7 @@ def test_make_offer_bad_filename(
"wallet",
"make_offer",
FINGERPRINT_ARG,
f"-p{str(tmp_path)}",
f"-p{tmp_path!s}",
Quexington marked this conversation as resolved.
Show resolved Hide resolved
"--reuse",
"-m0.5",
"--offer",
Expand All @@ -705,7 +705,7 @@ def test_make_offer_bad_filename(
"wallet",
"make_offer",
FINGERPRINT_ARG,
f"-p{str(test_file)}",
f"-p{test_file!s}",
"--reuse",
"-m0.5",
"--offer",
Expand Down Expand Up @@ -773,7 +773,7 @@ async def create_offer_for_ids(
"wallet",
"make_offer",
FINGERPRINT_ARG,
f"-p{str(tmp_path / 'test.offer')}",
f"-p{tmp_path / 'test.offer'!s}",
"--reuse",
"-m0.5",
"--offer",
Expand Down
6 changes: 3 additions & 3 deletions chia/_tests/core/cmds/test_beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def test_prepare_submission(
assert output in result.output

if exit_code == 0:
submission_file = list(beta_path.rglob("*.zip"))[0]
submission_file = next(iter(beta_path.rglob("*.zip")))
assert submission_file.name.startswith(f"submission_{choice - 1}")
with zipfile.ZipFile(submission_file) as zip_file:
all_files = [Path(info.filename) for info in zip_file.filelist]
Expand Down Expand Up @@ -378,5 +378,5 @@ def test_beta_status(root_path_populated_with_config: Path, enabled: bool, path:

assert result.exit_code == 0
assert f"enabled: {enabled}" in result.output
assert f"path: {str(path)}" in result.output
assert f"metrics log interval: {str(metrics_log_interval_default)}" in result.output
assert f"path: {path!s}" in result.output
assert f"metrics log interval: {metrics_log_interval_default!s}" in result.output
Loading
Loading