Skip to content

Commit d9027b0

Browse files
ty: un-ignore invalid-type-arguments (#15392)
* ty: un-ignore invalid-type-arguments * Refactor Comparable protocol and improve docstrings Removed unused __lt__ method from Comparable protocol and updated docstrings for clarity. * Remove unused total_ordering import Removed unused import of total_ordering from functools. --------- Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent f688e7c commit d9027b0

3 files changed

Lines changed: 16 additions & 25 deletions

File tree

data_structures/heap/heap.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,7 @@
77

88
class Comparable(Protocol):
99
@abstractmethod
10-
def __lt__(self: T, other: T) -> bool:
11-
pass
12-
13-
@abstractmethod
14-
def __gt__(self: T, other: T) -> bool:
15-
pass
16-
17-
@abstractmethod
18-
def __eq__(self: T, other: object) -> bool:
10+
def __gt__(self: T, other: T, /) -> bool:
1911
pass
2012

2113

@@ -54,7 +46,7 @@ def __repr__(self) -> str:
5446

5547
def parent_index(self, child_idx: int) -> int | None:
5648
"""
57-
returns the parent index based on the given child index
49+
Returns the parent index based on the given child index
5850
5951
>>> h = Heap()
6052
>>> h.build_max_heap([103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5])
@@ -90,8 +82,8 @@ def parent_index(self, child_idx: int) -> int | None:
9082

9183
def left_child_idx(self, parent_idx: int) -> int | None:
9284
"""
93-
return the left child index if the left child exists.
94-
if not, return None.
85+
Return the left child index if the left child exists.
86+
If not, return None.
9587
"""
9688
left_child_index = 2 * parent_idx + 1
9789
if left_child_index < self.heap_size:
@@ -100,8 +92,8 @@ def left_child_idx(self, parent_idx: int) -> int | None:
10092

10193
def right_child_idx(self, parent_idx: int) -> int | None:
10294
"""
103-
return the right child index if the right child exists.
104-
if not, return None.
95+
Return the right child index if the right child exists.
96+
If not, return None.
10597
"""
10698
right_child_index = 2 * parent_idx + 2
10799
if right_child_index < self.heap_size:
@@ -110,10 +102,10 @@ def right_child_idx(self, parent_idx: int) -> int | None:
110102

111103
def max_heapify(self, index: int) -> None:
112104
"""
113-
correct a single violation of the heap property in a subtree's root.
105+
Correct a single violation of the heap property in a subtree's root.
114106
115107
It is the function that is responsible for restoring the property
116-
of Max heap i.e the maximum element is always at top.
108+
of a max heap, i.e the maximum element is always at the top.
117109
"""
118110
if index < self.heap_size:
119111
violation: int = index
@@ -133,7 +125,7 @@ def max_heapify(self, index: int) -> None:
133125

134126
def build_max_heap(self, collection: Iterable[T]) -> None:
135127
"""
136-
build max heap from an unsorted array
128+
Build a max heap from an unsorted array
137129
138130
>>> h = Heap()
139131
>>> h.build_max_heap([20,40,50,20,10])
@@ -164,7 +156,7 @@ def build_max_heap(self, collection: Iterable[T]) -> None:
164156

165157
def extract_max(self) -> T:
166158
"""
167-
get and remove max from heap
159+
Get and remove max from heap
168160
169161
>>> h = Heap()
170162
>>> h.build_max_heap([20,40,50,20,10])
@@ -195,7 +187,7 @@ def extract_max(self) -> T:
195187

196188
def insert(self, value: T) -> None:
197189
"""
198-
insert a new value into the max heap
190+
Insert a new value into the max heap
199191
200192
>>> h = Heap()
201193
>>> h.insert(10)
@@ -241,7 +233,6 @@ def heap_sort(self) -> None:
241233
if __name__ == "__main__":
242234
import doctest
243235

244-
# run doc test
245236
doctest.testmod()
246237

247238
# demo

machine_learning/k_nearest_neighbors.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616
from heapq import nsmallest
1717

1818
import numpy as np
19+
from numpy.typing import NDArray
1920
from sklearn import datasets
2021
from sklearn.model_selection import train_test_split
2122

2223

2324
class KNN:
2425
def __init__(
2526
self,
26-
train_data: np.ndarray[float],
27-
train_target: np.ndarray[int],
27+
train_data: NDArray[np.float64],
28+
train_target: NDArray[np.int64],
2829
class_labels: list[str],
2930
) -> None:
3031
"""
@@ -34,7 +35,7 @@ def __init__(
3435
self.labels = class_labels
3536

3637
@staticmethod
37-
def _euclidean_distance(a: np.ndarray[float], b: np.ndarray[float]) -> float:
38+
def _euclidean_distance(a: NDArray[np.float64], b: NDArray[np.float64]) -> float:
3839
"""
3940
Calculate the Euclidean distance between two points
4041
>>> KNN._euclidean_distance(np.array([0, 0]), np.array([3, 4]))
@@ -44,7 +45,7 @@ def _euclidean_distance(a: np.ndarray[float], b: np.ndarray[float]) -> float:
4445
"""
4546
return float(np.linalg.norm(a - b))
4647

47-
def classify(self, pred_point: np.ndarray[float], k: int = 5) -> str:
48+
def classify(self, pred_point: NDArray[np.float64], k: int = 5) -> str:
4849
"""
4950
Classify a given point using the kNN algorithm
5051
>>> train_X = np.array(

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ rules.call-non-callable = "ignore"
205205
rules.deprecated = "ignore"
206206
rules.invalid-argument-type = "ignore"
207207
rules.invalid-return-type = "ignore"
208-
rules.invalid-type-arguments = "ignore"
209208
rules.no-matching-overload = "ignore"
210209
rules.not-iterable = "ignore"
211210
rules.not-subscriptable = "ignore"

0 commit comments

Comments
 (0)