Skip to content

Commit 06c3c42

Browse files
authored
Merge pull request #474 from opentensor/feat/thewhaleking/add-ruff-formatter
Add ruff formatter
2 parents 56dd47e + 3a24e4f commit 06c3c42

File tree

7 files changed

+75
-32
lines changed

7 files changed

+75
-32
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Ruff Formatter Check
2+
permissions:
3+
contents: read
4+
5+
on:
6+
pull_request:
7+
types: [opened, synchronize, reopened, edited]
8+
9+
jobs:
10+
ruff:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
python-version: ["3.9.13"]
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
19+
- name: Set up Python ${{ matrix.python-version }}
20+
uses: actions/setup-python@v5
21+
with:
22+
python-version: ${{ matrix.python-version }}
23+
24+
- name: Set up caching for Ruff virtual environment
25+
id: cache-ruff
26+
uses: actions/cache@v4
27+
with:
28+
path: .venv
29+
key: v2-pypi-py-ruff-${{ matrix.python-version }}-${{ hashFiles('pyproject.toml') }}
30+
restore-keys: |
31+
v2-pypi-py-ruff-${{ matrix.python-version }}-
32+
33+
- name: Set up Ruff virtual environment if cache is missed
34+
if: steps.cache-ruff.outputs.cache-hit != 'true'
35+
run: |
36+
python -m venv .venv
37+
.venv/bin/python -m pip install ruff==0.11.5
38+
39+
- name: Ruff format check
40+
run: |
41+
.venv/bin/ruff format --diff bittensor_cli
42+
.venv/bin/ruff format --diff tests

