From 20aabc9f4bdc9745234a7e6904822ae193b73505 Mon Sep 17 00:00:00 2001 From: Saransh Chopra Date: Tue, 28 Jan 2025 15:36:30 +0000 Subject: [PATCH 1/2] gh-480: fix mypy failures --- glass/core/array.py | 4 ++-- glass/lensing.py | 8 ++++---- glass/observations.py | 2 +- glass/points.py | 8 ++++---- glass/shells.py | 12 ++++++------ 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/glass/core/array.py b/glass/core/array.py index 00a4e34f..f719e8a3 100644 --- a/glass/core/array.py +++ b/glass/core/array.py @@ -138,7 +138,7 @@ def trapezoid_product( npt.NDArray[np.float64], ], axis: int = -1, -) -> npt.NDArray[np.float64]: +) -> float | npt.NDArray[np.float64]: """ Trapezoidal rule for a product of functions. @@ -166,7 +166,7 @@ def trapezoid_product( y = np.interp(x, *f) for f_ in ff: y *= np.interp(x, *f_) - return np.trapezoid(y, x, axis=axis) # type: ignore[return-value] + return np.trapezoid(y, x, axis=axis) def cumulative_trapezoid( diff --git a/glass/lensing.py b/glass/lensing.py index 981c025f..fea90963 100644 --- a/glass/lensing.py +++ b/glass/lensing.py @@ -410,8 +410,8 @@ def __init__(self, cosmo: Cosmology) -> None: self.cosmo = cosmo # set up initial values of variables - self.z2: float = 0.0 - self.z3: float = 0.0 + self.z2: float | npt.NDArray[np.float64] = 0.0 + self.z3: float | npt.NDArray[np.float64] = 0.0 self.x3: float = 0.0 self.w3: float = 0.0 self.r23: float = 1.0 @@ -442,7 +442,7 @@ def add_window(self, delta: npt.NDArray[np.float64], w: RadialWindow) -> None: def add_plane( self, delta: npt.NDArray[np.float64], - zsrc: float, + zsrc: float | npt.NDArray[np.float64], wlens: float = 1.0, ) -> None: """ @@ -505,7 +505,7 @@ def add_plane( self.kappa3 += f * delta2 @property - def zsrc(self) -> float: + def zsrc(self) -> float | npt.NDArray[np.float64]: """The redshift of the current convergence plane.""" return self.z3 diff --git a/glass/observations.py b/glass/observations.py index 229099eb..4c78f741 100644 --- a/glass/observations.py +++ b/glass/observations.py @@ -125,7 +125,7 @@ def gaussian_nz( if norm is not None: nz *= norm - return nz # type: ignore[no-any-return] + return nz def smail_nz( diff --git a/glass/points.py b/glass/points.py index 1ecf1f34..b0cbf431 100644 --- a/glass/points.py +++ b/glass/points.py @@ -52,7 +52,7 @@ def effective_bias( z: npt.NDArray[np.float64], bz: npt.NDArray[np.float64], w: RadialWindow, -) -> npt.NDArray[np.float64]: +) -> float | npt.NDArray[np.double]: r""" Effective bias parameter from a redshift-dependent bias function. @@ -85,7 +85,7 @@ def effective_bias( """ norm = np.trapezoid(w.wa, w.za) - return trapezoid_product((z, bz), (w.za, w.wa)) / norm # type: ignore[return-value] + return trapezoid_product((z, bz), (w.za, w.wa)) / norm def linear_bias( @@ -107,7 +107,7 @@ def linear_bias( The density contrast after biasing. """ - return b * delta # type: ignore[return-value] + return b * delta def loglinear_bias( @@ -413,6 +413,6 @@ def position_weights( densities = densities / np.sum(densities, axis=0) # apply bias after normalisation if bias is not None: - densities = densities * bias # type: ignore[assignment] + densities = densities * bias # densities now contains the relative contribution with bias applied return densities diff --git a/glass/shells.py b/glass/shells.py index 4ee414d5..d303e864 100644 --- a/glass/shells.py +++ b/glass/shells.py @@ -176,7 +176,7 @@ class RadialWindow(typing.NamedTuple): za: npt.NDArray[np.float64] wa: npt.NDArray[np.float64] - zeff: float = 0 + zeff: float | npt.NDArray[np.float64] = 0 def tophat_windows( @@ -235,10 +235,10 @@ def tophat_windows( ws = [] for zmin, zmax in itertools.pairwise(zbins): n = max(round((zmax - zmin) / dz), 2) - z = np.linspace(zmin, zmax, n) - w = wht(z) # type: ignore[arg-type] + z = np.linspace(zmin, zmax, n, dtype=np.float64) + w = wht(z) zeff = np.trapezoid(w * z, z) / np.trapezoid(w, z) - ws.append(RadialWindow(z, w, zeff)) # type: ignore[arg-type] + ws.append(RadialWindow(z, w, zeff)) return ws @@ -410,7 +410,7 @@ def restrict( z_ = np.compress(np.greater(z, w.za[0]) & np.less(z, w.za[-1]), z) zr = np.union1d(w.za, z_) fr = ndinterp(zr, z, f, left=0.0, right=0.0) * ndinterp(zr, w.za, w.wa) - return zr, fr # type: ignore[return-value] + return zr, fr def partition( @@ -742,7 +742,7 @@ def _uniform_grid( if step is not None and num is None: return np.arange(start, np.nextafter(stop + step, stop), step) if step is None and num is not None: - return np.linspace(start, stop, num + 1) # type: ignore[return-value] + return np.linspace(start, stop, num + 1, dtype=np.float64) msg = "exactly one of grid step size or number of steps must be given" raise ValueError(msg) From 0beb992b3a3c3694d1f2d338308aca8385c116d7 Mon Sep 17 00:00:00 2001 From: Saransh Chopra Date: Tue, 28 Jan 2025 15:53:46 +0000 Subject: [PATCH 2/2] lower bound for numpy --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 365ded25..3395ed95 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -71,7 +71,7 @@ repos: - id: mypy files: ^glass/ additional_dependencies: - - numpy + - numpy>=2.2.2 - repo: https://github.com/kynan/nbstripout rev: 0.8.1 hooks: