From ffea5403990ad1ba7ca242e03ec1c82b55693a17 Mon Sep 17 00:00:00 2001 From: "Patrick J. Roddy" Date: Fri, 17 Nov 2023 16:21:12 +0000 Subject: [PATCH] Disable `disallow_any_generics` for `mypy` (#316) --- examples/polar_cap/simons_5_1.py | 4 +++- examples/polar_cap/simons_5_3.py | 6 +++++- pyproject.toml | 2 +- src/sleplet/_data/setup_pooch.py | 2 +- src/sleplet/_mask_methods.py | 3 ++- src/sleplet/_mesh_methods.py | 9 ++++++--- src/sleplet/_plotly_methods.py | 6 ++++-- src/sleplet/_scripts/plotting_on_sphere.py | 2 +- src/sleplet/_slepian_arbitrary_methods.py | 6 +++--- src/sleplet/_string_methods.py | 10 +++++++++- src/sleplet/meshes/mesh_slepian.py | 2 +- src/sleplet/plotting/_create_plot_sphere.py | 2 +- src/sleplet/slepian/slepian_limit_lat_lon.py | 6 +++--- 13 files changed, 40 insertions(+), 20 deletions(-) diff --git a/examples/polar_cap/simons_5_1.py b/examples/polar_cap/simons_5_1.py index a5ec1376c..391af8649 100644 --- a/examples/polar_cap/simons_5_1.py +++ b/examples/polar_cap/simons_5_1.py @@ -1,3 +1,5 @@ +import typing + import matplotlib.pyplot as plt import numpy as np import numpy.typing as npt @@ -47,7 +49,7 @@ def main() -> None: def _helper( # noqa: PLR0913 - ax: npt.NDArray, + ax: npt.NDArray[typing.Any], slepian: sleplet.slepian.SlepianPolarCap, resolution: int, x: npt.NDArray[np.float_], diff --git a/examples/polar_cap/simons_5_3.py b/examples/polar_cap/simons_5_3.py index e6b128dd7..97ad6730d 100644 --- a/examples/polar_cap/simons_5_3.py +++ b/examples/polar_cap/simons_5_3.py @@ -1,3 +1,5 @@ +import typing + import matplotlib.pyplot as plt import numpy as np import numpy.typing as npt @@ -28,7 +30,9 @@ def main() -> None: plt.close() -def _create_plot(ax: npt.NDArray, position: tuple[int, int], theta_max: int) -> None: +def _create_plot( + ax: npt.NDArray[typing.Any], position: tuple[int, int], theta_max: int +) -> None: """Create the plot.""" print(f"theta_max={theta_max}") slepian = sleplet.slepian.SlepianPolarCap(L, np.deg2rad(theta_max)) diff --git a/pyproject.toml b/pyproject.toml index 9adf45309..11902df1f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -58,6 +58,7 @@ optional-dependencies = {dev = [ "build", "mypy", "pre-commit", + "pytest", "ruff", "tox", "twine", @@ -102,7 +103,6 @@ paths.source = [ ] [tool.mypy] -disallow_any_generics = false disallow_subclassing_any = false disallow_untyped_decorators = false explicit_package_bases = true diff --git a/src/sleplet/_data/setup_pooch.py b/src/sleplet/_data/setup_pooch.py index 83899f696..86f96a028 100644 --- a/src/sleplet/_data/setup_pooch.py +++ b/src/sleplet/_data/setup_pooch.py @@ -16,7 +16,7 @@ _POOCH.load_registry_from_doi() -def find_on_pooch_then_local(filename: str) -> os.PathLike | None: +def find_on_pooch_then_local(filename: str) -> os.PathLike[str] | None: """Find a file on POOCH first and if not look in data folder.""" if filename in _POOCH.registry: msg = f"Found {filename} at https://doi.org/{_ZENODO_DATA_DOI}" diff --git a/src/sleplet/_mask_methods.py b/src/sleplet/_mask_methods.py index 341da9239..929dea839 100644 --- a/src/sleplet/_mask_methods.py +++ b/src/sleplet/_mask_methods.py @@ -1,5 +1,6 @@ import logging import os +import typing import numpy as np import numpy.typing as npt @@ -114,7 +115,7 @@ def create_default_region() -> "sleplet.slepian.region.Region": def create_mesh_region( - mesh_config: dict, + mesh_config: dict[str, typing.Any], vertices: npt.NDArray[np.float_], ) -> npt.NDArray[np.bool_]: """Create a boolean region for the given mesh.""" diff --git a/src/sleplet/_mesh_methods.py b/src/sleplet/_mesh_methods.py index e687a66c5..8ce4c3da9 100644 --- a/src/sleplet/_mesh_methods.py +++ b/src/sleplet/_mesh_methods.py @@ -1,5 +1,6 @@ import logging import pathlib +import typing import igl import numpy as np @@ -40,7 +41,7 @@ def average_functions_on_vertices_to_faces( def create_mesh_region( - mesh_config: dict, + mesh_config: dict[str, typing.Any], vertices: npt.NDArray[np.float_], ) -> npt.NDArray[np.bool_]: """Create a boolean region for the given mesh.""" @@ -54,7 +55,7 @@ def create_mesh_region( ) -def extract_mesh_config(mesh_name: str) -> dict: +def extract_mesh_config(mesh_name: str) -> dict[str, float | int | str]: """Read in the given mesh region settings file.""" with pathlib.Path.open( _data_path / f"meshes_regions_{mesh_name}.toml", @@ -110,7 +111,9 @@ def mesh_eigendecomposition( return eigenvalues, eigenvectors, number_basis_functions -def read_mesh(mesh_config: dict) -> tuple[npt.NDArray[np.float_], npt.NDArray[np.int_]]: +def read_mesh( + mesh_config: dict[str, float | int | str] +) -> tuple[npt.NDArray[np.float_], npt.NDArray[np.int_]]: """Read in the given mesh.""" vertices, faces = igl.read_triangle_mesh( str(_data_path / f"meshes_polygons_{mesh_config['FILENAME']}"), diff --git a/src/sleplet/_plotly_methods.py b/src/sleplet/_plotly_methods.py index 86b141fef..56817237c 100644 --- a/src/sleplet/_plotly_methods.py +++ b/src/sleplet/_plotly_methods.py @@ -1,3 +1,5 @@ +import typing + import plotly.graph_objs as go _axis = { @@ -30,7 +32,7 @@ def create_camera( # noqa: PLR0913 def create_layout( camera: go.layout.scene.Camera, *, - annotations: list[dict] | None = None, + annotations: list[dict[str, float | int]] | None = None, ) -> go.Layout: """Create the default plotly layout.""" return go.Layout( @@ -65,7 +67,7 @@ def create_colour_bar( bar_len: float = 0.94, bar_pos: float = 0.97, font_size: int = 38, -) -> dict: +) -> dict[str, typing.Any]: """Create the default plotly colour bar.""" return { "x": bar_pos, diff --git a/src/sleplet/_scripts/plotting_on_sphere.py b/src/sleplet/_scripts/plotting_on_sphere.py index c5ea79287..cc3f46228 100644 --- a/src/sleplet/_scripts/plotting_on_sphere.py +++ b/src/sleplet/_scripts/plotting_on_sphere.py @@ -302,7 +302,7 @@ def _translation_helper( alpha_pi_frac: float, beta_pi_frac: float, shannon: int | None, -) -> tuple[npt.NDArray[np.complex_ | np.float_], str, dict]: +) -> tuple[npt.NDArray[np.complex_ | np.float_], str, dict[str, float | int]]: """Perform the translation specific steps.""" msg = f"angles: (alpha, beta) = ({alpha_pi_frac}, {beta_pi_frac})" _logger.info(msg) diff --git a/src/sleplet/_slepian_arbitrary_methods.py b/src/sleplet/_slepian_arbitrary_methods.py index 86c05efc0..81a72b36e 100644 --- a/src/sleplet/_slepian_arbitrary_methods.py +++ b/src/sleplet/_slepian_arbitrary_methods.py @@ -10,14 +10,14 @@ def clean_evals_and_evecs( - eigendecomposition: tuple, + eigendecomposition: tuple[npt.NDArray[np.complex_], npt.NDArray[np.complex_]], ) -> tuple[npt.NDArray[np.float_], npt.NDArray[np.complex_]]: """Need eigenvalues and eigenvectors to be in a certain format.""" # access values - eigenvalues, eigenvectors = eigendecomposition + eigenvalues_complex, eigenvectors = eigendecomposition # eigenvalues should be real - eigenvalues = eigenvalues.real + eigenvalues = eigenvalues_complex.real # Sort eigenvalues and eigenvectors in descending order of eigenvalues idx = eigenvalues.argsort()[::-1] diff --git a/src/sleplet/_string_methods.py b/src/sleplet/_string_methods.py index 53754b7ca..4a50402bd 100644 --- a/src/sleplet/_string_methods.py +++ b/src/sleplet/_string_methods.py @@ -1,8 +1,13 @@ import fractions import re +import typing import numpy as np +if typing.TYPE_CHECKING: + import sleplet.functions.coefficients + import sleplet.meshes.mesh_coefficients + def _get_angle_num_dem(angle_fraction: float) -> tuple[int, int]: """Get numerator and denominator for a given decimal.""" @@ -84,7 +89,10 @@ def _convert_camel_case_to_snake_case(name: str) -> str: def convert_classes_list_to_snake_case( - classes: list, + classes: list[ + type["sleplet.functions.coefficients.Coefficients"] + | type["sleplet.meshes.mesh_coefficients.MeshCoefficients"] + ], *, word_to_remove: str = "", ) -> list[str]: diff --git a/src/sleplet/meshes/mesh_slepian.py b/src/sleplet/meshes/mesh_slepian.py index 0a1ab6fbd..9b69d11bc 100644 --- a/src/sleplet/meshes/mesh_slepian.py +++ b/src/sleplet/meshes/mesh_slepian.py @@ -161,7 +161,7 @@ def _integral(self: typing_extensions.Self, i: int, j: int) -> float: @staticmethod def _clean_evals_and_evecs( - eigendecomposition: tuple, + eigendecomposition: tuple[npt.NDArray[np.float_], npt.NDArray[np.float_]], ) -> tuple[npt.NDArray[np.float_], npt.NDArray[np.float_]]: """Need eigenvalues and eigenvectors to be in a certain format.""" # access values diff --git a/src/sleplet/plotting/_create_plot_sphere.py b/src/sleplet/plotting/_create_plot_sphere.py index 859692cf8..24aa697c0 100644 --- a/src/sleplet/plotting/_create_plot_sphere.py +++ b/src/sleplet/plotting/_create_plot_sphere.py @@ -33,7 +33,7 @@ class PlotSphere: _: dataclasses.KW_ONLY amplitude: float | None = None """Whether to customise the amplitude range of the colour bar.""" - annotations: list[dict] = pydantic.Field(default_factory=list) + annotations: list[dict[str, float | int]] = pydantic.Field(default_factory=list) """Whether to display any annotations on the surface plot or not.""" normalise: bool = True """Whether to normalise the plot or not.""" diff --git a/src/sleplet/slepian/slepian_limit_lat_lon.py b/src/sleplet/slepian/slepian_limit_lat_lon.py index 84903f8c4..b54652734 100644 --- a/src/sleplet/slepian/slepian_limit_lat_lon.py +++ b/src/sleplet/slepian/slepian_limit_lat_lon.py @@ -205,14 +205,14 @@ def _slepian_matrix( @staticmethod def _clean_evals_and_evecs( - eigendecomposition: tuple, + eigendecomposition: tuple[npt.NDArray[np.complex_], npt.NDArray[np.complex_]], ) -> tuple[npt.NDArray[np.float_], npt.NDArray[np.complex_]]: """Need eigenvalues and eigenvectors to be in a certain format.""" # access values - eigenvalues, eigenvectors = eigendecomposition + eigenvalues_complex, eigenvectors = eigendecomposition # eigenvalues should be real - eigenvalues = eigenvalues.real + eigenvalues = eigenvalues_complex.real # Sort eigenvalues and eigenvectors in descending order of eigenvalues idx = eigenvalues.argsort()[::-1]