Skip to content

Commit e6da74c

Browse files
committed
ruff changes
1 parent d84afd8 commit e6da74c

File tree

446 files changed

+4026
-4305
lines changed

Some content is hidden

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

446 files changed

+4026
-4305
lines changed

benchmarks/block_ref.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from dataclasses import dataclass
77
from pathlib import Path
88
from time import monotonic
9-
from typing import Optional
109

1110
import aiosqlite
1211
import click
@@ -38,7 +37,7 @@
3837
@dataclass(frozen=True)
3938
class BlockInfo:
4039
prev_header_hash: bytes32
41-
transactions_generator: Optional[SerializedProgram]
40+
transactions_generator: SerializedProgram | None
4241
transactions_generator_ref_list: list[uint32]
4342

4443

benchmarks/mempool-long-lived.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from collections.abc import Collection
55
from dataclasses import dataclass
66
from time import monotonic
7-
from typing import Optional
87

98
from chia_rs import CoinSpend, G2Element, SpendBundle
109
from chia_rs.sized_bytes import bytes32
@@ -35,9 +34,9 @@ class BenchBlockRecord:
3534

3635
header_hash: bytes32
3736
height: uint32
38-
timestamp: Optional[uint64]
37+
timestamp: uint64 | None
3938
prev_transaction_block_height: uint32
40-
prev_transaction_block_hash: Optional[bytes32]
39+
prev_transaction_block_hash: bytes32 | None
4140

4241
@property
4342
def is_transaction_block(self) -> bool:
@@ -90,7 +89,7 @@ async def get_coin_record(coin_ids: Collection[bytes32]) -> list[CoinRecord]:
9089
return ret
9190

9291
# We currently don't need to keep track of these for our purpose
93-
async def get_unspent_lineage_info_for_puzzle_hash(_: bytes32) -> Optional[UnspentLineageInfo]:
92+
async def get_unspent_lineage_info_for_puzzle_hash(_: bytes32) -> UnspentLineageInfo | None:
9493
assert False
9594

9695
timestamp = uint64(1631794488)

benchmarks/mempool.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from dataclasses import dataclass
88
from subprocess import check_call
99
from time import monotonic
10-
from typing import Optional
1110

1211
from chia_rs import SpendBundle
1312
from chia_rs.sized_bytes import bytes32
@@ -58,9 +57,9 @@ class BenchBlockRecord:
5857

5958
header_hash: bytes32
6059
height: uint32
61-
timestamp: Optional[uint64]
60+
timestamp: uint64 | None
6261
prev_transaction_block_height: uint32
63-
prev_transaction_block_hash: Optional[bytes32]
62+
prev_transaction_block_hash: bytes32 | None
6463

6564
@property
6665
def is_transaction_block(self) -> bool:
@@ -91,7 +90,7 @@ async def get_coin_records(coin_ids: Collection[bytes32]) -> list[CoinRecord]:
9190
return ret
9291

9392
# We currently don't need to keep track of these for our purpose
94-
async def get_unspent_lineage_info_for_puzzle_hash(_: bytes32) -> Optional[UnspentLineageInfo]:
93+
async def get_unspent_lineage_info_for_puzzle_hash(_: bytes32) -> UnspentLineageInfo | None:
9594
assert False
9695

9796
wt = WalletTool(DEFAULT_CONSTANTS)

benchmarks/streamable.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import json
44
import random
55
import sys
6+
from collections.abc import Callable
67
from dataclasses import dataclass
78
from enum import Enum
89
from statistics import stdev
910
from time import process_time as clock
10-
from typing import Any, Callable, Optional, TextIO, Union
11+
from typing import Any, TextIO
1112

1213
import click
1314
from chia_rs import FullBlock
@@ -43,8 +44,8 @@ class BenchmarkMiddle(Streamable):
4344
@streamable
4445
@dataclass(frozen=True)
4546
class BenchmarkClass(Streamable):
46-
a: Optional[BenchmarkMiddle]
47-
b: Optional[BenchmarkMiddle]
47+
a: BenchmarkMiddle | None
48+
b: BenchmarkMiddle | None
4849
c: BenchmarkMiddle
4950
d: list[BenchmarkMiddle]
5051
e: tuple[BenchmarkMiddle, BenchmarkMiddle, BenchmarkMiddle]
@@ -64,8 +65,8 @@ def get_random_middle() -> BenchmarkMiddle:
6465

