From 8df815892aac118b0666d6b1b2c6a23f91bbf671 Mon Sep 17 00:00:00 2001 From: Khushityagi90 Date: Mon, 21 Sep 2026 20:53:49 +0530 Subject: [PATCH 1/3] ty: un-ignore invalid-type-arguments --- data_structures/heap/heap.py | 6 +++--- machine_learning/k_nearest_neighbors.py | 9 +++++---- pyproject.toml | 1 - 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/data_structures/heap/heap.py b/data_structures/heap/heap.py index 41ef0ddd1005..d13cbe612ff7 100644 --- a/data_structures/heap/heap.py +++ b/data_structures/heap/heap.py @@ -7,15 +7,15 @@ class Comparable(Protocol): @abstractmethod - def __lt__(self: T, other: T) -> bool: + def __lt__(self: T, other: T, /) -> bool: pass @abstractmethod - def __gt__(self: T, other: T) -> bool: + def __gt__(self: T, other: T, /) -> bool: pass @abstractmethod - def __eq__(self: T, other: object) -> bool: + def __eq__(self: T, other: object, /) -> bool: pass diff --git a/machine_learning/k_nearest_neighbors.py b/machine_learning/k_nearest_neighbors.py index 28697dbf890d..1fbd5f8df27c 100644 --- a/machine_learning/k_nearest_neighbors.py +++ b/machine_learning/k_nearest_neighbors.py @@ -16,6 +16,7 @@ from heapq import nsmallest import numpy as np +from numpy.typing import NDArray from sklearn import datasets from sklearn.model_selection import train_test_split @@ -23,8 +24,8 @@ class KNN: def __init__( self, - train_data: np.ndarray[float], - train_target: np.ndarray[int], + train_data: NDArray[np.float64], + train_target: NDArray[np.int64], class_labels: list[str], ) -> None: """ @@ -34,7 +35,7 @@ def __init__( self.labels = class_labels @staticmethod - def _euclidean_distance(a: np.ndarray[float], b: np.ndarray[float]) -> float: + def _euclidean_distance(a: NDArray[np.float64], b: NDArray[np.float64]) -> float: """ Calculate the Euclidean distance between two points >>> 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: """ return float(np.linalg.norm(a - b)) - def classify(self, pred_point: np.ndarray[float], k: int = 5) -> str: + def classify(self, pred_point: NDArray[np.float64], k: int = 5) -> str: """ Classify a given point using the kNN algorithm >>> train_X = np.array( diff --git a/pyproject.toml b/pyproject.toml index e5181860e03c..34cdd0cd3712 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -205,7 +205,6 @@ rules.call-non-callable = "ignore" rules.deprecated = "ignore" rules.invalid-argument-type = "ignore" rules.invalid-return-type = "ignore" -rules.invalid-type-arguments = "ignore" rules.no-matching-overload = "ignore" rules.not-iterable = "ignore" rules.not-subscriptable = "ignore" From c33c13cab1c72ac6fae1be8c28210d55617d037c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 21 Sep 2026 18:23:53 +0200 Subject: [PATCH 2/3] Refactor Comparable protocol and improve docstrings Removed unused __lt__ method from Comparable protocol and updated docstrings for clarity. --- data_structures/heap/heap.py | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/data_structures/heap/heap.py b/data_structures/heap/heap.py index d13cbe612ff7..20f88c18cf23 100644 --- a/data_structures/heap/heap.py +++ b/data_structures/heap/heap.py @@ -2,26 +2,20 @@ from abc import abstractmethod from collections.abc import Iterable +from functools import total_ordering from typing import Protocol, TypeVar class Comparable(Protocol): - @abstractmethod - def __lt__(self: T, other: T, /) -> bool: - pass - @abstractmethod def __gt__(self: T, other: T, /) -> bool: pass - @abstractmethod - def __eq__(self: T, other: object, /) -> bool: - pass - T = TypeVar("T", bound=Comparable) +@total_ordering class Heap[T: Comparable]: """A Max Heap Implementation @@ -54,7 +48,7 @@ def __repr__(self) -> str: def parent_index(self, child_idx: int) -> int | None: """ - returns the parent index based on the given child index + Returns the parent index based on the given child index >>> h = Heap() >>> h.build_max_heap([103, 9, 1, 7, 11, 15, 25, 201, 209, 107, 5]) @@ -90,8 +84,8 @@ def parent_index(self, child_idx: int) -> int | None: def left_child_idx(self, parent_idx: int) -> int | None: """ - return the left child index if the left child exists. - if not, return None. + Return the left child index if the left child exists. + If not, return None. """ left_child_index = 2 * parent_idx + 1 if left_child_index < self.heap_size: @@ -100,8 +94,8 @@ def left_child_idx(self, parent_idx: int) -> int | None: def right_child_idx(self, parent_idx: int) -> int | None: """ - return the right child index if the right child exists. - if not, return None. + Return the right child index if the right child exists. + If not, return None. """ right_child_index = 2 * parent_idx + 2 if right_child_index < self.heap_size: @@ -110,10 +104,10 @@ def right_child_idx(self, parent_idx: int) -> int | None: def max_heapify(self, index: int) -> None: """ - correct a single violation of the heap property in a subtree's root. + Correct a single violation of the heap property in a subtree's root. It is the function that is responsible for restoring the property - of Max heap i.e the maximum element is always at top. + of a max heap, i.e the maximum element is always at the top. """ if index < self.heap_size: violation: int = index @@ -133,7 +127,7 @@ def max_heapify(self, index: int) -> None: def build_max_heap(self, collection: Iterable[T]) -> None: """ - build max heap from an unsorted array + Build a max heap from an unsorted array >>> h = Heap() >>> h.build_max_heap([20,40,50,20,10]) @@ -164,7 +158,7 @@ def build_max_heap(self, collection: Iterable[T]) -> None: def extract_max(self) -> T: """ - get and remove max from heap + Get and remove max from heap >>> h = Heap() >>> h.build_max_heap([20,40,50,20,10]) @@ -195,7 +189,7 @@ def extract_max(self) -> T: def insert(self, value: T) -> None: """ - insert a new value into the max heap + Insert a new value into the max heap >>> h = Heap() >>> h.insert(10) @@ -241,7 +235,6 @@ def heap_sort(self) -> None: if __name__ == "__main__": import doctest - # run doc test doctest.testmod() # demo From 6a1bdc5b4092264aa00d801a208bfc91f46572bb Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 21 Sep 2026 18:25:26 +0200 Subject: [PATCH 3/3] Remove unused total_ordering import Removed unused import of total_ordering from functools. --- data_structures/heap/heap.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/data_structures/heap/heap.py b/data_structures/heap/heap.py index 20f88c18cf23..7614e3d7f500 100644 --- a/data_structures/heap/heap.py +++ b/data_structures/heap/heap.py @@ -2,7 +2,6 @@ from abc import abstractmethod from collections.abc import Iterable -from functools import total_ordering from typing import Protocol, TypeVar @@ -15,7 +14,6 @@ def __gt__(self: T, other: T, /) -> bool: T = TypeVar("T", bound=Comparable) -@total_ordering class Heap[T: Comparable]: """A Max Heap Implementation