diff --git a/computer_vision/mosaic_augmentation.py b/computer_vision/mosaic_augmentation.py index 753b8bf012ef..b3c86f341af0 100644 --- a/computer_vision/mosaic_augmentation.py +++ b/computer_vision/mosaic_augmentation.py @@ -91,7 +91,7 @@ def update_image_and_anno( output_size: tuple[int, int], scale_range: tuple[float, float], filter_scale: float = 0.0, -) -> tuple[list, list, str]: +) -> tuple[np.ndarray, list, str]: """ - all_img_list : list of all images - all_annos : list of all annotations of specific image @@ -111,7 +111,7 @@ def update_image_and_anno( divid_point_y = int(scale_y * output_size[0]) new_anno = [] - path_list = [] + path_list: list[str] = [] for i, index in enumerate(idxs): path = all_img_list[index] path_list.append(path) diff --git a/data_structures/arrays/dynamic_array.py b/data_structures/arrays/dynamic_array.py index 9724270cb277..2db537c722d6 100644 --- a/data_structures/arrays/dynamic_array.py +++ b/data_structures/arrays/dynamic_array.py @@ -2,7 +2,8 @@ class DynamicArray: def __init__(self) -> None: self.size = 0 # Number of elements in the array self.capacity = 1 # Initial capacity of the array - self.array = [None] * self.capacity # Create an array with initial capacity + # Create an array with initial capacity + self.array: list[int | None] = [None] * self.capacity def append(self, item: int) -> None: """ @@ -77,7 +78,9 @@ def get(self, index: int) -> int: """ if index < 0 or index >= self.size: raise IndexError("index out of range") - return self.array[index] + value = self.array[index] + assert value is not None # every filled slot holds an int + return value def __setitem__(self, index: int, value: int) -> None: if index < 0 or index >= self.size: diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index d2c4030b921b..ae0d4171f72f 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -46,7 +46,7 @@ def pass_and_relaxation( def bidirectional_dij( source: str, destination: str, graph_forward: dict, graph_backward: dict -) -> int: +) -> float: """ Bi-directional Dijkstra's algorithm. diff --git a/graphs/hopcroft_karp.py b/graphs/hopcroft_karp.py index fcbd22095517..0fcb2223be86 100644 --- a/graphs/hopcroft_karp.py +++ b/graphs/hopcroft_karp.py @@ -49,6 +49,7 @@ import math from collections import deque +from typing import cast _NIL = object() @@ -228,11 +229,14 @@ def maximum_matching(self) -> dict[T, T]: if self.pair_left[left_vertex] is _NIL: self.depth_first_search(left_vertex) - return { - left_vertex: matched_right # type: ignore[misc] - for left_vertex, matched_right in self.pair_left.items() - if matched_right is not _NIL - } + return cast( + "dict[T, T]", + { + left_vertex: matched_right + for left_vertex, matched_right in self.pair_left.items() + if matched_right is not _NIL + }, + ) def hopcroft_karp[T](graph: dict[T, list[T]]) -> dict[T, T]: diff --git a/machine_learning/mfcc.py b/machine_learning/mfcc.py index dcc3151d5a1a..5fb786a5d24d 100644 --- a/machine_learning/mfcc.py +++ b/machine_learning/mfcc.py @@ -58,6 +58,7 @@ """ import logging +from typing import overload import numpy as np import scipy.fftpack as fft @@ -289,7 +290,11 @@ def freq_to_mel(freq: float) -> float: return 2595.0 * np.log10(1.0 + freq / 700.0) -def mel_to_freq(mels: float) -> float: +@overload +def mel_to_freq(mels: float) -> float: ... +@overload +def mel_to_freq(mels: np.ndarray) -> np.ndarray: ... +def mel_to_freq(mels: float | np.ndarray) -> float | np.ndarray: """ Convert a frequency in the mel scale to Hertz. diff --git a/networking_flow/ford_fulkerson.py b/networking_flow/ford_fulkerson.py index 52c77902c68b..234b1deb8a8e 100644 --- a/networking_flow/ford_fulkerson.py +++ b/networking_flow/ford_fulkerson.py @@ -58,7 +58,7 @@ def breadth_first_search(graph: list, source: int, sink: int, parents: list) -> return visited[sink] -def ford_fulkerson(graph: list, source: int, sink: int) -> int: +def ford_fulkerson(graph: list, source: int, sink: int) -> int | float: """ This function returns the maximum flow from source to sink in the given graph. diff --git a/other/dancing_links.py b/other/dancing_links.py index 45ddd7f5ba12..2aa1942cef0a 100644 --- a/other/dancing_links.py +++ b/other/dancing_links.py @@ -106,7 +106,7 @@ def _uncover(self, col: ColumnNode) -> None: col.right.left = col col.left.right = col - def _choose_column(self) -> ColumnNode: + def _choose_column(self) -> ColumnNode | None: """Select the column with the smallest size (heuristic).""" min_size = float("inf") chosen = None diff --git a/pyproject.toml b/pyproject.toml index 932f64f2e00a..066b2a70531d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -318,7 +318,6 @@ skip = """\ environment.python-version = "3.15" rules.call-non-callable = "ignore" rules.invalid-argument-type = "ignore" -rules.invalid-return-type = "ignore" rules.no-matching-overload = "ignore" rules.not-iterable = "ignore" rules.not-subscriptable = "ignore" diff --git a/web_programming/random_anime_character.py b/web_programming/random_anime_character.py index 38d207162553..4e9993c5dc4e 100644 --- a/web_programming/random_anime_character.py +++ b/web_programming/random_anime_character.py @@ -34,6 +34,7 @@ def random_anime_character() -> tuple[str, str, str]: httpx2.get(URL, headers=headers, timeout=10).text, "html.parser" ) title = soup.find("meta", attrs={"property": "og:title"}).attrs["content"] + assert isinstance(title, str) image_url = soup.find("meta", attrs={"property": "og:image"}).attrs["content"] description = soup.find("p", id="description").get_text() _, image_extension = os.path.splitext(os.path.basename(image_url))