Add more ruff rules (#8767)

* Add more ruff rules

* Add more ruff rules

* pre-commit: Update ruff v0.0.269 -> v0.0.270

* Apply suggestions from code review

* Fix doctest

* Fix doctest (ignore whitespace)

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Christian Clauss
2023-05-26 09:34:17 +02:00
committed by GitHub
parent dd3b499bfa
commit 4b79d771cd
67 changed files with 349 additions and 223 deletions

View File

@ -78,17 +78,18 @@ def knapsack_with_example_solution(w: int, wt: list, val: list):
num_items = len(wt)
if num_items != len(val):
raise ValueError(
"The number of weights must be the "
"same as the number of values.\nBut "
f"got {num_items} weights and {len(val)} values"
msg = (
"The number of weights must be the same as the number of values.\n"
f"But got {num_items} weights and {len(val)} values"
)
raise ValueError(msg)
for i in range(num_items):
if not isinstance(wt[i], int):
raise TypeError(
"All weights must be integers but "
f"got weight of type {type(wt[i])} at index {i}"
msg = (
"All weights must be integers but got weight of "
f"type {type(wt[i])} at index {i}"
)
raise TypeError(msg)
optimal_val, dp_table = knapsack(w, wt, val, num_items)
example_optional_set: set = set()

View File

@ -42,7 +42,8 @@ def min_steps_to_one(number: int) -> int:
"""
if number <= 0:
raise ValueError(f"n must be greater than 0. Got n = {number}")
msg = f"n must be greater than 0. Got n = {number}"
raise ValueError(msg)
table = [number + 1] * (number + 1)

View File

@ -177,13 +177,15 @@ def _enforce_args(n: int, prices: list):
the rod
"""
if n < 0:
raise ValueError(f"n must be greater than or equal to 0. Got n = {n}")
msg = f"n must be greater than or equal to 0. Got n = {n}"
raise ValueError(msg)
if n > len(prices):
raise ValueError(
"Each integral piece of rod must have a corresponding "
f"price. Got n = {n} but length of prices = {len(prices)}"
msg = (
"Each integral piece of rod must have a corresponding price. "
f"Got n = {n} but length of prices = {len(prices)}"
)
raise ValueError(msg)
def main():

View File

@ -297,11 +297,13 @@ def _validate_list(_object: Any, var_name: str) -> None:
"""
if not isinstance(_object, list):
raise ValueError(f"{var_name} must be a list")
msg = f"{var_name} must be a list"
raise ValueError(msg)
else:
for x in _object:
if not isinstance(x, str):
raise ValueError(f"{var_name} must be a list of strings")
msg = f"{var_name} must be a list of strings"
raise ValueError(msg)
def _validate_dicts(
@ -384,14 +386,15 @@ def _validate_dict(
ValueError: mock_name nested dictionary all values must be float
"""
if not isinstance(_object, dict):
raise ValueError(f"{var_name} must be a dict")
msg = f"{var_name} must be a dict"
raise ValueError(msg)
if not all(isinstance(x, str) for x in _object):
raise ValueError(f"{var_name} all keys must be strings")
msg = f"{var_name} all keys must be strings"
raise ValueError(msg)
if not all(isinstance(x, value_type) for x in _object.values()):
nested_text = "nested dictionary " if nested else ""
raise ValueError(
f"{var_name} {nested_text}all values must be {value_type.__name__}"
)
msg = f"{var_name} {nested_text}all values must be {value_type.__name__}"
raise ValueError(msg)
if __name__ == "__main__":