From 5b2e07bd24530bd484dcc4110af17fb8a9b6d4f4 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Mon, 2 Jun 2025 19:50:08 +0200 Subject: [PATCH 1/3] Adds Ruff Format checker --- .github/workflows/ruff-formatter.yml | 40 ++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 .github/workflows/ruff-formatter.yml diff --git a/.github/workflows/ruff-formatter.yml b/.github/workflows/ruff-formatter.yml new file mode 100644 index 000000000..87ea9ba20 --- /dev/null +++ b/.github/workflows/ruff-formatter.yml @@ -0,0 +1,40 @@ +name: Ruff Formatter Check + +on: + pull_request: + types: [opened, synchronize, reopened, edited] + +jobs: + ruff: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.9.13"] + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - name: Set up caching for Ruff virtual environment + id: cache-ruff + uses: actions/cache@v4 + with: + path: .venv + key: v2-pypi-py-ruff-${{ matrix.python-version }}-${{ hashFiles('pyproject.toml') }} + restore-keys: | + v2-pypi-py-ruff-${{ matrix.python-version }}- + + - name: Set up Ruff virtual environment if cache is missed + if: steps.cache-ruff.outputs.cache-hit != 'true' + run: | + python -m venv .venv + .venv/bin/python -m pip install ruff==0.11.5 + + - name: Ruff format check + run: | + .venv/bin/ruff format --diff bittensor_cli + .venv/bin/ruff format --diff tests From ff9299b3c0f11cf36bd8a72491b4f107f26c0e19 Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Mon, 2 Jun 2025 20:00:01 +0200 Subject: [PATCH 2/3] Run ruff --- bittensor_cli/src/bittensor/utils.py | 8 ++--- bittensor_cli/src/commands/stake/add.py | 2 +- bittensor_cli/src/commands/stake/remove.py | 2 +- bittensor_cli/src/commands/sudo.py | 16 ++++++---- pyproject.toml | 1 + tests/e2e_tests/test_wallet_creations.py | 36 +++++++++++----------- 6 files changed, 33 insertions(+), 32 deletions(-) diff --git a/bittensor_cli/src/bittensor/utils.py b/bittensor_cli/src/bittensor/utils.py index d234fe74b..42f4cc3be 100644 --- a/bittensor_cli/src/bittensor/utils.py +++ b/bittensor_cli/src/bittensor/utils.py @@ -840,11 +840,7 @@ def update_metadata_table(table_name: str, values: dict[str, str]) -> None: """ with DB() as (conn, cursor): cursor.execute( - "CREATE TABLE IF NOT EXISTS metadata (" - "TableName TEXT, " - "Key TEXT, " - "Value TEXT" - ")" + "CREATE TABLE IF NOT EXISTS metadata (TableName TEXT, Key TEXT, Value TEXT)" ) conn.commit() for key, value in values.items(): @@ -1311,7 +1307,7 @@ def validate_rate_tolerance(value: Optional[float]) -> Optional[float]: raise typer.BadParameter("Rate tolerance cannot be greater than 1 (100%).") if value > 0.5: console.print( - f"[yellow]Warning: High rate tolerance of {value*100}% specified. " + f"[yellow]Warning: High rate tolerance of {value * 100}% specified. " "This may result in unfavorable transaction execution.[/yellow]" ) return value diff --git a/bittensor_cli/src/commands/stake/add.py b/bittensor_cli/src/commands/stake/add.py index a4af67fff..38fa6632b 100644 --- a/bittensor_cli/src/commands/stake/add.py +++ b/bittensor_cli/src/commands/stake/add.py @@ -586,7 +586,7 @@ def _define_stake_table( if safe_staking: table.add_column( - f"Rate with tolerance: [blue]({rate_tolerance*100}%)[/blue]", + f"Rate with tolerance: [blue]({rate_tolerance * 100}%)[/blue]", justify="center", style=COLOR_PALETTE["POOLS"]["RATE"], ) diff --git a/bittensor_cli/src/commands/stake/remove.py b/bittensor_cli/src/commands/stake/remove.py index f80201e22..c7a72ffed 100644 --- a/bittensor_cli/src/commands/stake/remove.py +++ b/bittensor_cli/src/commands/stake/remove.py @@ -1266,7 +1266,7 @@ def _create_unstake_table( ) if safe_staking: table.add_column( - f"Rate with tolerance: [blue]({rate_tolerance*100}%)[/blue]", + f"Rate with tolerance: [blue]({rate_tolerance * 100}%)[/blue]", justify="center", style=COLOR_PALETTE["POOLS"]["RATE"], ) diff --git a/bittensor_cli/src/commands/sudo.py b/bittensor_cli/src/commands/sudo.py index 87a54d520..4c8560f83 100644 --- a/bittensor_cli/src/commands/sudo.py +++ b/bittensor_cli/src/commands/sudo.py @@ -525,7 +525,8 @@ async def set_take_extrinsic( if current_take_u16 < take_u16: console.print( - f"Current take is [{COLOR_PALETTE['POOLS']['RATE']}]{current_take * 100.:.2f}%[/{COLOR_PALETTE['POOLS']['RATE']}]. Increasing to [{COLOR_PALETTE['POOLS']['RATE']}]{take * 100:.2f}%." + f"Current take is [{COLOR_PALETTE.P.RATE}]{current_take * 100.0:.2f}%[/{COLOR_PALETTE.P.RATE}]. " + f"Increasing to [{COLOR_PALETTE.P.RATE}]{take * 100:.2f}%." ) with console.status( f":satellite: Sending decrease_take_extrinsic call on [white]{subtensor}[/white] ..." @@ -542,7 +543,8 @@ async def set_take_extrinsic( else: console.print( - f"Current take is [{COLOR_PALETTE['POOLS']['RATE']}]{current_take * 100.:.2f}%[/{COLOR_PALETTE['POOLS']['RATE']}]. Decreasing to [{COLOR_PALETTE['POOLS']['RATE']}]{take * 100:.2f}%." + f"Current take is [{COLOR_PALETTE.P.RATE}]{current_take * 100.0:.2f}%[/{COLOR_PALETTE.P.RATE}]. " + f"Decreasing to [{COLOR_PALETTE.P.RATE}]{take * 100:.2f}%." ) with console.status( f":satellite: Sending increase_take_extrinsic call on [white]{subtensor}[/white] ..." @@ -871,7 +873,7 @@ async def get_current_take(subtensor: "SubtensorInterface", wallet: Wallet): async def display_current_take(subtensor: "SubtensorInterface", wallet: Wallet) -> None: current_take = await get_current_take(subtensor, wallet) console.print( - f"Current take is [{COLOR_PALETTE['POOLS']['RATE']}]{current_take * 100.:.2f}%" + f"Current take is [{COLOR_PALETTE.P.RATE}]{current_take * 100.0:.2f}%" ) @@ -891,7 +893,9 @@ async def _do_set_take() -> bool: ) if not len(netuids_registered) > 0: err_console.print( - f"Hotkey [{COLOR_PALETTE['GENERAL']['HOTKEY']}]{wallet.hotkey.ss58_address}[/{COLOR_PALETTE['GENERAL']['HOTKEY']}] is not registered to any subnet. Please register using [{COLOR_PALETTE['GENERAL']['SUBHEADING']}]`btcli subnets register`[{COLOR_PALETTE['GENERAL']['SUBHEADING']}] and try again." + f"Hotkey [{COLOR_PALETTE.G.HK}]{wallet.hotkey.ss58_address}[/{COLOR_PALETTE.G.HK}] is not registered to" + f" any subnet. Please register using [{COLOR_PALETTE.G.SUBHEAD}]`btcli subnets register`" + f"[{COLOR_PALETTE.G.SUBHEAD}] and try again." ) return False @@ -908,12 +912,12 @@ async def _do_set_take() -> bool: else: new_take = await get_current_take(subtensor, wallet) console.print( - f"New take is [{COLOR_PALETTE['POOLS']['RATE']}]{new_take * 100.:.2f}%" + f"New take is [{COLOR_PALETTE.P.RATE}]{new_take * 100.0:.2f}%" ) return True console.print( - f"Setting take on [{COLOR_PALETTE['GENERAL']['LINKS']}]network: {subtensor.network}" + f"Setting take on [{COLOR_PALETTE.G.LINKS}]network: {subtensor.network}" ) if not unlock_key(wallet, "hot").success and unlock_key(wallet, "cold").success: diff --git a/pyproject.toml b/pyproject.toml index 7385a1396..459c49deb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,6 +40,7 @@ cuda = [ dev = [ "pytest", "pytest-asyncio", + "ruff==0.11.5", ] [project.urls] diff --git a/tests/e2e_tests/test_wallet_creations.py b/tests/e2e_tests/test_wallet_creations.py index 5dd3bd63c..7b573dbba 100644 --- a/tests/e2e_tests/test_wallet_creations.py +++ b/tests/e2e_tests/test_wallet_creations.py @@ -94,13 +94,13 @@ def verify_key_pattern(output: str, wallet_name: str) -> Optional[str]: match = re.search(pattern, line) if match: # Assert key starts with '5' - assert match.group(1).startswith( - "5" - ), f"{wallet_name} should start with '5'" + assert match.group(1).startswith("5"), ( + f"{wallet_name} should start with '5'" + ) # Assert length of key is 48 characters - assert ( - len(match.group(1)) == 48 - ), f"Key for {wallet_name} should be 48 characters long" + assert len(match.group(1)) == 48, ( + f"Key for {wallet_name} should be 48 characters long" + ) found = True return match.group(1) @@ -441,9 +441,9 @@ def test_wallet_regen(wallet_setup, capfd): new_coldkey_mod_time = os.path.getmtime(coldkey_path) - assert ( - initial_coldkey_mod_time != new_coldkey_mod_time - ), "Coldkey file was not regenerated as expected" + assert initial_coldkey_mod_time != new_coldkey_mod_time, ( + "Coldkey file was not regenerated as expected" + ) json_result = exec_command( command="wallet", sub_command="regen-coldkey", @@ -502,9 +502,9 @@ def test_wallet_regen(wallet_setup, capfd): new_coldkeypub_mod_time = os.path.getmtime(coldkeypub_path) - assert ( - initial_coldkeypub_mod_time != new_coldkeypub_mod_time - ), "Coldkeypub file was not regenerated as expected" + assert initial_coldkeypub_mod_time != new_coldkeypub_mod_time, ( + "Coldkeypub file was not regenerated as expected" + ) print("Passed wallet regen_coldkeypub command ✅") # ----------------------------- @@ -537,9 +537,9 @@ def test_wallet_regen(wallet_setup, capfd): new_hotkey_mod_time = os.path.getmtime(hotkey_path) - assert ( - initial_hotkey_mod_time != new_hotkey_mod_time - ), "Hotkey file was not regenerated as expected" + assert initial_hotkey_mod_time != new_hotkey_mod_time, ( + "Hotkey file was not regenerated as expected" + ) print("Passed wallet regen_hotkey command ✅") @@ -593,9 +593,9 @@ def test_wallet_balance_all(local_chain, wallet_setup, capfd): output = result.stdout for wallet_name in wallet_names: - assert ( - wallet_name in output - ), f"Wallet {wallet_name} not found in balance --all output" + assert wallet_name in output, ( + f"Wallet {wallet_name} not found in balance --all output" + ) json_results = exec_command( "wallet", From 3a24e4f367be34dc96aabd4542d9f12d837c193c Mon Sep 17 00:00:00 2001 From: BD Himes <37844818+thewhaleking@users.noreply.github.com> Date: Mon, 2 Jun 2025 20:00:20 +0200 Subject: [PATCH 3/3] Potential fix for code scanning alert no. 5: Workflow does not contain permissions Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> --- .github/workflows/ruff-formatter.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ruff-formatter.yml b/.github/workflows/ruff-formatter.yml index 87ea9ba20..93166c304 100644 --- a/.github/workflows/ruff-formatter.yml +++ b/.github/workflows/ruff-formatter.yml @@ -1,4 +1,6 @@ name: Ruff Formatter Check +permissions: + contents: read on: pull_request: