Enable ruff PLR5501 rule (#11332)

* Enable ruff PLR5501 rule

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

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

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Maxim Smolskiy 2024-03-28 20:25:41 +03:00 committed by GitHub
parent 19fd435042
commit 516a3028d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
24 changed files with 210 additions and 238 deletions

View File

@ -28,8 +28,7 @@ def is_valid(
if vertical:
if row + i >= len(puzzle) or puzzle[row + i][col] != "":
return False
else:
if col + i >= len(puzzle[0]) or puzzle[row][col + i] != "":
elif col + i >= len(puzzle[0]) or puzzle[row][col + i] != "":
return False
return True

View File

@ -101,8 +101,7 @@ def __judge_point(pt: bool, neighbours: list[list[bool]]) -> bool:
state = True
elif alive > 3:
state = False
else:
if alive == 3:
elif alive == 3:
state = True
return state

View File

@ -206,8 +206,7 @@ def decrypt_caesar_with_chi_squared(
# Add the margin of error to the total chi squared statistic
chi_squared_statistic += chi_letter_value
else:
if letter.lower() in frequencies:
elif letter.lower() in frequencies:
# Get the amount of times the letter occurs in the message
occurrences = decrypted_with_shift.count(letter)

View File

@ -215,8 +215,8 @@ def del_node(root: MyNode, data: Any) -> MyNode | None:
return root
else:
root.set_left(del_node(left_child, data))
else: # root.get_data() < data
if right_child is None:
# root.get_data() < data
elif right_child is None:
return root
else:
root.set_right(del_node(right_child, data))

View File

@ -185,8 +185,7 @@ class BinarySearchTree:
break
else:
parent_node = parent_node.left
else:
if parent_node.right is None:
elif parent_node.right is None:
parent_node.right = new_node
break
else:

View File

@ -74,8 +74,7 @@ class BinarySearchTree:
def _put(self, node: Node | None, label: int, parent: Node | None = None) -> Node:
if node is None:
node = Node(label, parent)
else:
if label < node.label:
elif label < node.label:
node.left = self._put(node.left, label, node)
elif label > node.label:
node.right = self._put(node.right, label, node)
@ -106,8 +105,7 @@ class BinarySearchTree:
if node is None:
msg = f"Node with label {label} does not exist"
raise ValueError(msg)
else:
if label < node.label:
elif label < node.label:
node = self._search(node.left, label)
elif label > node.label:
node = self._search(node.right, label)

View File

@ -107,8 +107,7 @@ class RedBlackTree:
else:
self.left = RedBlackTree(label, 1, self)
self.left._insert_repair()
else:
if self.right:
elif self.right:
self.right.insert(label)
else:
self.right = RedBlackTree(label, 1, self)
@ -178,9 +177,8 @@ class RedBlackTree:
self.parent.left = None
else:
self.parent.right = None
else:
# The node is black
if child is None:
elif child is None:
# This node and its child are black
if self.parent is None:
# The tree is now empty
@ -205,8 +203,7 @@ class RedBlackTree:
elif self.label is not None and self.label > label:
if self.left:
self.left.remove(label)
else:
if self.right:
elif self.right:
self.right.remove(label)
return self.parent or self
@ -369,8 +366,7 @@ class RedBlackTree:
return None
else:
return self.right.search(label)
else:
if self.left is None:
elif self.left is None:
return None
else:
return self.left.search(label)

View File

@ -43,8 +43,7 @@ def split(root: Node | None, value: int) -> tuple[Node | None, Node | None]:
return None, None
elif root.value is None:
return None, None
else:
if value < root.value:
elif value < root.value:
"""
Right tree's root will be current node.
Now we split(with the same value) current node's left son

View File

@ -40,8 +40,7 @@ class BinaryHeap:
while self.__size >= 2 * i:
if 2 * i + 1 > self.__size:
bigger_child = 2 * i
else:
if self.__heap[2 * i] > self.__heap[2 * i + 1]:
elif self.__heap[2 * i] > self.__heap[2 * i + 1]:
bigger_child = 2 * i
else:
bigger_child = 2 * i + 1

View File

@ -95,8 +95,7 @@ def infix_2_postfix(infix: str) -> str:
while stack[-1] != "(":
post_fix.append(stack.pop()) # Pop stack & add the content to Postfix
stack.pop()
else:
if len(stack) == 0:
elif len(stack) == 0:
stack.append(x) # If stack is empty, push x to stack
else: # while priority of x is not > priority of element in the stack
while stack and stack[-1] != "(" and priority[x] <= priority[stack[-1]]:

View File

@ -153,9 +153,8 @@ class RadixNode:
# We have word remaining so we check the next node
elif remaining_word != "":
return incoming_node.delete(remaining_word)
else:
# If it is not a leaf, we don't have to delete
if not incoming_node.is_leaf:
elif not incoming_node.is_leaf:
return False
else:
# We delete the nodes if no edges go from it

View File

@ -274,12 +274,11 @@ def convex_hull_bf(points: list[Point]) -> list[Point]:
points_left_of_ij = True
elif det_k < 0:
points_right_of_ij = True
else:
# point[i], point[j], point[k] all lie on a straight line
# if point[k] is to the left of point[i] or it's to the
# right of point[j], then point[i], point[j] cannot be
# part of the convex hull of A
if points[k] < points[i] or points[k] > points[j]:
elif points[k] < points[i] or points[k] > points[j]:
ij_part_of_convex_hull = False
break

View File

@ -120,10 +120,10 @@ class GraphAdjacencyList(Generic[T]):
else:
self.adj_list[source_vertex] = [destination_vertex]
self.adj_list[destination_vertex] = [source_vertex]
else: # For directed graphs
# For directed graphs
# if both source vertex and destination vertex are present in adjacency
# list, add destination vertex to source vertex list of adjacent vertices.
if source_vertex in self.adj_list and destination_vertex in self.adj_list:
elif source_vertex in self.adj_list and destination_vertex in self.adj_list:
self.adj_list[source_vertex].append(destination_vertex)
# if only source vertex is present in adjacency list, add destination
# vertex to source vertex list of adjacent vertices and create a new vertex

View File

@ -18,8 +18,7 @@ class Heap:
else:
if 2 * start + 2 >= size:
smallest_child = 2 * start + 1
else:
if heap[2 * start + 1] < heap[2 * start + 2]:
elif heap[2 * start + 1] < heap[2 * start + 2]:
smallest_child = 2 * start + 1
else:
smallest_child = 2 * start + 2

View File

@ -270,8 +270,7 @@ def multi_a_star(start: TPos, goal: TPos, n_heuristic: int):
back_pointer,
)
close_list_inad.append(get_s)
else:
if g_function[goal] <= open_list[0].minkey():
elif g_function[goal] <= open_list[0].minkey():
if g_function[goal] < float("inf"):
do_something(back_pointer, goal, start)
else:

View File

@ -113,8 +113,7 @@ def data_safety_checker(list_vote: list, actual_result: float) -> bool:
for i in list_vote:
if i > actual_result:
safe = not_safe + 1
else:
if abs(abs(i) - abs(actual_result)) <= 0.1:
elif abs(abs(i) - abs(actual_result)) <= 0.1:
safe += 1
else:
not_safe += 1

View File

@ -20,8 +20,7 @@ def res(x, y):
if 0 not in (x, y):
# We use the relation x^y = y*log10(x), where 10 is the base.
return y * math.log10(x)
else:
if x == 0: # 0 raised to any number is 0
elif x == 0: # 0 raised to any number is 0
return 0
elif y == 0:
return 1 # any number raised to 0 is 1

View File

@ -94,9 +94,8 @@ def pollard_rho(
if divisor == 1:
# No common divisor yet, just keep searching.
continue
else:
# We found a common divisor!
if divisor == num:
elif divisor == num:
# Unfortunately, the divisor is ``num`` itself and is useless.
break
else:

View File

@ -73,8 +73,7 @@ def cramers_rule_2x2(equation1: list[int], equation2: list[int]) -> tuple[float,
raise ValueError("Infinite solutions. (Consistent system)")
else:
raise ValueError("No solution. (Inconsistent system)")
else:
if determinant_x == determinant_y == 0:
elif determinant_x == determinant_y == 0:
# Trivial solution (Inconsistent system)
return (0.0, 0.0)
else:

View File

@ -46,8 +46,7 @@ def solution():
elif day > 29 and month == 2:
month += 1
day = day - 29
else:
if day > days_per_month[month - 1]:
elif day > days_per_month[month - 1]:
month += 1
day = day - days_per_month[month - 2]

View File

@ -12,7 +12,6 @@ lint.ignore = [ # `ruff rule S101` for a description of that rule
"NPY002", # Replace legacy `np.random.choice` call with `np.random.Generator` -- FIX ME
"PGH003", # Use specific rule codes when ignoring type issues -- FIX ME
"PLC1901", # `{}` can be simplified to `{}` as an empty string is falsey
"PLR5501", # Consider using `elif` instead of `else` -- FIX ME
"PLW0120", # `else` clause on loop without a `break` statement -- FIX ME
"PLW060", # Using global for `{name}` but no assignment is done -- DO NOT FIX
"PLW2901", # PLW2901: Redefined loop variable -- FIX ME

View File

@ -137,9 +137,8 @@ def hill_climbing(
if change > max_change and change > 0:
max_change = change
next_state = neighbor
else: # finding min
elif change < min_change and change < 0: # finding min
# to direction with greatest descent
if change < min_change and change < 0:
min_change = change
next_state = neighbor
if next_state is not None:

View File

@ -33,15 +33,13 @@ def interpolation_search(sorted_collection, item):
current_item = sorted_collection[point]
if current_item == item:
return point
else:
if point < left:
elif point < left:
right = left
left = point
elif point > right:
left = right
right = point
else:
if item < current_item:
elif item < current_item:
right = point - 1
else:
left = point + 1
@ -79,8 +77,7 @@ def interpolation_search_by_recursion(sorted_collection, item, left, right):
return interpolation_search_by_recursion(sorted_collection, item, point, left)
elif point > right:
return interpolation_search_by_recursion(sorted_collection, item, right, left)
else:
if sorted_collection[point] > item:
elif sorted_collection[point] > item:
return interpolation_search_by_recursion(
sorted_collection, item, left, point - 1
)

View File

@ -60,8 +60,7 @@ def compute_transform_tables(
def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]:
if i == 0 and j == 0:
return []
else:
if ops[i][j][0] in {"C", "R"}:
elif ops[i][j][0] in {"C", "R"}:
seq = assemble_transformation(ops, i - 1, j - 1)
seq.append(ops[i][j])
return seq