Replace black and flake8 with ruff for linting and formatting

- Remove .flake8; add [tool.ruff] and [tool.ruff.format] config in pyproject.toml
  (line-length 120, excludes, ignore list, per-file-ignores, mccabe complexity)
- Update GitHub workflow lint job to run `ruff check .` and `ruff format --check .`
- Swap black and flake8 for ruff in requirements.txt and Pipfile
- Change capnp/__init__.py to ruff-style noqa comment
- Move max-complexity into [tool.ruff.lint.mccabe], lint options into [tool.ruff.lint]
- Add per-file-ignores for capnp/__init__.py (F401, F403, F405), remove inline noqa
- Run ruff format across codebase (24 files) for consistent style
This commit is contained in:
Jacob Alexander
2026-02-25 17:24:24 -08:00
parent a27c849021
commit 162fddbcf6
30 changed files with 89 additions and 203 deletions

View File

@@ -63,9 +63,7 @@ async def test_large_response_sequential():
response = await cap.foo(i=size, j=False)
# Verify the response has the correct length
assert (
len(response.x) == size
), f"Size mismatch for {size}: expected {size}, got {len(response.x)}"
assert len(response.x) == size, f"Size mismatch for {size}: expected {size}, got {len(response.x)}"
# Verify the pattern is correct (not corrupted)
expected = "".join(chr(65 + (k % 26)) for k in range(size))
@@ -107,6 +105,4 @@ async def test_large_response_pipelined():
assert len(response.x) == size, f"Size mismatch for {size}"
expected = "".join(chr(65 + (k % 26)) for k in range(size))
assert (
response.x == expected
), f"Data corruption detected for {size} bytes payload!"
assert response.x == expected, f"Data corruption detected for {size} bytes payload!"

View File

@@ -212,9 +212,7 @@ class TailCaller(capability.TestTailCaller.Server):
async def foo_context(self, context):
self.count += 1
tail = context.params.callee.foo_request(
i=context.params.i, t="from TailCaller"
)
tail = context.params.callee.foo_request(i=context.params.i, t="from TailCaller")
await context.tail_call(tail)

View File

