Skip to content

Commit f165c76

Browse files
authored
Merge pull request #2613 from NicholasTanz/enableRulesets
Enable rulesets (BLE and RUF)
2 parents 9db9277 + 52601e2 commit f165c76

13 files changed

Lines changed: 33 additions & 30 deletions

File tree

examples/uploader/_localrepo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def add_target(self, role: str, targetpath: str) -> bool:
106106
with self.edit_targets(role) as delegated:
107107
delegated.targets[targetpath] = targetfile
108108

109-
except Exception as e:
109+
except Exception as e: # noqa: BLE001
110110
print(f"Failed to submit new {role} with added target: {e}")
111111
return False
112112

pyproject.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ line-length=80
8484
select = [
8585
"A", # flake8-builtins
8686
"B", # flake8-bugbear
87+
"BLE", # flake8-blind-except
8788
"C4", # flake8-comprehensions
8889
"D", # pydocstyle
8990
"DTZ", # flake8-datetimez
@@ -94,6 +95,7 @@ select = [
9495
"N", # pep8-naming
9596
"PL", # pylint
9697
"RET", # flake8-return
98+
"RUF", # ruff-specific rules
9799
"S", # flake8-bandit
98100
"SIM", # flake8-simplify
99101
"UP", # pyupgrade
@@ -107,9 +109,10 @@ ignore = [
107109

108110
[tool.ruff.lint.per-file-ignores]
109111
"tests/*" = [
110-
"D", # pydocstyle: no docstrings required for tests
111-
"E501", # line-too-long: embedded test data in "fmt: off" blocks is ok
112-
"S", # bandit: Not running bandit on tests
112+
"D", # pydocstyle: no docstrings required for tests
113+
"E501", # line-too-long: embedded test data in "fmt: off" blocks is ok
114+
"S", # bandit: Not running bandit on tests
115+
"RUF012", # ruff: mutable-class-default
113116
]
114117
"examples/*/*" = [
115118
"D", # pydocstyle: no docstrings required for examples

tests/test_fetcher_ng.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def setUpClass(cls) -> None:
4646

4747
cls.url_prefix = (
4848
f"http://{utils.TEST_HOST_ADDRESS}:"
49-
f"{str(cls.server_process_handler.port)}"
49+
f"{cls.server_process_handler.port!s}"
5050
)
5151
target_filename = os.path.basename(cls.target_file.name)
5252
cls.url = f"{cls.url_prefix}/{target_filename}"

tests/test_updater_top_level_update.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ def test_new_timestamp_fast_forward_recovery(self) -> None:
447447
self._assert_version_equals(Timestamp.type, 1)
448448

449449
def test_new_snapshot_hash_mismatch(self) -> None:
450-
# Check against timestamp roles snapshot hash
450+
# Check against timestamp role's snapshot hash
451451

452452
# Update timestamp with snapshot's hashes
453453
self.sim.compute_metafile_hashes_length = True
@@ -477,7 +477,7 @@ def test_new_snapshot_unsigned(self) -> None:
477477
self._assert_files_exist([Root.type, Timestamp.type])
478478

479479
def test_new_snapshot_version_mismatch(self) -> None:
480-
# Check against timestamp roles snapshot version
480+
# Check against timestamp role's snapshot version
481481

482482
# Increase snapshot version without updating timestamp
483483
self.sim.snapshot.version += 1
@@ -544,7 +544,7 @@ def test_new_snapshot_expired(self) -> None:
544544
self._assert_files_exist([Root.type, Timestamp.type])
545545

546546
def test_new_targets_hash_mismatch(self) -> None:
547-
# Check against snapshot roles targets hashes
547+
# Check against snapshot role's targets hashes
548548

549549
# Update snapshot with target's hashes
550550
self.sim.compute_metafile_hashes_length = True
@@ -575,7 +575,7 @@ def test_new_targets_unsigned(self) -> None:
575575
self._assert_files_exist([Root.type, Timestamp.type, Snapshot.type])
576576

577577
def test_new_targets_version_mismatch(self) -> None:
578-
# Check against snapshot roles targets version
578+
# Check against snapshot role's targets version
579579

580580
# Increase targets version without updating snapshot
581581
self.sim.targets.version += 1

tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def can_connect(port: int) -> bool:
3636
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
3737
sock.connect(("localhost", port))
3838
return True
39-
except Exception:
39+
except Exception: # noqa: BLE001
4040
return False
4141
finally:
4242
# The process will always enter in finally even after return.

tests/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def _start_process(self, extra_cmd_args: List[str], popen_cwd: str) -> None:
226226
"""Starts the process running the server."""
227227

228228
# The "-u" option forces stdin, stdout and stderr to be unbuffered.
229-
command = [sys.executable, "-u", self.server] + extra_cmd_args
229+
command = [sys.executable, "-u", self.server, *extra_cmd_args]
230230

231231
# Reusing one subprocess in multiple tests, but split up the logs
232232
# for each.
@@ -344,7 +344,7 @@ def flush_log(self) -> None:
344344

345345
if len(self.__logged_messages) > 0:
346346
title = "Test server (" + self.server + ") output:\n"
347-
message = [title] + self.__logged_messages
347+
message = [title, *self.__logged_messages]
348348
self.__logger.info("| ".join(message))
349349
self.__logged_messages = []
350350

tuf/api/_payload.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ def get_delegated_role(self, delegated_role: str) -> Role:
606606

607607
return self.roles[delegated_role]
608608

609-
def get_key(self, keyid: str) -> Key: # noqa: D102
609+
def get_key(self, keyid: str) -> Key:
610610
if keyid not in self.keys:
611611
raise ValueError(f"Key {keyid} not found")
612612

@@ -1778,7 +1778,7 @@ def get_delegated_role(self, delegated_role: str) -> Role:
17781778

17791779
return role
17801780

1781-
def get_key(self, keyid: str) -> Key: # noqa: D102
1781+
def get_key(self, keyid: str) -> Key:
17821782
if self.delegations is None:
17831783
raise ValueError("No delegations found")
17841784
if keyid not in self.delegations.keys:

tuf/api/dsse.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def from_bytes(cls, data: bytes) -> "SimpleEnvelope[T]":
8383
envelope_dict = json.loads(data.decode())
8484
envelope = SimpleEnvelope.from_dict(envelope_dict)
8585

86-
except Exception as e:
86+
except Exception as e: # noqa: BLE001
8787
raise DeserializationError from e
8888

8989
return envelope
@@ -103,7 +103,7 @@ def to_bytes(self) -> bytes:
103103
envelope_dict = self.to_dict()
104104
json_bytes = json.dumps(envelope_dict).encode()
105105

106-
except Exception as e:
106+
except Exception as e: # noqa: BLE001
107107
raise SerializationError from e
108108

109109
return json_bytes
@@ -123,7 +123,7 @@ def from_signed(cls, signed: T) -> "SimpleEnvelope[T]":
123123
signed_dict = signed.to_dict()
124124
json_bytes = json.dumps(signed_dict).encode()
125125

126-
except Exception as e:
126+
except Exception as e: # noqa: BLE001
127127
raise SerializationError from e
128128

129129
return cls(json_bytes, cls._DEFAULT_PAYLOAD_TYPE, [])
@@ -152,7 +152,7 @@ def get_signed(self) -> T:
152152
else:
153153
raise ValueError(f'unrecognized role type "{_type}"')
154154

155-
except Exception as e:
155+
except Exception as e: # noqa: BLE001
156156
raise DeserializationError from e
157157

158158
return cast(T, inner_cls.from_dict(payload_dict))

tuf/api/metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def sign(
377377

378378
try:
379379
signature = signer.sign(bytes_data)
380-
except Exception as e:
380+
except Exception as e: # noqa: BLE001
381381
raise UnsignedMetadataError(f"Failed to sign: {e}") from e
382382

383383
if not append:

tuf/api/serialization/json.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def deserialize(self, raw_data: bytes) -> Metadata:
3535
json_dict = json.loads(raw_data.decode("utf-8"))
3636
metadata_obj = Metadata.from_dict(json_dict)
3737

38-
except Exception as e:
38+
except Exception as e: # noqa: BLE001
3939
raise DeserializationError("Failed to deserialize JSON") from e
4040

4141
return metadata_obj
@@ -77,10 +77,10 @@ def serialize(self, metadata_obj: Metadata) -> bytes:
7777
raise ValueError(
7878
"Metadata changes if you serialize and deserialize."
7979
)
80-
except Exception as e:
80+
except Exception as e: # noqa: BLE001
8181
raise ValueError("Metadata cannot be validated!") from e
8282

83-
except Exception as e:
83+
except Exception as e: # noqa: BLE001
8484
raise SerializationError("Failed to serialize JSON") from e
8585

8686
return json_bytes
@@ -97,7 +97,7 @@ def serialize(self, signed_obj: Signed) -> bytes:
9797
signed_dict = signed_obj.to_dict()
9898
canonical_bytes = encode_canonical(signed_dict).encode("utf-8")
9999

100-
except Exception as e:
100+
except Exception as e: # noqa: BLE001
101101
raise SerializationError from e
102102

103103
return canonical_bytes

0 commit comments

Comments
 (0)