Skip to content

Commit 927603f

Browse files
authored
Enforce type hinting and remove redundant numpydoc type annotations (#717)
1 parent 9ec062a commit 927603f

36 files changed

Lines changed: 358 additions & 340 deletions

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,7 @@ DEP004 = ["pytest", "scipy", "bs4"]
7979
fix = true
8080

8181
[tool.ruff.lint]
82-
select = ["I", "E4", "E7", "E9", "F", "UP"]
82+
select = ["I", "E4", "E7", "E9", "F", "UP", "ANN"]
83+
84+
[tool.ruff.lint.per-file-ignores]
85+
"test/**" = ["ANN"]

pyrenew/arrayutils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class PeriodicProcessSample(NamedTuple):
1414
1515
Attributes
1616
----------
17-
value : ArrayLike
17+
value
1818
The sampled quantity.
1919
"""
2020

2121
value: ArrayLike | None = None
2222

23-
def __repr__(self):
23+
def __repr__(self) -> str:
2424
return f"PeriodicProcessSample(value={self})"
2525

2626

@@ -72,7 +72,7 @@ def repeat_until_n(
7272
period_size: int,
7373
n_timepoints: int,
7474
offset: int = 0,
75-
):
75+
) -> ArrayLike:
7676
"""
7777
Repeat each entry in `data` a given number of times (`period_size`)
7878
until an array of length `n_timepoints` has been produced.

pyrenew/convolve.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,12 @@ def compute_prop_already_reported(
258258
259259
Parameters
260260
----------
261-
reporting_delay_pmf : ArrayLike
261+
reporting_delay_pmf
262262
PMF of reporting delays. The i-th entry is the probability that
263263
an event is reported with a delay of i time units.
264-
n_timepoints : int
264+
n_timepoints
265265
Number of timepoints in the output array.
266-
right_truncation_offset : int
266+
right_truncation_offset
267267
Number of additional timepoints beyond the last observation
268268
for which reports could still arrive. An offset of 0 means
269269
the last timepoint has only had time for delay-0 reports.

pyrenew/datasets/hospital_admissions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ def load_hospital_data_for_state(
2121
2222
Parameters
2323
----------
24-
state_abbr : str
24+
state_abbr
2525
State abbreviation (e.g., "CA"). Default is "CA".
26-
filename : str
26+
filename
2727
CSV filename. Default is "2023-11-06.csv".
2828
2929
Returns

pyrenew/datasets/wastewater_nwss.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ def load_wastewater_data_for_state(
2121
2222
Parameters
2323
----------
24-
state_abbr : str
24+
state_abbr
2525
State abbreviation (e.g., "CA"). Default is "CA".
26-
filename : str
26+
filename
2727
CSV filename. Default is "fake_nwss.csv".
2828
2929
Returns

pyrenew/deterministic/deterministic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ def validate(value: ArrayLike) -> None:
6868

6969
def sample(
7070
self,
71-
record=False,
72-
**kwargs,
71+
record: bool = False,
72+
**kwargs: object,
7373
) -> ArrayLike:
7474
"""
7575
Retrieve the value of the deterministic Rv

pyrenew/deterministic/deterministicpmf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def validate(value: ArrayLike) -> None:
7272

7373
def sample(
7474
self,
75-
**kwargs,
75+
**kwargs: object,
7676
) -> ArrayLike:
7777
"""
7878
Retrieves the deterministic PMF

pyrenew/deterministic/nullrv.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def validate() -> None:
3636

3737
def sample(
3838
self,
39-
**kwargs,
39+
**kwargs: object,
4040
) -> None:
4141
"""Retrieve the value of the Null (None)
4242
@@ -83,7 +83,7 @@ def sample(
8383
self,
8484
mu: ArrayLike,
8585
obs: ArrayLike | None = None,
86-
**kwargs,
86+
**kwargs: object,
8787
) -> None:
8888
"""
8989
Retrieve the value of the Null (None)

pyrenew/distributions/censorednormal.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import jax.numpy as jnp
55
import numpyro
66
import numpyro.util
7+
from jax.typing import ArrayLike
78
from numpyro.distributions import constraints
89
from numpyro.distributions.util import promote_shapes, validate_sample
910

@@ -27,12 +28,12 @@ class CensoredNormal(numpyro.distributions.Distribution):
2728

2829
def __init__(
2930
self,
30-
loc=0,
31-
scale=1,
32-
lower_limit=-jnp.inf,
33-
upper_limit=jnp.inf,
34-
validate_args=None,
35-
):
31+
loc: ArrayLike = 0,
32+
scale: ArrayLike = 1,
33+
lower_limit: float = -jnp.inf,
34+
upper_limit: float = jnp.inf,
35+
validate_args: bool | None = None,
36+
) -> None:
3637
"""
3738
Default constructor
3839
@@ -69,10 +70,12 @@ def __init__(
6970
super().__init__(batch_shape=batch_shape, validate_args=validate_args)
7071

7172
@constraints.dependent_property(is_discrete=False, event_dim=0)
72-
def support(self): # numpydoc ignore=GL08
73+
def support(self) -> constraints.Constraint: # numpydoc ignore=GL08
7374
return self._support
7475

75-
def sample(self, key, sample_shape=()):
76+
def sample(
77+
self, key: jax.random.PRNGKey, sample_shape: tuple[int, ...] = ()
78+
) -> ArrayLike:
7679
"""
7780
Generates samples from the censored normal distribution.
7881
@@ -86,7 +89,7 @@ def sample(self, key, sample_shape=()):
8689
return jnp.clip(result, min=self.lower_limit, max=self.upper_limit)
8790

8891
@validate_sample
89-
def log_prob(self, value):
92+
def log_prob(self, value: ArrayLike) -> ArrayLike:
9093
"""
9194
Computes the log probability density of a given value(s) under
9295
the censored normal distribution.

pyrenew/latent/base.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ class LatentSample(NamedTuple):
2121
2222
Attributes
2323
----------
24-
aggregate : ArrayLike
24+
aggregate
2525
Total infections aggregated across all subpopulations.
2626
Shape: (n_total_days,)
27-
all_subpops : ArrayLike
27+
all_subpops
2828
Infections for all subpopulations.
2929
Shape: (n_total_days, n_subpops)
3030
"""
@@ -40,7 +40,7 @@ class PopulationStructure:
4040
4141
Attributes
4242
----------
43-
fractions : ArrayLike
43+
fractions
4444
Population fractions for all subpopulations.
4545
Shape: (n_subpops,)
4646
"""
@@ -78,9 +78,9 @@ class BaseLatentInfectionProcess(RandomVariable):
7878
7979
Parameters
8080
----------
81-
gen_int_rv : RandomVariable
81+
gen_int_rv
8282
Generation interval PMF
83-
n_initialization_points : int
83+
n_initialization_points
8484
Number of initialization days before day 0. Must be at least
8585
``len(gen_int_rv())`` to provide enough history for the renewal
8686
equation convolution.
@@ -108,11 +108,11 @@ def __init__(
108108
109109
Parameters
110110
----------
111-
name : str
111+
name
112112
A name for this random variable.
113-
gen_int_rv : RandomVariable
113+
gen_int_rv
114114
Generation interval PMF
115-
n_initialization_points : int
115+
n_initialization_points
116116
Number of initialization days before day 0. Must be at least
117117
``len(gen_int_rv())`` to provide enough history for the renewal
118118
equation convolution.
@@ -145,7 +145,7 @@ def _parse_and_validate_fractions(
145145
146146
Parameters
147147
----------
148-
subpop_fractions : ArrayLike
148+
subpop_fractions
149149
Population fractions for all subpopulations. Must be a 1D array
150150
with at least one element. Values must be non-negative and sum to 1.
151151
@@ -200,13 +200,13 @@ def _validate_output_shapes(
200200
201201
Parameters
202202
----------
203-
infections_aggregate : ArrayLike
203+
infections_aggregate
204204
Aggregate infections (sum across subpopulations)
205-
infections_all : ArrayLike
205+
infections_all
206206
All subpopulation infections
207-
n_total_days : int
207+
n_total_days
208208
Expected number of days (n_initialization_points + n_days_post_init)
209-
pop : PopulationStructure
209+
pop
210210
Population structure
211211
212212
Raises
@@ -241,7 +241,7 @@ def _validate_I0(I0: ArrayLike) -> None:
241241
242242
Parameters
243243
----------
244-
I0 : ArrayLike
244+
I0
245245
Initial infection prevalence (scalar or array)
246246
247247
Raises
@@ -310,16 +310,16 @@ def sample(
310310
n_days_post_init: int,
311311
*,
312312
subpop_fractions: ArrayLike = None,
313-
**kwargs,
313+
**kwargs: object,
314314
) -> LatentSample:
315315
"""
316316
Sample latent infections for all subpopulations.
317317
318318
Parameters
319319
----------
320-
n_days_post_init : int
320+
n_days_post_init
321321
Number of days to simulate after initialization period
322-
subpop_fractions : ArrayLike
322+
subpop_fractions
323323
Population fractions for all subpopulations.
324324
Shape: (n_subpops,). Must sum to 1.0.
325325
**kwargs

0 commit comments

Comments
 (0)