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

@ -77,7 +77,8 @@ class BinarySearchTree:
elif label > node.label:
node.right = self._put(node.right, label, node)
else:
raise Exception(f"Node with label {label} already exists")
msg = f"Node with label {label} already exists"
raise Exception(msg)
return node
@ -100,7 +101,8 @@ class BinarySearchTree:
def _search(self, node: Node | None, label: int) -> Node:
if node is None:
raise Exception(f"Node with label {label} does not exist")
msg = f"Node with label {label} does not exist"
raise Exception(msg)
else:
if label < node.label:
node = self._search(node.left, label)

View File

@ -31,7 +31,8 @@ def binary_tree_mirror(binary_tree: dict, root: int = 1) -> dict:
if not binary_tree:
raise ValueError("binary tree cannot be empty")
if root not in binary_tree:
raise ValueError(f"root {root} is not present in the binary_tree")
msg = f"root {root} is not present in the binary_tree"
raise ValueError(msg)
binary_tree_mirror_dictionary = dict(binary_tree)
binary_tree_mirror_dict(binary_tree_mirror_dictionary, root)
return binary_tree_mirror_dictionary

View File

@ -56,7 +56,8 @@ def find_python_set(node: Node) -> set:
for s in sets:
if node.data in s:
return s
raise ValueError(f"{node.data} is not in {sets}")
msg = f"{node.data} is not in {sets}"
raise ValueError(msg)
def test_disjoint_set() -> None:

View File

@ -94,25 +94,25 @@ def test_circular_linked_list() -> None:
try:
circular_linked_list.delete_front()
raise AssertionError() # This should not happen
raise AssertionError # This should not happen
except IndexError:
assert True # This should happen
try:
circular_linked_list.delete_tail()
raise AssertionError() # This should not happen
raise AssertionError # This should not happen
except IndexError:
assert True # This should happen
try:
circular_linked_list.delete_nth(-1)
raise AssertionError()
raise AssertionError
except IndexError:
assert True
try:
circular_linked_list.delete_nth(0)
raise AssertionError()
raise AssertionError
except IndexError:
assert True

View File

@ -198,13 +198,13 @@ def test_doubly_linked_list() -> None:
try:
linked_list.delete_head()
raise AssertionError() # This should not happen.
raise AssertionError # This should not happen.
except IndexError:
assert True # This should happen.
try:
linked_list.delete_tail()
raise AssertionError() # This should not happen.
raise AssertionError # This should not happen.
except IndexError:
assert True # This should happen.

View File

@ -353,13 +353,13 @@ def test_singly_linked_list() -> None:
try:
linked_list.delete_head()
raise AssertionError() # This should not happen.
raise AssertionError # This should not happen.
except IndexError:
assert True # This should happen.
try:
linked_list.delete_tail()
raise AssertionError() # This should not happen.
raise AssertionError # This should not happen.
except IndexError:
assert True # This should happen.

View File

@ -92,13 +92,13 @@ def test_stack() -> None:
try:
_ = stack.pop()
raise AssertionError() # This should not happen
raise AssertionError # This should not happen
except StackUnderflowError:
assert True # This should happen
try:
_ = stack.peek()
raise AssertionError() # This should not happen
raise AssertionError # This should not happen
except StackUnderflowError:
assert True # This should happen
@ -118,7 +118,7 @@ def test_stack() -> None:
try:
stack.push(200)
raise AssertionError() # This should not happen
raise AssertionError # This should not happen
except StackOverflowError:
assert True # This should happen