@@ -19,9 +19,7 @@ def cleanup():
p.kill()
def run_subprocesses(
address, server, client, wildcard_server=False, ipv4_force=True
): # noqa
def run_subprocesses(address, server, client, wildcard_server=False, ipv4_force=True): # noqa
server_attempt = 0
server_attempts = 2
done = False
@@ -29,9 +27,7 @@ def run_subprocesses(
c_address = address
s_address = address
while not done:
assert server_attempt < server_attempts, "Failed {} server attempts".format(
server_attempts
)
assert server_attempt < server_attempts, "Failed {} server attempts".format(server_attempts)
server_attempt += 1
# Force ipv4 for tests (known issues on GitHub Actions with IPv6 for some targets)
@@ -125,9 +121,7 @@ def test_async_calculator_example(unused_tcp_port, cleanup):
def test_addressbook_example(cleanup):
proc = subprocess.Popen(
[sys.executable, os.path.join(examples_dir, "addressbook.py")]
)
proc = subprocess.Popen([sys.executable, os.path.join(examples_dir, "addressbook.py")])
ret = proc.wait()
assert ret == 0

View File

@@ -197,12 +197,10 @@ def test_view_keeps_message_alive(all_types):
view = msg.get_data_as_view("dataField")
new_ref_count = sys.getrefcount(msg)
assert (
new_ref_count > initial_ref_count
), f"View failed to hold reference to Message! (Old: {initial_ref_count}, New: {new_ref_count})"
print(
f"\n[Ref Check] Success: Ref count increased from {initial_ref_count} to {new_ref_count}"
assert new_ref_count > initial_ref_count, (
f"View failed to hold reference to Message! (Old: {initial_ref_count}, New: {new_ref_count})"
)
print(f"\n[Ref Check] Success: Ref count increased from {initial_ref_count} to {new_ref_count}")
del msg
gc.collect()

View File

@@ -216,9 +216,7 @@ def test_addressbook_explicit_fields(addressbook):
alicePhones[0]._set_by_field(phone_fields["number"], "555-1212")
alicePhones[0]._set_by_field(phone_fields["type"], "mobile")
employment = alice._get_by_field(person_fields["employment"])
employment._set_by_field(
addressbook.Person.Employment.schema.fields["school"], "MIT"
)
employment._set_by_field(addressbook.Person.Employment.schema.fields["school"], "MIT")
bob = people[1]
bob._set_by_field(person_fields["id"], 456)
@@ -230,9 +228,7 @@ def test_addressbook_explicit_fields(addressbook):
bobPhones[1]._set_by_field(phone_fields["number"], "555-7654")
bobPhones[1]._set_by_field(phone_fields["type"], "work")
employment = bob._get_by_field(person_fields["employment"])
employment._set_by_field(
addressbook.Person.Employment.schema.fields["unemployed"], None
)
employment._set_by_field(addressbook.Person.Employment.schema.fields["unemployed"], None)
addresses.write(file)
@@ -252,9 +248,7 @@ def test_addressbook_explicit_fields(addressbook):
assert alicePhones[0]._get_by_field(phone_fields["number"]) == "555-1212"
assert alicePhones[0]._get_by_field(phone_fields["type"]) == "mobile"
employment = alice._get_by_field(person_fields["employment"])
employment._get_by_field(
addressbook.Person.Employment.schema.fields["school"]
) == "MIT"
employment._get_by_field(addressbook.Person.Employment.schema.fields["school"]) == "MIT"
bob = people[1]
assert bob._get_by_field(person_fields["id"]) == 456
@@ -266,9 +260,7 @@ def test_addressbook_explicit_fields(addressbook):
assert bobPhones[1]._get_by_field(phone_fields["number"]) == "555-7654"
assert bobPhones[1]._get_by_field(phone_fields["type"]) == "work"
employment = bob._get_by_field(person_fields["employment"])
employment._get_by_field(
addressbook.Person.Employment.schema.fields["unemployed"]
) is None
employment._get_by_field(addressbook.Person.Employment.schema.fields["unemployed"]) is None
f = open("example", "w")
writeAddressBook(f)
@@ -450,13 +442,9 @@ def check_all_types(reader):
check_list(subReader.uInt8List, [12, 34, 0, 0xFF])
check_list(subReader.uInt16List, [1234, 5678, 0, 0xFFFF])
check_list(subReader.uInt32List, [12345678, 90123456, 0, 0xFFFFFFFF])
check_list(
subReader.uInt64List, [123456789012345, 678901234567890, 0, 0xFFFFFFFFFFFFFFFF]
)
check_list(subReader.uInt64List, [123456789012345, 678901234567890, 0, 0xFFFFFFFFFFFFFFFF])
check_list(subReader.float32List, [0.0, 1234567.0, 1e37, -1e37, 1e-37, -1e-37])
check_list(
subReader.float64List, [0.0, 123456789012345.0, 1e306, -1e306, 1e-306, -1e-306]
)
check_list(subReader.float64List, [0.0, 123456789012345.0, 1e306, -1e306, 1e-306, -1e-306])
check_list(subReader.textList, ["quux", "corge", "grault"])
check_list(subReader.dataList, [b"garply", b"waldo", b"fred"])
@@ -510,25 +498,19 @@ def check_all_types(reader):
def test_build(all_types):
root = all_types.TestAllTypes.new_message()
init_all_types(root)
expectedText = open(
os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8"
).read()
expectedText = open(os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8").read()
assert str(root) + "\n" == expectedText
def test_build_first_segment_size(all_types):
root = all_types.TestAllTypes.new_message(1)
init_all_types(root)
expectedText = open(
os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8"
).read()
expectedText = open(os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8").read()
assert str(root) + "\n" == expectedText
root = all_types.TestAllTypes.new_message(1024 * 1024)
init_all_types(root)
expectedText = open(
os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8"
).read()
expectedText = open(os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8").read()
assert str(root) + "\n" == expectedText
@@ -537,9 +519,7 @@ def test_binary_read(all_types):
root = all_types.TestAllTypes.read(f)
check_all_types(root)
expectedText = open(
os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8"
).read()
expectedText = open(os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8").read()
assert str(root) + "\n" == expectedText
# Test set_root().
@@ -557,9 +537,7 @@ def test_packed_read(all_types):
root = all_types.TestAllTypes.read_packed(f)
check_all_types(root)
expectedText = open(
os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8"
).read()
expectedText = open(os.path.join(this_dir, "all-types.txt"), "r", encoding="utf8").read()
assert str(root) + "\n" == expectedText

View File

@@ -40,15 +40,11 @@ def test_annotations(annotations):
assert annotation.value.struct.as_struct(annotations.AnnotationStruct).test == 100
annotation = annotations.TestAnnotationThree.schema.node.annotations[0]
annotation_list = annotation.value.list.as_list(
capnp._ListSchema(annotations.AnnotationStruct)
)
annotation_list = annotation.value.list.as_list(capnp._ListSchema(annotations.AnnotationStruct))
assert annotation_list[0].test == 100
assert annotation_list[1].test == 101
annotation = annotations.TestAnnotationFour.schema.node.annotations[0]
annotation_list = annotation.value.list.as_list(
capnp._ListSchema(capnp.types.UInt16)
)
annotation_list = annotation.value.list.as_list(capnp._ListSchema(capnp.types.UInt16))
assert annotation_list[0] == 200
assert annotation_list[1] == 201

View File

@@ -114,9 +114,7 @@ def test_roundtrip_bytes_packed(all_types):
@contextmanager
def _warnings(
expected_count=2, expected_text="This message has already been written once."
):
def _warnings(expected_count=2, expected_text="This message has already been written once."):
with warnings.catch_warnings(record=True) as w:
yield

View File

@@ -168,9 +168,7 @@ def test_new_message(all_types):
assert msg.structField.int32Field == 100
msg = all_types.TestAllTypes.new_message(
structList=[{"int32Field": 100}, {"int32Field": 101}]
)
msg = all_types.TestAllTypes.new_message(structList=[{"int32Field": 100}, {"int32Field": 101}])
assert msg.structList[0].int32Field == 100
assert msg.structList[1].int32Field == 101
@@ -199,9 +197,7 @@ def test_set_dict(all_types):
def test_set_dict_union(addressbook):
person = addressbook.Person.new_message(
**{"employment": {"employer": {"name": "foo"}}}
)
person = addressbook.Person.new_message(**{"employment": {"employer": {"name": "foo"}}})
assert person.employment.which == addressbook.Person.Employment.employer
@@ -212,16 +208,12 @@ def test_union_enum(all_types):
assert all_types.UnionAllTypes.Union.UnionStructField1 == 0
assert all_types.UnionAllTypes.Union.UnionStructField2 == 1
msg = all_types.UnionAllTypes.new_message(
**{"unionStructField1": {"textField": "foo"}}
)
msg = all_types.UnionAllTypes.new_message(**{"unionStructField1": {"textField": "foo"}})
assert msg.which == all_types.UnionAllTypes.Union.UnionStructField1
assert msg.which == "unionStructField1"
assert msg.which == 0
msg = all_types.UnionAllTypes.new_message(
**{"unionStructField2": {"textField": "foo"}}
)
msg = all_types.UnionAllTypes.new_message(**{"unionStructField2": {"textField": "foo"}})
assert msg.which == all_types.UnionAllTypes.Union.UnionStructField2
assert msg.which == "unionStructField2"
assert msg.which == 1
@@ -229,14 +221,10 @@ def test_union_enum(all_types):
assert all_types.GroupedUnionAllTypes.Union.G1 == 0
assert all_types.GroupedUnionAllTypes.Union.G2 == 1
msg = all_types.GroupedUnionAllTypes.new_message(
**{"g1": {"unionStructField1": {"textField": "foo"}}}
)
msg = all_types.GroupedUnionAllTypes.new_message(**{"g1": {"unionStructField1": {"textField": "foo"}}})
assert msg.which == all_types.GroupedUnionAllTypes.Union.G1
msg = all_types.GroupedUnionAllTypes.new_message(
**{"g2": {"unionStructField2": {"textField": "foo"}}}
)
msg = all_types.GroupedUnionAllTypes.new_message(**{"g2": {"unionStructField2": {"textField": "foo"}}})
assert msg.which == all_types.GroupedUnionAllTypes.Union.G2
msg = all_types.UnionAllTypes.new_message()
@@ -248,9 +236,7 @@ def isstr(s):
def test_to_dict_enum(addressbook):
person = addressbook.Person.new_message(
**{"phones": [{"number": "999-9999", "type": "mobile"}]}
)
person = addressbook.Person.new_message(**{"phones": [{"number": "999-9999", "type": "mobile"}]})
field = person.to_dict()["phones"][0]["type"]
assert isstr(field)