bittensor_cli/src/bittensor/utils.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -840,11 +840,7 @@ def update_metadata_table(table_name: str, values: dict[str, str]) -> None:
840840
"""
841841
with DB() as (conn, cursor):
842842
cursor.execute(
843-
"CREATE TABLE IF NOT EXISTS metadata ("
844-
"TableName TEXT, "
845-
"Key TEXT, "
846-
"Value TEXT"
847-
")"
843+
"CREATE TABLE IF NOT EXISTS metadata (TableName TEXT, Key TEXT, Value TEXT)"
848844
)
849845
conn.commit()
850846
for key, value in values.items():
@@ -1311,7 +1307,7 @@ def validate_rate_tolerance(value: Optional[float]) -> Optional[float]:
13111307
raise typer.BadParameter("Rate tolerance cannot be greater than 1 (100%).")
13121308
if value > 0.5:
13131309
console.print(
1314-
f"[yellow]Warning: High rate tolerance of {value*100}% specified. "
1310+
f"[yellow]Warning: High rate tolerance of {value * 100}% specified. "
13151311
"This may result in unfavorable transaction execution.[/yellow]"
13161312
)
13171313
return value

bittensor_cli/src/commands/stake/add.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ def _define_stake_table(
586586

587587
if safe_staking:
588588
table.add_column(
589-
f"Rate with tolerance: [blue]({rate_tolerance*100}%)[/blue]",
589+
f"Rate with tolerance: [blue]({rate_tolerance * 100}%)[/blue]",
590590
justify="center",
591591
style=COLOR_PALETTE["POOLS"]["RATE"],
592592
)

bittensor_cli/src/commands/stake/remove.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ def _create_unstake_table(
12661266
)
12671267
if safe_staking:
12681268
table.add_column(
1269-
f"Rate with tolerance: [blue]({rate_tolerance*100}%)[/blue]",
1269+
f"Rate with tolerance: [blue]({rate_tolerance * 100}%)[/blue]",
12701270
justify="center",
12711271
style=COLOR_PALETTE["POOLS"]["RATE"],
12721272
)

bittensor_cli/src/commands/sudo.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,8 @@ async def set_take_extrinsic(
525525

526526
if current_take_u16 < take_u16:
527527
console.print(
528-
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}%."
528+
f"Current take is [{COLOR_PALETTE.P.RATE}]{current_take * 100.0:.2f}%[/{COLOR_PALETTE.P.RATE}]. "
529+
f"Increasing to [{COLOR_PALETTE.P.RATE}]{take * 100:.2f}%."
529530
)
530531
with console.status(
531532
f":satellite: Sending decrease_take_extrinsic call on [white]{subtensor}[/white] ..."
@@ -542,7 +543,8 @@ async def set_take_extrinsic(
542543

543544
else:
544545
console.print(
545-
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}%."
546+
f"Current take is [{COLOR_PALETTE.P.RATE}]{current_take * 100.0:.2f}%[/{COLOR_PALETTE.P.RATE}]. "
547+
f"Decreasing to [{COLOR_PALETTE.P.RATE}]{take * 100:.2f}%."
546548
)
547549
with console.status(
548550
f":satellite: Sending increase_take_extrinsic call on [white]{subtensor}[/white] ..."
@@ -871,7 +873,7 @@ async def get_current_take(subtensor: "SubtensorInterface", wallet: Wallet):
871873
async def display_current_take(subtensor: "SubtensorInterface", wallet: Wallet) -> None:
872874
current_take = await get_current_take(subtensor, wallet)
873875
console.print(
874-
f"Current take is [{COLOR_PALETTE['POOLS']['RATE']}]{current_take * 100.:.2f}%"
876+
f"Current take is [{COLOR_PALETTE.P.RATE}]{current_take * 100.0:.2f}%"
875877
)
876878

877879

@@ -891,7 +893,9 @@ async def _do_set_take() -> bool:
891893
)
892894
if not len(netuids_registered) > 0:
893895
err_console.print(
894-
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."
896+
f"Hotkey [{COLOR_PALETTE.G.HK}]{wallet.hotkey.ss58_address}[/{COLOR_PALETTE.G.HK}] is not registered to"
897+
f" any subnet. Please register using [{COLOR_PALETTE.G.SUBHEAD}]`btcli subnets register`"
898+
f"[{COLOR_PALETTE.G.SUBHEAD}] and try again."
895899
)
896900
return False
897901

@@ -908,12 +912,12 @@ async def _do_set_take() -> bool:
908912
else:
909913
new_take = await get_current_take(subtensor, wallet)
910914
console.print(
911-
f"New take is [{COLOR_PALETTE['POOLS']['RATE']}]{new_take * 100.:.2f}%"
915+
f"New take is [{COLOR_PALETTE.P.RATE}]{new_take * 100.0:.2f}%"
912916
)
913917
return True
914918

915919
console.print(
916-
f"Setting take on [{COLOR_PALETTE['GENERAL']['LINKS']}]network: {subtensor.network}"
920+
f"Setting take on [{COLOR_PALETTE.G.LINKS}]network: {subtensor.network}"
917921
)
918922

919923
if not unlock_key(wallet, "hot").success and unlock_key(wallet, "cold").success:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ cuda = [
4040
dev = [
4141
"pytest",
4242
"pytest-asyncio",
43+
"ruff==0.11.5",
4344
]
4445

4546
[project.urls]

tests/e2e_tests/test_wallet_creations.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ def verify_key_pattern(output: str, wallet_name: str) -> Optional[str]:
9494
match = re.search(pattern, line)
9595
if match:
9696
# Assert key starts with '5'
97-
assert match.group(1).startswith(
98-
"5"
99-
), f"{wallet_name} should start with '5'"
97+
assert match.group(1).startswith("5"), (
98+
f"{wallet_name} should start with '5'"
99+
)
100100
# Assert length of key is 48 characters
101-
assert (
102-
len(match.group(1)) == 48
103-
), f"Key for {wallet_name} should be 48 characters long"
101+
assert len(match.group(1)) == 48, (
102+
f"Key for {wallet_name} should be 48 characters long"
103+
)
104104
found = True
105105
return match.group(1)
106106

@@ -441,9 +441,9 @@ def test_wallet_regen(wallet_setup, capfd):
441441

442442
new_coldkey_mod_time = os.path.getmtime(coldkey_path)
443443

444-
assert (
445-
initial_coldkey_mod_time != new_coldkey_mod_time
446-
), "Coldkey file was not regenerated as expected"
444+
assert initial_coldkey_mod_time != new_coldkey_mod_time, (
445+
"Coldkey file was not regenerated as expected"
446+
)
447447
json_result = exec_command(
448448
command="wallet",
449449
sub_command="regen-coldkey",
@@ -502,9 +502,9 @@ def test_wallet_regen(wallet_setup, capfd):
502502

503503
new_coldkeypub_mod_time = os.path.getmtime(coldkeypub_path)
504504

505-
assert (
506-
initial_coldkeypub_mod_time != new_coldkeypub_mod_time
507-
), "Coldkeypub file was not regenerated as expected"
505+
assert initial_coldkeypub_mod_time != new_coldkeypub_mod_time, (
506+
"Coldkeypub file was not regenerated as expected"
507+
)
508508
print("Passed wallet regen_coldkeypub command ✅")
509509

510510
# -----------------------------
@@ -537,9 +537,9 @@ def test_wallet_regen(wallet_setup, capfd):
537537

538538
new_hotkey_mod_time = os.path.getmtime(hotkey_path)
539539

540-
assert (
541-
initial_hotkey_mod_time != new_hotkey_mod_time
542-
), "Hotkey file was not regenerated as expected"
540+
assert initial_hotkey_mod_time != new_hotkey_mod_time, (
541+
"Hotkey file was not regenerated as expected"
542+
)
543543
print("Passed wallet regen_hotkey command ✅")
544544

545545

@@ -593,9 +593,9 @@ def test_wallet_balance_all(local_chain, wallet_setup, capfd):
593593
output = result.stdout
594594

595595
for wallet_name in wallet_names:
596-
assert (
597-
wallet_name in output
598-
), f"Wallet {wallet_name} not found in balance --all output"
596+
assert wallet_name in output, (
597+
f"Wallet {wallet_name} not found in balance --all output"
598+
)
599599

600600
json_results = exec_command(
601601
"wallet",

0 commit comments

Comments
 (0)