Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-480: fix mypy failures #490

Merged
merged 3 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The failing ignore[return-value] checks were on numpy 2.2.0, and should be resolved now.



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
Comment on lines +413 to +414
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of zsrc's new type signature

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],
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If zeff can be an array, zsrc in add_window can be one too, and add_window passes zsrc to add_plane

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]:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • np.trapezoid can return a float
  • Division between np.float64 and np.float64 can result in np.double ("float-any") for some reason.

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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.trapezoid can return an array and I don't think they have enough overloads in place -- therefore zeff can be an array and the type signature of RadialWindow should reflect this

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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returns "float-any" so be explicit about the type

msg = "exactly one of grid step size or number of steps must be given"
raise ValueError(msg)

Expand Down