Skip to content

Commit 945b5ab

Browse files
data_structures: add return type annotations (ANN201) (#15306)
Annotate the unambiguous, primitive/container-returning public methods flagged by ruff ANN201 across data_structures/ (int/bool/float/list/dict and one fluent self-return). Reduces ANN201 in this directory from 58 to 36. Element-typed and sentinel-union returns (e.g. node .data getters, dict|False in the Sudoku solver) are intentionally left for follow-up, since they warrant generics/TypeVar or a type checker rather than a guess. Refs #15296
1 parent f0391e0 commit 945b5ab

10 files changed

Lines changed: 22 additions & 22 deletions

File tree

data_structures/binary_tree/segment_tree.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __init__(self, a) -> None:
1111
if self.N:
1212
self.build(1, 0, self.N - 1)
1313

14-
def left(self, idx):
14+
def left(self, idx) -> int:
1515
"""
1616
Returns the left child index for a given index in a binary tree.
1717
@@ -23,7 +23,7 @@ def left(self, idx):
2323
"""
2424
return idx * 2
2525

26-
def right(self, idx):
26+
def right(self, idx) -> int:
2727
"""
2828
Returns the right child index for a given index in a binary tree.
2929
@@ -44,7 +44,7 @@ def build(self, idx, left, right) -> None:
4444
self.build(self.right(idx), mid + 1, right)
4545
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
4646

47-
def update(self, a, b, val):
47+
def update(self, a, b, val) -> bool:
4848
"""
4949
Update the values in the segment tree in the range [a,b] with the given value.
5050
@@ -71,7 +71,7 @@ def update_recursive(self, idx, left, right, a, b, val) -> bool:
7171
self.st[idx] = max(self.st[self.left(idx)], self.st[self.right(idx)])
7272
return True
7373

74-
def query(self, a, b):
74+
def query(self, a, b) -> float:
7575
"""
7676
Query the maximum value in the range [a,b].
7777
@@ -83,7 +83,7 @@ def query(self, a, b):
8383
"""
8484
return self.query_recursive(1, 0, self.N - 1, a - 1, b - 1)
8585

86-
def query_recursive(self, idx, left, right, a, b):
86+
def query_recursive(self, idx, left, right, a, b) -> float:
8787
"""
8888
query(1, 1, N, a, b) for query max of [a,b]
8989
"""

data_structures/hashing/hash_table.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(
2222
self.__aux_list: list = []
2323
self._keys: dict = {}
2424

25-
def keys(self):
25+
def keys(self) -> dict:
2626
"""
2727
The keys function returns a dictionary containing the key value pairs.
2828
key being the index number in hash table and value being the data value.
@@ -48,12 +48,12 @@ def keys(self):
4848
"""
4949
return self._keys
5050

51-
def balanced_factor(self):
51+
def balanced_factor(self) -> float:
5252
return sum(1 for slot in self.values if slot is not None) / (
5353
self.size_table * self.charge_factor
5454
)
5555

56-
def hash_function(self, key):
56+
def hash_function(self, key) -> int:
5757
"""
5858
Generates hash for the given key value
5959

data_structures/hashing/hash_table_with_linked_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def _set_value(self, key, data) -> None:
1212
self.values[key].appendleft(data)
1313
self._keys[key] = self.values[key]
1414

15-
def balanced_factor(self):
15+
def balanced_factor(self) -> float:
1616
return (
1717
sum(self.charge_factor - len(slot) for slot in self.values)
1818
/ self.size_table

data_structures/heap/binomial_heap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ def peek(self):
251251
"""
252252
return self.min_node.val
253253

254-
def is_empty(self):
254+
def is_empty(self) -> bool:
255255
return self.size == 0
256256

257257
def delete_min(self):

data_structures/heap/max_heap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def pop(self) -> int:
6060
return max_value
6161

6262
@property
63-
def get_list(self):
63+
def get_list(self) -> list:
6464
return self.__heap[1:]
6565

6666
def __len__(self) -> int:

data_structures/heap/min_heap.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ def __init__(self, array) -> None:
3939
def __getitem__(self, key):
4040
return self.get_value(key)
4141

42-
def get_parent_idx(self, idx):
42+
def get_parent_idx(self, idx) -> int:
4343
return (idx - 1) // 2
4444

45-
def get_left_child_idx(self, idx):
45+
def get_left_child_idx(self, idx) -> int:
4646
return idx * 2 + 1
4747

48-
def get_right_child_idx(self, idx):
48+
def get_right_child_idx(self, idx) -> int:
4949
return idx * 2 + 2
5050

5151
def get_value(self, key):
5252
return self.heap_dict[key]
5353

54-
def build_heap(self, array):
54+
def build_heap(self, array) -> list:
5555
last_idx = len(array) - 1
5656
start_from = self.get_parent_idx(last_idx)
5757

@@ -120,7 +120,7 @@ def insert(self, node) -> None:
120120
self.heap_dict[node.name] = node.val
121121
self.sift_up(len(self.heap) - 1)
122122

123-
def is_empty(self):
123+
def is_empty(self) -> bool:
124124
return len(self.heap) == 0
125125

126126
def decrease_key(self, node, new_value) -> None:

data_structures/linked_list/doubly_linked_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def delete(self, data) -> str:
196196
current.next.previous = current.previous # 1 <--> 3
197197
return data
198198

199-
def is_empty(self):
199+
def is_empty(self) -> bool:
200200
"""
201201
>>> linked_list = DoublyLinkedList()
202202
>>> linked_list.is_empty()

data_structures/linked_list/doubly_linked_list_two.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def remove_node_pointers(node: Node) -> None:
158158
node.next = None
159159
node.previous = None
160160

161-
def is_empty(self):
161+
def is_empty(self) -> bool:
162162
return self.head is None
163163

164164

data_structures/queues/circular_queue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def first(self):
4747
"""
4848
return False if self.is_empty() else self.array[self.front]
4949

50-
def enqueue(self, data):
50+
def enqueue(self, data) -> "CircularQueue":
5151
"""
5252
This function inserts an element at the end of the queue using self.rear value
5353
as an index.

data_structures/stacks/prefix_evaluation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212

1313

14-
def is_operand(c):
14+
def is_operand(c) -> bool:
1515
"""
1616
Return True if the given char c is an operand, e.g. it is a number
1717
@@ -23,7 +23,7 @@ def is_operand(c):
2323
return c.isdigit()
2424

2525

26-
def evaluate(expression):
26+
def evaluate(expression) -> float:
2727
"""
2828
Evaluate a given expression in prefix notation.
2929
Asserts that the given expression is valid.
@@ -55,7 +55,7 @@ def evaluate(expression):
5555
return stack.pop()
5656

5757

58-
def evaluate_recursive(expression: list[str]):
58+
def evaluate_recursive(expression: list[str]) -> float:
5959
"""
6060
Alternative recursive implementation
6161

0 commit comments

Comments
 (0)