6566

6667
def get_random_benchmark_object() -> BenchmarkClass:
67-
a: Optional[BenchmarkMiddle] = None
68-
b: Optional[BenchmarkMiddle] = get_random_middle()
68+
a: BenchmarkMiddle | None = None
69+
b: BenchmarkMiddle | None = get_random_middle()
6970
c: BenchmarkMiddle = get_random_middle()
7071
d: list[BenchmarkMiddle] = [get_random_middle() for _ in range(5)]
7172
e: tuple[BenchmarkMiddle, BenchmarkMiddle, BenchmarkMiddle] = (
@@ -79,10 +80,10 @@ def get_random_benchmark_object() -> BenchmarkClass:
7980
def print_row(
8081
*,
8182
mode: str,
82-
us_per_iteration: Union[str, float],
83-
stdev_us_per_iteration: Union[str, float],
84-
avg_iterations: Union[str, int],
85-
stdev_iterations: Union[str, float],
83+
us_per_iteration: str | float,
84+
stdev_us_per_iteration: str | float,
85+
avg_iterations: str | int,
86+
stdev_iterations: str | float,
8687
end: str = "\n",
8788
) -> None:
8889
print(
@@ -142,14 +143,14 @@ def to_bytes(obj: Any) -> bytes:
142143
@dataclass
143144
class ModeParameter:
144145
conversion_cb: Callable[[Any], Any]
145-
preparation_cb: Optional[Callable[[Any], Any]] = None
146+
preparation_cb: Callable[[Any], Any] | None = None
146147

147148

148149
@dataclass
149150
class BenchmarkParameter:
150151
data_class: type[Any]
151152
object_creation_cb: Callable[[], Any]
152-
mode_parameter: dict[Mode, Optional[ModeParameter]]
153+
mode_parameter: dict[Mode, ModeParameter | None]
153154

154155

155156
benchmark_parameter: dict[Data, BenchmarkParameter] = {
@@ -202,12 +203,12 @@ def pop_data(key: str, *, old: dict[str, Any], new: dict[str, Any]) -> tuple[Any
202203
return old.pop(key), new.pop(key)
203204

204205

205-
def print_compare_row(c0: str, c1: Union[str, float], c2: Union[str, float], c3: Union[str, float]) -> None:
206+
def print_compare_row(c0: str, c1: str | float, c2: str | float, c3: str | float) -> None:
206207
print(f"{c0:<12} | {c1:<16} | {c2:<16} | {c3:<12}")
207208

208209

209210
def compare_results(
210-
old: dict[str, dict[str, dict[str, Union[float, int]]]], new: dict[str, dict[str, dict[str, Union[float, int]]]]
211+
old: dict[str, dict[str, dict[str, float | int]]], new: dict[str, dict[str, dict[str, float | int]]]
211212
) -> None:
212213
old_version, new_version = pop_data("version", old=old, new=new)
213214
if old_version != new_version:

benchmarks/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import sys
88
from collections.abc import AsyncIterator
99
from pathlib import Path
10-
from typing import Any, Generic, Optional, TypeVar, Union
10+
from typing import Any, Generic, TypeVar
1111

1212
import click
1313

@@ -22,20 +22,20 @@ def __init__(self, enum: type[_T_Enum], case_sensitive: bool = False) -> None:
2222
self.__enum = enum
2323
super().__init__(choices=[item.value for item in enum], case_sensitive=case_sensitive)
2424

25-
def convert(self, value: Any, param: Optional[click.Parameter], ctx: Optional[click.Context]) -> _T_Enum:
25+
def convert(self, value: Any, param: click.Parameter | None, ctx: click.Context | None) -> _T_Enum:
2626
converted_str = super().convert(value, param, ctx)
2727
return self.__enum(converted_str)
2828

2929

3030
@contextlib.asynccontextmanager
31-
async def setup_db(name: Union[str, os.PathLike[str]], db_version: int) -> AsyncIterator[DBWrapper2]:
31+
async def setup_db(name: str | os.PathLike[str], db_version: int) -> AsyncIterator[DBWrapper2]:
3232
db_filename = Path(name)
3333
try:
3434
os.unlink(db_filename)
3535
except FileNotFoundError:
3636
pass
3737

38-
log_path: Optional[Path]
38+
log_path: Path | None
3939
if "--sql-logging" in sys.argv:
4040
log_path = Path("sql.log")
4141
else:

chia/_tests/blockchain/blockchain_test_utils.py

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

3-
from typing import Optional
4-
53
from chia_rs import FullBlock, SpendBundleConditions
64
from chia_rs.sized_ints import uint32, uint64
75

@@ -46,10 +44,10 @@ async def _validate_and_add_block(
4644
blockchain: Blockchain,
4745
block: FullBlock,
4846
*,
49-
expected_result: Optional[AddBlockResult] = None,
50-
expected_error: Optional[Err] = None,
47+
expected_result: AddBlockResult | None = None,
48+
expected_error: Err | None = None,
5149
skip_prevalidation: bool = False,
52-
fork_info: Optional[ForkInfo] = None,
50+
fork_info: ForkInfo | None = None,
5351
) -> None:
5452
# Tries to validate and add the block, and checks that there are no errors in the process and that the
5553
# block is added to the peak.
@@ -137,7 +135,7 @@ async def _validate_and_add_block_multi_error(
137135
block: FullBlock,
138136
expected_errors: list[Err],
139137
skip_prevalidation: bool = False,
140-
fork_info: Optional[ForkInfo] = None,
138+
fork_info: ForkInfo | None = None,
141139
) -> None:
142140
# Checks that the blockchain returns one of the expected errors
143141
try:
@@ -155,7 +153,7 @@ async def _validate_and_add_block_multi_result(
155153
block: FullBlock,
156154
expected_result: list[AddBlockResult],
157155
skip_prevalidation: bool = False,
158-
fork_info: Optional[ForkInfo] = None,
156+
fork_info: ForkInfo | None = None,
159157
) -> None:
160158
try:
161159
await _validate_and_add_block(
@@ -176,7 +174,7 @@ async def _validate_and_add_block_no_error(
176174
blockchain: Blockchain,
177175
block: FullBlock,
178176
skip_prevalidation: bool = False,
179-
fork_info: Optional[ForkInfo] = None,
177+
fork_info: ForkInfo | None = None,
180178
) -> None:
181179
# adds a block and ensures that there is no error. However, does not ensure that block extended the peak of
182180
# the blockchain

chia/_tests/blockchain/test_augmented_chain.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import re
44
from dataclasses import dataclass, field
5-
from typing import TYPE_CHECKING, ClassVar, Optional, cast
5+
from typing import TYPE_CHECKING, ClassVar, cast
66

77
import pytest
88
from chia_rs import BlockRecord, FullBlock
@@ -30,14 +30,14 @@ class NullBlockchain:
3030
async def lookup_block_generators(self, header_hash: bytes32, generator_refs: set[uint32]) -> dict[uint32, bytes]:
3131
raise ValueError(Err.GENERATOR_REF_HAS_NO_GENERATOR) # pragma: no cover
3232

33-
async def get_block_record_from_db(self, header_hash: bytes32) -> Optional[BlockRecord]:
33+
async def get_block_record_from_db(self, header_hash: bytes32) -> BlockRecord | None:
3434
return None # pragma: no cover
3535

3636
def add_block_record(self, block_record: BlockRecord) -> None:
3737
self.added_blocks.add(block_record.header_hash)
3838

3939
# BlockRecordsProtocol
40-
def try_block_record(self, header_hash: bytes32) -> Optional[BlockRecord]:
40+
def try_block_record(self, header_hash: bytes32) -> BlockRecord | None:
4141
return None # pragma: no cover
4242

4343
def block_record(self, header_hash: bytes32) -> BlockRecord:
@@ -46,7 +46,7 @@ def block_record(self, header_hash: bytes32) -> BlockRecord:
4646
def height_to_block_record(self, height: uint32) -> BlockRecord:
4747
raise ValueError("Height is not in blockchain")
4848

49-
def height_to_hash(self, height: uint32) -> Optional[bytes32]:
49+
def height_to_hash(self, height: uint32) -> bytes32 | None:
5050
return self.heights.get(height)
5151

5252
def contains_block(self, header_hash: bytes32, height: uint32) -> bool:

chia/_tests/blockchain/test_blockchain.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from collections.abc import AsyncIterator, Awaitable
1010
from contextlib import asynccontextmanager
1111
from dataclasses import dataclass, replace
12-
from typing import Optional
1312

1413
import pytest
1514
from chia_rs import (
@@ -3257,7 +3256,7 @@ async def test_invalid_agg_sig(self, empty_blockchain: Blockchain, bt: BlockTool
32573256
assert preval_result.error == Err.BAD_AGGREGATE_SIGNATURE.value
32583257

32593258

3260-
def maybe_header_hash(block: Optional[BlockRecord]) -> Optional[bytes32]:
3259+
def maybe_header_hash(block: BlockRecord | None) -> bytes32 | None:
32613260
if block is None:
32623261
return None
32633262
return block.header_hash
@@ -3304,7 +3303,7 @@ async def test_get_tx_peak_reorg(
33043303
reorg_point = 12
33053304
blocks = bt.get_consecutive_blocks(reorg_point)
33063305

3307-
last_tx_block: Optional[bytes32] = None
3306+
last_tx_block: bytes32 | None = None
33083307
for block in blocks:
33093308
assert maybe_header_hash(b.get_tx_peak()) == last_tx_block
33103309
await _validate_and_add_block(b, block)
@@ -3315,7 +3314,7 @@ async def test_get_tx_peak_reorg(
33153314
assert peak.height == reorg_point - 1
33163315
assert maybe_header_hash(b.get_tx_peak()) == last_tx_block
33173316

3318-
reorg_last_tx_block: Optional[bytes32] = None
3317+
reorg_last_tx_block: bytes32 | None = None
33193318
fork_block = blocks[9]
33203319
fork_info = ForkInfo(fork_block.height, fork_block.height, fork_block.header_hash)
33213320
blocks_reorg_chain = bt.get_consecutive_blocks(7, blocks[:10], seed=b"2")
@@ -4076,7 +4075,7 @@ async def test_get_tx_peak(default_400_blocks: list[FullBlock], empty_blockchain
40764075
assert bc.get_tx_peak() == last_tx_block_record
40774076

40784077

4079-
def to_bytes(gen: Optional[SerializedProgram]) -> bytes:
4078+
def to_bytes(gen: SerializedProgram | None) -> bytes:
40804079
assert gen is not None
40814080
return bytes(gen)
40824081

@@ -4209,7 +4208,7 @@ async def get_fork_info(blockchain: Blockchain, block: FullBlock, peak: BlockRec
42094208
counter = 0
42104209
start = time.monotonic()
42114210
for height in range(fork_info.fork_height + 1, block.height):
4212-
fork_block: Optional[FullBlock] = await blockchain.block_store.get_full_block(fork_chain[uint32(height)])
4211+
fork_block: FullBlock | None = await blockchain.block_store.get_full_block(fork_chain[uint32(height)])
42134212
assert fork_block is not None
42144213
assert fork_block.height - 1 == fork_info.peak_height
42154214
assert fork_block.height == 0 or fork_block.prev_header_hash == fork_info.peak_hash

chia/_tests/blockchain/test_build_chains.py

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

3-
from typing import Optional
4-
53
import pytest
64
from chia_rs import Coin, ConsensusConstants, FullBlock, additions_and_removals, get_flags_for_height_and_constants
75
from chia_rs.sized_ints import uint64
@@ -102,8 +100,8 @@ def validate_chain(
102100
normalized_to_identity_icc_eos: bool = False,
103101
normalized_to_identity_cc_sp: bool = False,
104102
normalized_to_identity_cc_ip: bool = False,
105-
block_list_input: Optional[list[FullBlock]] = None,
106-
time_per_block: Optional[float] = None,
103+
block_list_input: list[FullBlock] | None = None,
104+
time_per_block: float | None = None,
107105
dummy_block_references: bool = False,
108106
include_transactions: bool = False,
109107
) -> None:

chia/_tests/blockchain/test_get_block_generator.py

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

33
from dataclasses import dataclass
4-
from typing import Optional
54

65
import pytest
76
from chia_rs.sized_bytes import bytes32
@@ -16,14 +15,14 @@
1615
@dataclass(frozen=True)
1716
class BR:
1817
prev_header_hash: bytes32
19-
transactions_generator: Optional[SerializedProgram]
18+
transactions_generator: SerializedProgram | None
2019
transactions_generator_ref_list: list[uint32]
2120

2221

2322
@dataclass(frozen=True)
2423
class FB:
2524
prev_header_hash: bytes32
26-
transactions_generator: Optional[SerializedProgram]
25+
transactions_generator: SerializedProgram | None
2726
height: uint32
2827

2928

0 commit comments

Comments
 (0)