Skip to content

Commit

Permalink
Replace black with Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
Quexington committed Nov 1, 2024
1 parent 0ba4867 commit 863ce63
Show file tree
Hide file tree
Showing 46 changed files with 124 additions and 201 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/upload-pypi-source.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ jobs:
python:
- major_dot_minor: "3.10"
check:
- name: black
command: black --check --diff .
- name: ruff
command: ruff format --check --diff .
- name: generated protocol tests
command: |
python3 -m chia._tests.util.build_network_protocol_files
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ repos:
pass_filenames: false
- repo: local
hooks:
- id: black
name: black
entry: ./activated.py black
- id: ruff_format
name: ruff format
entry: ./activated.py ruff format
language: system
require_serial: true
types_or: [python, pyi]
Expand Down
8 changes: 3 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ to configure how the tests are run. For example, for more logging: change the lo
```bash
sh install.sh -d
. ./activate
black . && ruff check --fix && mypy && pylint benchmarks build_scripts chia tests tools *.py
ruff format && ruff check --fix && mypy && pylint benchmarks build_scripts chia tests tools *.py
py.test tests -v --durations 0
```

The [black library](https://black.readthedocs.io/en/stable/) is used as an automatic style formatter to make things easier.
The [Mypy library](https://mypy.readthedocs.io/en/stable/) is very useful for ensuring objects are of the correct type, so try to always add the type of the return value, and the type of local variables.
The [Ruff library](https://docs.astral.sh) is used to sort, group, validate imports, ensure consistent style, and further lint all of the python files
The [Ruff library](https://docs.astral.sh) is used to format, sort, group, validate imports, ensure consistent style, and further lint all of the python files

If you want verbose logging for tests, edit the `tests/pytest.ini` file.

Expand All @@ -83,8 +82,7 @@ provided configuration with `pre-commit install`.
2. Set the environment to `./venv/bin/python`
3. Install mypy plugin
4. Preferences > Settings > Python > Linting > mypy enabled
5. Preferences > Settings > Formatting > Python > Provider > black
6. Preferences > Settings > mypy > Targets: set to `./chia`
5. Preferences > Settings > mypy > Targets: set to `./chia`

## Configure Pycharm

Expand Down
2 changes: 0 additions & 2 deletions chia/_tests/blockchain/test_augmented_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

@dataclass
class NullBlockchain:

if TYPE_CHECKING:
from chia.consensus.blockchain_interface import BlocksProtocol

Expand Down Expand Up @@ -72,7 +71,6 @@ def BR(b: FullBlock) -> BlockRecord:
@pytest.mark.anyio
@pytest.mark.limit_consensus_modes(reason="save time")
async def test_augmented_chain(default_10000_blocks: list[FullBlock]) -> None:

blocks = default_10000_blocks
# this test blockchain is expected to have block generators at these
# heights:
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/core/daemon/test_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,7 +1404,7 @@ async def test_misc_daemon_ws(

@pytest.mark.anyio
async def test_unexpected_json(
daemon_connection_and_temp_keychain: tuple[aiohttp.ClientWebSocketResponse, Keychain]
daemon_connection_and_temp_keychain: tuple[aiohttp.ClientWebSocketResponse, Keychain],
) -> None:
ws, _ = daemon_connection_and_temp_keychain

Expand Down
1 change: 0 additions & 1 deletion chia/_tests/core/full_node/stores/test_full_node_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,6 @@ async def test_basic_store(

fork_info = ForkInfo(-1, -1, blockchain.constants.GENESIS_CHALLENGE)
for block in blocks_reorg:

peak = blockchain.get_peak()
assert peak is not None

Expand Down
1 change: 0 additions & 1 deletion chia/_tests/core/full_node/test_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,6 @@ async def test_agg_sig_illegal_suffix(
bt: BlockTools,
consensus_mode: ConsensusMode,
) -> None:

c = bt.constants

additional_data = agg_sig_additional_data(c.AGG_SIG_ME_ADDITIONAL_DATA)
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/core/mempool/test_mempool_fee_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
async def test_protocol_messages(
simulator_and_wallet: tuple[
list[Union[FullNodeAPI, FullNodeSimulator]], list[tuple[Wallet, ChiaServer]], BlockTools
]
],
) -> None:
full_nodes, _wallets, bt = simulator_and_wallet
a_wallet = bt.get_pool_wallet_tool()
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/core/server/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ async def test_call_api_of_specific(

@pytest.mark.anyio
async def test_call_api_of_specific_for_missing_peer(
two_nodes: tuple[FullNodeAPI, FullNodeAPI, ChiaServer, ChiaServer, BlockTools]
two_nodes: tuple[FullNodeAPI, FullNodeAPI, ChiaServer, ChiaServer, BlockTools],
) -> None:
_, _, server_1, server_2, _ = two_nodes

Expand Down
6 changes: 3 additions & 3 deletions chia/_tests/environments/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ async def process_pending_states(
for env in self.environments:
ph_indexes: dict[uint32, Optional[DerivationRecord]] = {}
for wallet_id in env.wallet_state_manager.wallets:
ph_indexes[wallet_id] = (
await env.wallet_state_manager.puzzle_store.get_current_derivation_record_for_wallet(wallet_id)
)
ph_indexes[
wallet_id
] = await env.wallet_state_manager.puzzle_store.get_current_derivation_record_for_wallet(wallet_id)
puzzle_hash_indexes.append(ph_indexes)

pending_txs: list[list[TransactionRecord]] = []
Expand Down
6 changes: 3 additions & 3 deletions chia/_tests/farmer_harvester/test_farmer_harvester.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async def update_harvester_config(harvester_rpc_port: Optional[int], root_path:

@pytest.mark.anyio
async def test_start_with_empty_keychain(
farmer_one_harvester_not_started: tuple[list[HarvesterService], FarmerService, BlockTools]
farmer_one_harvester_not_started: tuple[list[HarvesterService], FarmerService, BlockTools],
) -> None:
_, farmer_service, bt = farmer_one_harvester_not_started
farmer: Farmer = farmer_service._node
Expand All @@ -67,7 +67,7 @@ async def test_start_with_empty_keychain(

@pytest.mark.anyio
async def test_harvester_handshake(
farmer_one_harvester_not_started: tuple[list[HarvesterService], FarmerService, BlockTools]
farmer_one_harvester_not_started: tuple[list[HarvesterService], FarmerService, BlockTools],
) -> None:
harvesters, farmer_service, bt = farmer_one_harvester_not_started
harvester_service = harvesters[0]
Expand Down Expand Up @@ -212,7 +212,7 @@ def check_config_match(config1: dict[str, Any], config2: dict[str, Any]) -> None

@pytest.mark.anyio
async def test_missing_signage_point(
farmer_one_harvester: tuple[list[HarvesterService], FarmerService, BlockTools]
farmer_one_harvester: tuple[list[HarvesterService], FarmerService, BlockTools],
) -> None:
_, farmer_service, _bt = farmer_one_harvester
farmer_api = farmer_service._api
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ def add_tx_called_fun(self: FeeTracker, mitem: MempoolItem) -> None:

# Replace with test method
mempool.fee_estimator.tracker.add_tx = types.MethodType( # type: ignore[attr-defined]
add_tx_called_fun, mempool.fee_estimator.tracker # type: ignore[attr-defined]
add_tx_called_fun,
mempool.fee_estimator.tracker, # type: ignore[attr-defined]
)

mempool.add_to_pool(item)
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/fee_estimation/test_fee_estimation_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def setup_node_and_rpc(

@pytest.fixture(scope="function")
async def one_node_no_blocks(
one_node: tuple[list[SimulatorFullNodeService], list[WalletService], BlockTools]
one_node: tuple[list[SimulatorFullNodeService], list[WalletService], BlockTools],
) -> tuple[FullNodeRpcClient, FullNodeRpcApi]:
full_nodes, _wallets, bt = one_node
full_node_apis = [full_node_service._api for full_node_service in full_nodes]
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/plot_sync/test_plot_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ async def test_farmer_restart(environment: Environment) -> None:

@pytest.mark.anyio
async def test_sync_start_and_disconnect_while_sync_is_active(
farmer_one_harvester: tuple[list[HarvesterService], FarmerService, BlockTools]
farmer_one_harvester: tuple[list[HarvesterService], FarmerService, BlockTools],
) -> None:
harvesters, farmer_service, _ = farmer_one_harvester
harvester_service = harvesters[0]
Expand Down
3 changes: 1 addition & 2 deletions chia/_tests/util/build_network_protocol_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,8 @@ def main() -> None:
for name, function in name_to_function.items():
path = tests_dir.joinpath(name)
path.write_text(function())
# black seems to have trouble when run as a module so not using `python -m black`
subprocess.run(
[scripts_path.joinpath("black"), os.fspath(path.relative_to(tests_dir))],
[scripts_path.joinpath("ruff"), "format", os.fspath(path.relative_to(tests_dir))],
check=True,
cwd=tests_dir,
)
Expand Down
1 change: 0 additions & 1 deletion chia/_tests/util/test_network_protocol_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def parse_blob(input_bytes: bytes) -> tuple[bytes, bytes]:


def test_protocol_bytes() -> None:

filename: Path = get_network_protocol_filename()
assert filename.exists()
with open(filename, "rb") as f:
Expand Down
18 changes: 15 additions & 3 deletions chia/_tests/wallet/dao_wallet/test_dao_clvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,11 @@ def test_validator() -> None:

# Setup the spend_p2_singleton (proposal inner puz)
spend_p2_singleton = SPEND_P2_SINGLETON_MOD.curry(
treasury_struct, CAT_MOD_HASH, conditions, [], p2_singleton_puzhash # tailhash conds
treasury_struct,
CAT_MOD_HASH,
conditions,
[],
p2_singleton_puzhash, # tailhash conds
)
spend_p2_singleton_puzhash = spend_p2_singleton.get_tree_hash()

Expand Down Expand Up @@ -820,7 +824,11 @@ def test_treasury() -> None:

# Setup the spend_p2_singleton (proposal inner puz)
spend_p2_singleton = SPEND_P2_SINGLETON_MOD.curry(
treasury_struct, CAT_MOD_HASH, conditions, [], p2_singleton_puzhash # tailhash conds
treasury_struct,
CAT_MOD_HASH,
conditions,
[],
p2_singleton_puzhash, # tailhash conds
)
spend_p2_singleton_puzhash = spend_p2_singleton.get_tree_hash()

Expand Down Expand Up @@ -1067,7 +1075,11 @@ def test_proposal_lifecycle() -> None:

# Setup the spend_p2_singleton (proposal inner puz)
spend_p2_singleton = SPEND_P2_SINGLETON_MOD.curry(
treasury_singleton_struct, CAT_MOD_HASH, conditions, [], p2_singleton_puzhash # tailhash conds
treasury_singleton_struct,
CAT_MOD_HASH,
conditions,
[],
p2_singleton_puzhash, # tailhash conds
)
spend_p2_singleton_puzhash = spend_p2_singleton.get_tree_hash()

Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/wallet/sync/test_wallet_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ async def new_func(
request_puzzle_solution_failure_tested = False

def flaky_request_puzzle_solution(
func: Callable[[wallet_protocol.RequestPuzzleSolution], Awaitable[Optional[Message]]]
func: Callable[[wallet_protocol.RequestPuzzleSolution], Awaitable[Optional[Message]]],
) -> Callable[[wallet_protocol.RequestPuzzleSolution], Awaitable[Optional[Message]]]:
@functools.wraps(func)
async def new_func(request: wallet_protocol.RequestPuzzleSolution) -> Optional[Message]:
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/wallet/test_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def test_announcement_inversions(
tuple[type[CreateCoinAnnouncement], type[AssertCoinAnnouncement]],
tuple[type[CreatePuzzleAnnouncement], type[AssertPuzzleAnnouncement]],
tuple[type[CreateAnnouncement], type[AssertAnnouncement]],
]
],
) -> None:
create_driver, assert_driver = drivers
# mypy is not smart enough to understand that this `if` narrows down the potential types it could be
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/wallet/test_new_wallet_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ async def test_request_puzzle_state(one_node: OneNode, self_hostname: str) -> No
simulator, _, peer = await connect_to_simulator(one_node, self_hostname)

# Farm block to a puzzle hash we aren't looking at
await simulator.farm_blocks_to_puzzlehash(3, farm_to=bytes32(b"\x0A" * 32))
await simulator.farm_blocks_to_puzzlehash(3, farm_to=bytes32(b"\x0a" * 32))

genesis = simulator.full_node.blockchain.constants.GENESIS_CHALLENGE

Expand Down
6 changes: 3 additions & 3 deletions chia/_tests/wallet/test_signer_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ async def test_p2dohp_wallet_signer_protocol(wallet_environments: WalletTestFram
solution,
)

derivation_record: Optional[DerivationRecord] = (
await wallet_state_manager.puzzle_store.get_derivation_record_for_puzzle_hash(coin.puzzle_hash)
)
derivation_record: Optional[
DerivationRecord
] = await wallet_state_manager.puzzle_store.get_derivation_record_for_puzzle_hash(coin.puzzle_hash)
assert derivation_record is not None
pubkey: G1Element = derivation_record.pubkey
atom = puzzle.uncurry()[1].at("f").atom
Expand Down
4 changes: 1 addition & 3 deletions chia/_tests/wallet/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ def test_cs_config() -> None:
{
"excluded_coins": [coin_to_exclude.to_json_dict()],
}
).override(
max_coin_amount=100
).autofill(constants=DEFAULT_CONSTANTS).to_json_dict() == {
).override(max_coin_amount=100).autofill(constants=DEFAULT_CONSTANTS).to_json_dict() == {
**default_cs_config,
"excluded_coin_ids": ["0x" + coin_to_exclude.name().hex()],
"max_coin_amount": 100,
Expand Down
32 changes: 16 additions & 16 deletions chia/_tests/wallet/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1113,10 +1113,10 @@ async def test_clawback_resync(self, self_hostname: str, wallet_environments: Wa
await time_out_assert(20, wsm_2.coin_store.count_small_unspent, 0, 1000, CoinType.CLAWBACK)

before_txs: dict[str, dict[TransactionType, int]] = {"sender": {}, "recipient": {}}
before_txs["sender"][TransactionType.INCOMING_CLAWBACK_SEND] = (
await wsm_1.tx_store.get_transaction_count_for_wallet(
1, type_filter=TransactionTypeFilter.include([TransactionType.INCOMING_CLAWBACK_SEND])
)
before_txs["sender"][
TransactionType.INCOMING_CLAWBACK_SEND
] = await wsm_1.tx_store.get_transaction_count_for_wallet(
1, type_filter=TransactionTypeFilter.include([TransactionType.INCOMING_CLAWBACK_SEND])
)
before_txs["sender"][TransactionType.OUTGOING_CLAWBACK] = await wsm_1.tx_store.get_transaction_count_for_wallet(
1, type_filter=TransactionTypeFilter.include([TransactionType.OUTGOING_CLAWBACK])
Expand All @@ -1130,10 +1130,10 @@ async def test_clawback_resync(self, self_hostname: str, wallet_environments: Wa
before_txs["sender"][TransactionType.COINBASE_REWARD] = await wsm_1.tx_store.get_transaction_count_for_wallet(
1, type_filter=TransactionTypeFilter.include([TransactionType.COINBASE_REWARD])
)
before_txs["recipient"][TransactionType.INCOMING_CLAWBACK_RECEIVE] = (
await wsm_2.tx_store.get_transaction_count_for_wallet(
1, type_filter=TransactionTypeFilter.include([TransactionType.INCOMING_CLAWBACK_RECEIVE])
)
before_txs["recipient"][
TransactionType.INCOMING_CLAWBACK_RECEIVE
] = await wsm_2.tx_store.get_transaction_count_for_wallet(
1, type_filter=TransactionTypeFilter.include([TransactionType.INCOMING_CLAWBACK_RECEIVE])
)
# Resync start
env_1.node._close()
Expand All @@ -1160,10 +1160,10 @@ async def test_clawback_resync(self, self_hostname: str, wallet_environments: Wa
wsm_2 = env_2.node.wallet_state_manager

after_txs: dict[str, dict[TransactionType, int]] = {"sender": {}, "recipient": {}}
after_txs["sender"][TransactionType.INCOMING_CLAWBACK_SEND] = (
await wsm_1.tx_store.get_transaction_count_for_wallet(
1, type_filter=TransactionTypeFilter.include([TransactionType.INCOMING_CLAWBACK_SEND])
)
after_txs["sender"][
TransactionType.INCOMING_CLAWBACK_SEND
] = await wsm_1.tx_store.get_transaction_count_for_wallet(
1, type_filter=TransactionTypeFilter.include([TransactionType.INCOMING_CLAWBACK_SEND])
)
after_txs["sender"][TransactionType.OUTGOING_CLAWBACK] = await wsm_1.tx_store.get_transaction_count_for_wallet(
1, type_filter=TransactionTypeFilter.include([TransactionType.OUTGOING_CLAWBACK])
Expand All @@ -1177,10 +1177,10 @@ async def test_clawback_resync(self, self_hostname: str, wallet_environments: Wa
after_txs["sender"][TransactionType.COINBASE_REWARD] = await wsm_1.tx_store.get_transaction_count_for_wallet(
1, type_filter=TransactionTypeFilter.include([TransactionType.COINBASE_REWARD])
)
after_txs["recipient"][TransactionType.INCOMING_CLAWBACK_RECEIVE] = (
await wsm_2.tx_store.get_transaction_count_for_wallet(
1, type_filter=TransactionTypeFilter.include([TransactionType.INCOMING_CLAWBACK_RECEIVE])
)
after_txs["recipient"][
TransactionType.INCOMING_CLAWBACK_RECEIVE
] = await wsm_2.tx_store.get_transaction_count_for_wallet(
1, type_filter=TransactionTypeFilter.include([TransactionType.INCOMING_CLAWBACK_RECEIVE])
)
# Check clawback
clawback_tx_1 = await wsm_1.tx_store.get_transaction_record(clawback_coin_id_1)
Expand Down
2 changes: 1 addition & 1 deletion chia/_tests/wallet/test_wallet_coin_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ async def test_get_coin_records_total_count_cache() -> None:
assert (await store.get_coin_records(include_total_count=True)).total_count == 3
# Make sure the total count increases for the same query when rolling back
assert (await store.get_coin_records(include_total_count=True)).total_count == 3
await store.rollback_to_block(0),
(await store.rollback_to_block(0),)
assert (await store.get_coin_records(include_total_count=True)).total_count == 0


Expand Down
1 change: 0 additions & 1 deletion chia/cmds/cmds_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ class TransactionBundle(Streamable):
def tx_out_cmd(
enable_timelock_args: Optional[bool] = None,
) -> Callable[[Callable[..., list[TransactionRecord]]], Callable[..., None]]:

def _tx_out_cmd(func: Callable[..., list[TransactionRecord]]) -> Callable[..., None]:
@timelock_args(enable=enable_timelock_args)
def original_cmd(transaction_file: Optional[str] = None, **kwargs: Any) -> None:
Expand Down
2 changes: 1 addition & 1 deletion chia/cmds/keys_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ def private_key_from_mnemonic_seed_file(filename: Path) -> PrivateKey:


def resolve_derivation_master_key(
fingerprint_or_filename: Optional[Union[int, str, Path]]
fingerprint_or_filename: Optional[Union[int, str, Path]],
) -> tuple[Optional[int], Optional[PrivateKey]]:
"""
Given a key fingerprint of file containing a mnemonic seed, return the private key.
Expand Down
1 change: 0 additions & 1 deletion chia/cmds/wallet_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,6 @@ async def transfer_did(
push: bool,
condition_valid_times: ConditionValidTimes,
) -> list[TransactionRecord]:

async with get_wallet_client(wallet_rpc_port, fp) as (wallet_client, fingerprint, config):
try:
target_address = target_cli_address.original_address
Expand Down
1 change: 0 additions & 1 deletion chia/consensus/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,6 @@ def seen_compact_proofs(self, vdf_info: VDFInfo, height: uint32) -> bool:
return False

async def lookup_block_generators(self, header_hash: bytes32, generator_refs: set[uint32]) -> dict[uint32, bytes]:

generators: dict[uint32, bytes] = {}

# if this is empty, we shouldn't have called this function to begin with
Expand Down
1 change: 0 additions & 1 deletion chia/consensus/multiprocess_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def pre_validate_block(
vs: ValidationState,
validate_signatures: bool,
) -> PreValidationResult:

try:
validation_start = time.monotonic()
tx_additions: list[Coin] = []
Expand Down
Loading

0 comments on commit 863ce63

Please sign in to comment.