Skip to content

Commit c15af39

Browse files
authored
gh-355: consistent use of typing and collections.abc (#356)
Fixes #355, simplifying #308, xref #350. Change all import of `typing`/`collections.abc` to `import <x>` rather than `from <x> import` syntax. This is useful because it is straightforward to see at a glance where the `import` is coming from. Further, we have both `numpy.random.Generator` and `collections.abc.Generator` in use - so it separates them. I've also moved everything out of the `TYPE_CHECKING` block, except from where `ruff` says we should.
1 parent 8ad8831 commit c15af39

File tree

8 files changed

+44
-53
lines changed

8 files changed

+44
-53
lines changed

glass/core/algorithm.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING
6-
75
import numpy as np
8-
9-
if TYPE_CHECKING:
10-
import numpy.typing as npt
6+
import numpy.typing as npt
117

128

139
def nnls(

glass/fields.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,30 @@
2626

2727
from __future__ import annotations
2828

29+
import collections.abc
30+
import typing
2931
import warnings
30-
from collections.abc import Generator, Iterable, Sequence
31-
from typing import Any, Callable, Optional, Union
3232

3333
import healpy as hp
3434
import numpy as np
3535
import numpy.typing as npt
3636
from gaussiancl import gaussiancl
3737

3838
# types
39-
Size = Optional[Union[int, tuple[int, ...]]]
40-
Iternorm = tuple[Optional[int], npt.NDArray, npt.NDArray]
41-
ClTransform = Union[str, Callable[[npt.NDArray], npt.NDArray]]
42-
Cls = Sequence[Union[npt.NDArray, Sequence[float]]]
39+
Size = typing.Optional[typing.Union[int, tuple[int, ...]]]
40+
Iternorm = tuple[typing.Optional[int], npt.NDArray, npt.NDArray]
41+
ClTransform = typing.Union[str, typing.Callable[[npt.NDArray], npt.NDArray]]
42+
Cls = collections.abc.Sequence[
43+
typing.Union[npt.NDArray, collections.abc.Sequence[float]]
44+
]
4345
Alms = npt.NDArray
4446

4547

4648
def iternorm(
4749
k: int,
48-
cov: Iterable[npt.NDArray],
50+
cov: collections.abc.Iterable[npt.NDArray],
4951
size: Size = None,
50-
) -> Generator[Iternorm, None, None]:
52+
) -> collections.abc.Generator[Iternorm, None, None]:
5153
"""Return the vector a and variance sigma^2 for iterative normal sampling."""
5254
n: tuple[int, ...]
5355
if size is None:
@@ -105,7 +107,9 @@ def iternorm(
105107
yield j, a, s
106108

107109

108-
def cls2cov(cls: Cls, nl: int, nf: int, nc: int) -> Generator[npt.NDArray, None, None]:
110+
def cls2cov(
111+
cls: Cls, nl: int, nf: int, nc: int
112+
) -> collections.abc.Generator[npt.NDArray, None, None]:
109113
"""Return array of cls as a covariance matrix for iterative sampling."""
110114
cov = np.zeros((nl, nc + 1))
111115
end = 0
@@ -134,7 +138,7 @@ def multalm(alm: Alms, bl: npt.NDArray, *, inplace: bool = False) -> Alms:
134138
return out
135139

136140

137-
def transform_cls(cls: Cls, tfm: ClTransform, pars: tuple[Any, ...] = ()) -> Cls:
141+
def transform_cls(cls: Cls, tfm: ClTransform, pars: tuple[typing.Any, ...] = ()) -> Cls:
138142
"""Transform Cls to Gaussian Cls."""
139143
gls = []
140144
for cl in cls:
@@ -212,7 +216,7 @@ def generate_gaussian(
212216
*,
213217
ncorr: int | None = None,
214218
rng: np.random.Generator | None = None,
215-
) -> Generator[npt.NDArray, None, None]:
219+
) -> collections.abc.Generator[npt.NDArray, None, None]:
216220
"""
217221
Sample Gaussian random fields from Cls iteratively.
218222
@@ -298,7 +302,7 @@ def generate_lognormal(
298302
*,
299303
ncorr: int | None = None,
300304
rng: np.random.Generator | None = None,
301-
) -> Generator[npt.NDArray, None, None]:
305+
) -> collections.abc.Generator[npt.NDArray, None, None]:
302306
"""Sample lognormal random fields from Gaussian Cls iteratively."""
303307
for i, m in enumerate(generate_gaussian(gls, nside, ncorr=ncorr, rng=rng)):
304308
# compute the variance of the auto-correlation

glass/galaxies.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,18 @@
1919

2020
from __future__ import annotations
2121

22+
import typing
2223
import warnings
23-
from typing import TYPE_CHECKING
24-
25-
if TYPE_CHECKING:
26-
import numpy.typing as npt
27-
28-
from glass.shells import RadialWindow
2924

3025
import healpix
3126
import numpy as np
27+
import numpy.typing as npt
3228

3329
from glass.core.array import broadcast_leading_axes, cumtrapz
3430

31+
if typing.TYPE_CHECKING:
32+
from glass.shells import RadialWindow
33+
3534

3635
def redshifts(
3736
n: int | npt.ArrayLike,

glass/lensing.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,14 @@
3131

3232
from __future__ import annotations
3333

34-
from typing import TYPE_CHECKING
34+
import typing
3535

3636
import healpy as hp
3737
import numpy as np
38+
import numpy.typing as npt
3839

39-
if TYPE_CHECKING:
40-
from collections.abc import Sequence
41-
42-
import numpy.typing as npt
40+
if typing.TYPE_CHECKING:
41+
import collections.abc
4342

4443
from cosmology import Cosmology
4544

@@ -363,7 +362,7 @@ def wlens(self) -> float:
363362

364363

365364
def multi_plane_matrix(
366-
shells: Sequence[RadialWindow],
365+
shells: collections.abc.Sequence[RadialWindow],
367366
cosmo: Cosmology,
368367
) -> npt.ArrayLike:
369368
"""Compute the matrix of lensing contributions from each shell."""
@@ -377,7 +376,7 @@ def multi_plane_matrix(
377376

378377
def multi_plane_weights(
379378
weights: npt.ArrayLike,
380-
shells: Sequence[RadialWindow],
379+
shells: collections.abc.Sequence[RadialWindow],
381380
cosmo: Cosmology,
382381
) -> npt.ArrayLike:
383382
"""

glass/observations.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,13 @@
2929
from __future__ import annotations
3030

3131
import math
32-
from typing import TYPE_CHECKING
3332

3433
import healpy as hp
3534
import numpy as np
35+
import numpy.typing as npt
3636

3737
from glass.core.array import cumtrapz
3838

39-
if TYPE_CHECKING:
40-
import numpy.typing as npt
41-
4239

4340
def vmap_galactic_ecliptic(
4441
nside: int,

glass/shapes.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@
2424

2525
from __future__ import annotations
2626

27-
from typing import TYPE_CHECKING
28-
2927
import numpy as np
30-
31-
if TYPE_CHECKING:
32-
import numpy.typing as npt
28+
import numpy.typing as npt
3329

3430

3531
def triaxial_axis_ratio(zeta, xi, size=None, *, rng=None):

glass/shells.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,21 @@
4444

4545
from __future__ import annotations
4646

47+
import collections.abc
48+
import typing
4749
import warnings
48-
from collections.abc import Sequence
49-
from typing import TYPE_CHECKING, Callable, NamedTuple, Union
5050

5151
import numpy as np
5252
import numpy.typing as npt
5353

5454
from glass.core.array import ndinterp
5555

56-
if TYPE_CHECKING:
56+
if typing.TYPE_CHECKING:
5757
from cosmology import Cosmology
5858

5959
# types
60-
ArrayLike1D = Union[Sequence[float], npt.NDArray]
61-
WeightFunc = Callable[[ArrayLike1D], npt.NDArray]
60+
ArrayLike1D = typing.Union[collections.abc.Sequence[float], npt.NDArray]
61+
WeightFunc = typing.Callable[[ArrayLike1D], npt.NDArray]
6262

6363

6464
def distance_weight(z: npt.ArrayLike, cosmo: Cosmology) -> npt.NDArray:
@@ -76,7 +76,7 @@ def density_weight(z: npt.ArrayLike, cosmo: Cosmology) -> npt.NDArray:
7676
return cosmo.rho_m_z(z) * cosmo.xm(z) ** 2 / cosmo.ef(z)
7777

7878

79-
class RadialWindow(NamedTuple):
79+
class RadialWindow(typing.NamedTuple):
8080
"""
8181
A radial window, defined by a window function.
8282
@@ -120,8 +120,8 @@ class RadialWindow(NamedTuple):
120120
121121
"""
122122

123-
za: Sequence[float]
124-
wa: Sequence[float]
123+
za: collections.abc.Sequence[float]
124+
wa: collections.abc.Sequence[float]
125125
zeff: float
126126

127127

@@ -338,7 +338,7 @@ def restrict(
338338
def partition(
339339
z: npt.ArrayLike,
340340
fz: npt.ArrayLike,
341-
shells: Sequence[RadialWindow],
341+
shells: collections.abc.Sequence[RadialWindow],
342342
*,
343343
method: str = "nnls",
344344
) -> npt.ArrayLike:
@@ -449,7 +449,7 @@ def partition(
449449
def partition_lstsq(
450450
z: npt.ArrayLike,
451451
fz: npt.ArrayLike,
452-
shells: Sequence[RadialWindow],
452+
shells: collections.abc.Sequence[RadialWindow],
453453
*,
454454
sumtol: float = 0.01,
455455
) -> npt.ArrayLike:
@@ -495,7 +495,7 @@ def partition_lstsq(
495495
def partition_nnls(
496496
z: npt.ArrayLike,
497497
fz: npt.ArrayLike,
498-
shells: Sequence[RadialWindow],
498+
shells: collections.abc.Sequence[RadialWindow],
499499
*,
500500
sumtol: float = 0.01,
501501
) -> npt.ArrayLike:
@@ -556,7 +556,7 @@ def partition_nnls(
556556
def partition_restrict(
557557
z: npt.ArrayLike,
558558
fz: npt.ArrayLike,
559-
shells: Sequence[RadialWindow],
559+
shells: collections.abc.Sequence[RadialWindow],
560560
) -> npt.ArrayLike:
561561
"""Partition by restriction and integration."""
562562
part = np.empty((len(shells),) + np.shape(fz)[:-1])
@@ -594,7 +594,7 @@ def distance_grid(cosmo, zmin, zmax, *, dx=None, num=None):
594594
def combine(
595595
z: npt.ArrayLike,
596596
weights: npt.ArrayLike,
597-
shells: Sequence[RadialWindow],
597+
shells: collections.abc.Sequence[RadialWindow],
598598
) -> npt.ArrayLike:
599599
r"""
600600
Evaluate a linear combination of window functions.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Issues = "https://github.com/glass-dev/glass/issues"
6969

7070
[tool.coverage]
7171
report = {exclude_also = [
72-
"if TYPE_CHECKING:",
72+
"if typing.TYPE_CHECKING:",
7373
], omit = [
7474
"glass/_version.py",
7575
], skip_covered = true, sort = "cover"}

0 commit comments

Comments
 (0)