Skip to content

Commit

Permalink
gh-480: fix mypy failures (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saransh-cpp authored Feb 1, 2025
1 parent 0602d04 commit 17dfffd
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions glass/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions glass/lensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion glass/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions glass/points.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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
12 changes: 6 additions & 6 deletions glass/shells.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 17dfffd

Please sign in to comment.