diff --git a/cellular_automata/conways_game_of_life.py b/cellular_automata/conways_game_of_life.py index 485f0d47bd8b..4009d0e1b7ae 100644 --- a/cellular_automata/conways_game_of_life.py +++ b/cellular_automata/conways_game_of_life.py @@ -78,6 +78,7 @@ def generate_images(cells: list[list[int]], frames: int) -> list[Image.Image]: # Create output image img = Image.new("RGB", (len(cells[0]), len(cells))) pixels = img.load() + assert pixels is not None # Save cells to image for x in range(len(cells)): diff --git a/cellular_automata/one_dimensional.py b/cellular_automata/one_dimensional.py index da77e444502f..04d55c2473e8 100644 --- a/cellular_automata/one_dimensional.py +++ b/cellular_automata/one_dimensional.py @@ -55,6 +55,7 @@ def generate_image(cells: list[list[int]]) -> Image.Image: # Create the output image img = Image.new("RGB", (len(cells[0]), len(cells))) pixels = img.load() + assert pixels is not None # Generates image for w in range(img.width): for h in range(img.height): diff --git a/data_structures/binary_tree/non_recursive_segment_tree.py b/data_structures/binary_tree/non_recursive_segment_tree.py index 7d1c965fab50..d2f36547ca69 100644 --- a/data_structures/binary_tree/non_recursive_segment_tree.py +++ b/data_structures/binary_tree/non_recursive_segment_tree.py @@ -39,7 +39,7 @@ from __future__ import annotations from collections.abc import Callable -from typing import Any, TypeVar +from typing import TypeVar, cast T = TypeVar("T") @@ -57,10 +57,9 @@ def __init__(self, arr: list[T], fnc: Callable[[T, T], T]) -> None: ... lambda a, b: (a[0] + b[0], a[1] + b[1])).query(0, 2) (6, 9) """ - any_type: Any | T = None self.N: int = len(arr) - self.st: list[T] = [any_type for _ in range(self.N)] + arr + self.st: list[T] = [cast(T, None) for _ in range(self.N)] + arr self.fn = fnc self.build() diff --git a/data_structures/heap/binomial_heap.py b/data_structures/heap/binomial_heap.py index 9cfdf0c12fe0..c97ec1c32149 100644 --- a/data_structures/heap/binomial_heap.py +++ b/data_structures/heap/binomial_heap.py @@ -222,6 +222,7 @@ def insert(self, val): if val < self.min_node.val: self.min_node = new_node # Put new_node as a bottom_root in heap + assert self.bottom_root is not None self.bottom_root.left = new_node new_node.parent = self.bottom_root self.bottom_root = new_node @@ -283,6 +284,7 @@ def delete_min(self): # Update bottom root self.bottom_root = self.bottom_root.parent + assert self.bottom_root is not None self.bottom_root.left = None # Update min_node diff --git a/data_structures/linked_list/doubly_linked_list.py b/data_structures/linked_list/doubly_linked_list.py index bd3445f9f6c5..1eee15edf714 100644 --- a/data_structures/linked_list/doubly_linked_list.py +++ b/data_structures/linked_list/doubly_linked_list.py @@ -2,12 +2,16 @@ https://en.wikipedia.org/wiki/Doubly_linked_list """ +from __future__ import annotations + +from typing import Any + class Node: - def __init__(self, data): + def __init__(self, data: Any): self.data = data - self.previous = None - self.next = None + self.previous: Node | None = None + self.next: Node | None = None def __str__(self): return f"{self.data}" @@ -15,8 +19,8 @@ def __str__(self): class DoublyLinkedList: def __init__(self): - self.head = None - self.tail = None + self.head: Node | None = None + self.tail: Node | None = None def __iter__(self): """ @@ -93,13 +97,18 @@ def insert_at_nth(self, index: int, data): new_node.next = self.head self.head = new_node elif index == length: + assert self.tail is not None self.tail.next = new_node + assert self.tail is not None new_node.previous = self.tail self.tail = new_node else: temp = self.head + assert temp is not None for _ in range(index): temp = temp.next + assert temp is not None + assert temp.previous is not None temp.previous.next = new_node new_node.previous = temp.previous new_node.next = temp @@ -141,23 +150,32 @@ def delete_at_nth(self, index: int): if length == 1: self.head = self.tail = None elif index == 0: + assert self.head is not None self.head = self.head.next + assert self.head is not None self.head.previous = None elif index == length - 1: + assert self.tail is not None delete_node = self.tail self.tail = self.tail.previous + assert self.tail is not None self.tail.next = None else: temp = self.head + assert temp is not None for _ in range(index): temp = temp.next + assert temp is not None delete_node = temp + assert temp.next is not None + assert temp.previous is not None temp.next.previous = temp.previous temp.previous.next = temp.next return delete_node.data def delete(self, data) -> str: current = self.head + assert current is not None while current.data != data: # Find the position to delete if current.next: @@ -172,6 +190,8 @@ def delete(self, data) -> str: self.delete_tail() else: # Before: 1 <--> 2(current) <--> 3 + assert current.previous is not None + assert current.next is not None current.previous.next = current.next # 1 --> 3 current.next.previous = current.previous # 1 <--> 3 return data diff --git a/data_structures/linked_list/singly_linked_list.py b/data_structures/linked_list/singly_linked_list.py index 2c6713a47ad9..3ec91242d62c 100644 --- a/data_structures/linked_list/singly_linked_list.py +++ b/data_structures/linked_list/singly_linked_list.py @@ -45,7 +45,7 @@ def __init__(self): >>> linked_list.head is None True """ - self.head = None + self.head: Node | None = None def __iter__(self) -> Iterator[Any]: """ @@ -153,8 +153,10 @@ def __setitem__(self, index: int, data: Any) -> None: if not 0 <= index < len(self): raise ValueError("list index out of range.") current = self.head + assert current is not None for _ in range(index): current = current.next_node + assert current is not None current.data = data def insert_tail(self, data: Any) -> None: @@ -215,8 +217,10 @@ def insert_nth(self, index: int, data: Any) -> None: self.head = new_node else: temp = self.head + assert temp is not None for _ in range(index - 1): temp = temp.next_node + assert temp is not None new_node.next_node = temp.next_node temp.next_node = new_node @@ -316,10 +320,13 @@ def delete_nth(self, index: int = 0) -> Any: self.head = self.head.next_node else: temp = self.head + assert temp is not None for _ in range(index - 1): temp = temp.next_node + assert temp is not None delete_node = temp.next_node - temp.next_node = temp.next_node.next_node + assert delete_node is not None + temp.next_node = delete_node.next_node return delete_node.data def is_empty(self) -> bool: diff --git a/fractals/mandelbrot.py b/fractals/mandelbrot.py index 359d965a882d..6b849c1b00a6 100644 --- a/fractals/mandelbrot.py +++ b/fractals/mandelbrot.py @@ -109,6 +109,7 @@ def get_image( """ img = Image.new("RGB", (image_width, image_height)) pixels = img.load() + assert pixels is not None # loop through the image-coordinates for image_x in range(image_width): diff --git a/machine_learning/automatic_differentiation.py b/machine_learning/automatic_differentiation.py index 93e77e761ef9..ced44b9a7c27 100644 --- a/machine_learning/automatic_differentiation.py +++ b/machine_learning/automatic_differentiation.py @@ -9,7 +9,6 @@ from __future__ import annotations -from collections import defaultdict from enum import Enum from types import TracebackType from typing import Any, Self @@ -258,7 +257,7 @@ def gradient(self, target: Variable, source: Variable) -> np.ndarray | None: """ # partial derivatives with respect to target - partial_deriv = defaultdict(lambda: 0) + partial_deriv: dict[Variable, np.ndarray] = {} partial_deriv[target] = np.ones_like(target.to_ndarray()) # iterating through each operations in the computation graph @@ -270,7 +269,10 @@ def gradient(self, target: Variable, source: Variable) -> np.ndarray | None: # of variables with respect to the target dparam_doutput = self.derivative(param, operation) dparam_dtarget = dparam_doutput * partial_deriv[operation.output] - partial_deriv[param] += dparam_dtarget + partial_deriv[param] = ( + partial_deriv.get(param, np.zeros_like(dparam_dtarget)) + + dparam_dtarget + ) if param.result_of and param.result_of != OpType.NOOP: operation_queue.append(param.result_of) diff --git a/networking_flow/minimum_cut.py b/networking_flow/minimum_cut.py index c1f4a83b6aee..fbe170626faf 100644 --- a/networking_flow/minimum_cut.py +++ b/networking_flow/minimum_cut.py @@ -71,7 +71,7 @@ def mincut(graph: list[list[int]], source: int, sink: int) -> list[tuple[int, in parent = [-1] * (len(residual)) res = [] while bfs(residual, source, sink, parent): - path_flow = float("inf") + path_flow = max(max(row) for row in residual) s = sink while s != source: diff --git a/neural_network/input_data.py b/neural_network/input_data.py index 3a8628f939f8..7f0b7538df54 100644 --- a/neural_network/input_data.py +++ b/neural_network/input_data.py @@ -21,6 +21,7 @@ import os import typing import urllib +import urllib.request import numpy as np from tensorflow.python.framework import dtypes, random_seed diff --git a/pyproject.toml b/pyproject.toml index 20a7120a1307..d57fc6f0d257 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -192,7 +192,6 @@ environment.python-version = "3.14" rules.call-non-callable = "ignore" rules.deprecated = "ignore" rules.invalid-argument-type = "ignore" -rules.invalid-assignment = "ignore" rules.invalid-parameter-default = "ignore" rules.invalid-return-type = "ignore" rules.invalid-type-arguments = "ignore" diff --git a/scheduling/cpuschedulingalgorithms.py b/scheduling/cpuschedulingalgorithms.py index 7a39d57a213f..aba85b482230 100644 --- a/scheduling/cpuschedulingalgorithms.py +++ b/scheduling/cpuschedulingalgorithms.py @@ -334,7 +334,10 @@ def add_process(self) -> None: def delete_process(self) -> None: """Deletes a selected process.""" if sel := self.tree.selection(): - pid = self.tree.item(sel[0])["values"][0] + values = self.tree.item(sel[0])["values"] + if not values: + return + pid = values[0] self.processes = [p for p in self.processes if p["pid"] != pid] self.tree.delete(sel[0])