From 47a0661c20ea7362783fa1c0a34ab9286b149194 Mon Sep 17 00:00:00 2001 From: Henrik Skov Midtiby Date: Wed, 6 Nov 2024 13:45:15 +0100 Subject: [PATCH] Handle mypy errors in utils/space_ops.py --- manim/utils/space_ops.py | 37 +++++++++++++++++++++++-------------- mypy.ini | 3 +++ 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/manim/utils/space_ops.py b/manim/utils/space_ops.py index 0017a53ef7..431f33fcd2 100644 --- a/manim/utils/space_ops.py +++ b/manim/utils/space_ops.py @@ -4,7 +4,7 @@ import itertools as it from collections.abc import Sequence -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Callable import numpy as np from mapbox_earcut import triangulate_float32 as earcut @@ -59,7 +59,8 @@ def norm_squared(v: float) -> float: - return np.dot(v, v) + val: float = np.dot(v, v) + return val def cross(v1: Vector3D, v2: Vector3D) -> Vector3D: @@ -200,7 +201,7 @@ def rotate_vector( return rotation_matrix(angle, axis) @ vector -def thick_diagonal(dim: int, thickness=2) -> np.ndarray: +def thick_diagonal(dim: int, thickness: float = 2) -> np.ndarray: row_indices = np.arange(dim).repeat(dim).reshape((dim, dim)) col_indices = np.transpose(row_indices) return (np.abs(row_indices - col_indices) < thickness).astype("uint8") @@ -318,8 +319,10 @@ def angle_of_vector(vector: Sequence[float] | np.ndarray) -> float: c_vec = np.empty(vector.shape[1], dtype=np.complex128) c_vec.real = vector[0] c_vec.imag = vector[1] - return np.angle(c_vec) - return np.angle(complex(*vector[:2])) + val1: float = np.angle(c_vec) + return val1 + val: float = np.angle(complex(*vector[:2])) + return val def angle_between_vectors(v1: np.ndarray, v2: np.ndarray) -> float: @@ -338,13 +341,17 @@ def angle_between_vectors(v1: np.ndarray, v2: np.ndarray) -> float: float The angle between the vectors. """ - return 2 * np.arctan2( + val: float = 2 * np.arctan2( np.linalg.norm(normalize(v1) - normalize(v2)), np.linalg.norm(normalize(v1) + normalize(v2)), ) + return val -def normalize(vect: np.ndarray | tuple[float], fall_back=None) -> np.ndarray: + +def normalize( + vect: np.ndarray | tuple[float], fall_back: np.ndarray | None = None +) -> np.ndarray: norm = np.linalg.norm(vect) if norm > 0: return np.array(vect) / norm @@ -487,11 +494,11 @@ def R3_to_complex(point: Sequence[float]) -> np.ndarray: return complex(*point[:2]) -def complex_func_to_R3_func(complex_func): +def complex_func_to_R3_func(complex_func: Callable) -> Callable: return lambda p: complex_to_R3(complex_func(R3_to_complex(p))) -def center_of_mass(points: Sequence[float]) -> np.ndarray: +def center_of_mass(points: list[Sequence[float]]) -> np.ndarray: """Gets the center of mass of the points in space. Parameters @@ -619,12 +626,13 @@ def get_winding_number(points: Sequence[np.ndarray]) -> float: >>> get_winding_number(polygon.get_vertices()) 0.0 """ - total_angle = 0 + total_angle: float = 0 for p1, p2 in adjacent_pairs(points): d_angle = angle_of_vector(p2) - angle_of_vector(p1) d_angle = ((d_angle + PI) % TAU) - PI total_angle += d_angle - return total_angle / TAU + val: float = total_angle / TAU + return val def shoelace(x_y: np.ndarray) -> float: @@ -637,7 +645,8 @@ def shoelace(x_y: np.ndarray) -> float: """ x = x_y[:, 0] y = x_y[:, 1] - return np.trapz(y, x) + val: float = np.trapz(y, x) + return val def shoelace_direction(x_y: np.ndarray) -> str: @@ -758,7 +767,7 @@ def earclip_triangulation(verts: np.ndarray, ring_ends: list) -> list: raise Exception("Could not find a ring to attach") # Setup linked list - after = [] + after: list = [] end0 = 0 for end1 in ring_ends: after.extend(range(end0 + 1, end1)) @@ -829,7 +838,7 @@ def spherical_to_cartesian(spherical: Sequence[float]) -> np.ndarray: def perpendicular_bisector( line: Sequence[np.ndarray], - norm_vector=OUT, + norm_vector: Vector3D = OUT, ) -> Sequence[np.ndarray]: """Returns a list of two points that correspond to the ends of the perpendicular bisector of the diff --git a/mypy.ini b/mypy.ini index cc298f009f..66824dd1b6 100644 --- a/mypy.ini +++ b/mypy.ini @@ -109,6 +109,9 @@ ignore_errors = False [mypy-manim.utils.parameter_parsing.*] ignore_errors = False +[mypy-manim.utils.space_ops.*] +ignore_errors = False + [mypy-manim.utils.simple_functions.*] ignore_errors = False