Skip to content

Commit

Permalink
Merge pull request #850 from pangeo-data/new-mypy-conventions
Browse files Browse the repository at this point in the history
No longer allow implicit optional (mypy)
  • Loading branch information
Zeitsperre authored Jan 5, 2024
2 parents 0ccf4a4 + 098b3dc commit e11d976
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 50 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ repos:
exclude: 'asv_bench'
additional_dependencies: [
# Type stubs
types-python-dateutil,
types-pkg_resources,
types-PyYAML,
types-pkg_resources,
types-python-dateutil,
types-pytz,
typing-extensions==3.10.0.0,
typing-extensions,
# Dependencies that are typed
numpy,
]
Expand Down
90 changes: 49 additions & 41 deletions climpred/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,23 +980,24 @@ def _warn_if_chunked_along_init_member_time(self) -> None:
ndims.remove(d)
if len(ndims) > 0:
msg += (
f"\nConsider chunking embarassingly parallel dimensions such as "
f"\nConsider chunking embarrassingly parallel dimensions such as "
f"{ndims} automatically, i.e. "
f'`{name}.chunk({ndims[0]}="auto").verify(...).'
)
warnings.warn(msg)

def _bootstrap(
self,
metric: metricType = None,
comparison: comparisonType = None,
dim: dimType = None,
alignment: alignmentType = None,
reference: referenceType = None,
groupby: groupbyType = None,
iterations: int = None,
*,
metric: metricType,
comparison: comparisonType,
dim: Optional[dimType] = None,
alignment: Optional[alignmentType] = None,
reference: Optional[referenceType] = None,
groupby: Optional[groupbyType] = None,
iterations: Optional[int] = None,
sig: int = 95,
resample_dim: str = None,
resample_dim: Optional[str] = None,
**metric_kwargs: metric_kwargsType,
) -> xr.Dataset:
"""PredictionEnsemble.bootstrap() parent method.
Expand Down Expand Up @@ -1167,7 +1168,7 @@ def _bootstrap(
]
results_labels = ["verify skill", "low_ci", "high_ci"]

if reference != []:
if reference:
pvalue = _pvalue_from_distributions(
resampled_skills.drop_sel(skill="initialized"),
resampled_skills.sel(skill="initialized"),
Expand Down Expand Up @@ -1347,11 +1348,12 @@ def get_control(self) -> xr.Dataset:

def verify(
self,
metric: metricType = None,
comparison: comparisonType = None,
dim: dimType = None,
reference: referenceType = None,
groupby: groupbyType = None,
*,
metric: metricType,
comparison: comparisonType,
dim: Optional[dimType] = None,
reference: Optional[referenceType] = None,
groupby: Optional[groupbyType] = None,
**metric_kwargs: metric_kwargsType,
) -> xr.Dataset:
"""Verify initialized predictions against a configuration of its members.
Expand Down Expand Up @@ -1498,9 +1500,10 @@ def verify(

def _compute_uninitialized(
self,
metric: metricType = None,
comparison: comparisonType = None,
dim: dimType = None,
*,
metric: metricType,
comparison: comparisonType,
dim: Optional[dimType] = None,
**metric_kwargs: metric_kwargsType,
) -> xr.Dataset:
"""Verify the bootstrapped uninitialized run against itself.
Expand Down Expand Up @@ -1554,8 +1557,9 @@ def _compute_uninitialized(

def _compute_persistence(
self,
metric: metricType = None,
dim: dimType = None,
*,
metric: metricType,
dim: Optional[dimType] = None,
**metric_kwargs: metric_kwargsType,
):
"""Verify a simple persistence forecast of the control run against itself.
Expand Down Expand Up @@ -1630,9 +1634,10 @@ def _compute_persistence(

def _compute_climatology(
self,
metric: metricType = None,
comparison: comparisonType = None,
dim: dimType = None,
*,
metric: metricType,
comparison: comparisonType,
dim: Optional[dimType] = None,
**metric_kwargs: metric_kwargsType,
) -> xr.Dataset:
"""Verify a climatology forecast.
Expand Down Expand Up @@ -1681,11 +1686,12 @@ def _compute_climatology(

def bootstrap(
self,
metric: metricType = None,
comparison: comparisonType = None,
dim: dimType = None,
reference: referenceType = None,
groupby: groupbyType = None,
*,
metric: metricType,
comparison: comparisonType,
dim: Optional[dimType] = None,
reference: Optional[referenceType] = None,
groupby: Optional[groupbyType] = None,
iterations: Optional[int] = None,
sig: int = 95,
resample_dim: str = "member",
Expand Down Expand Up @@ -2102,12 +2108,13 @@ def plot_alignment(

def verify(
self,
metric: metricType = None,
comparison: comparisonType = None,
dim: dimType = None,
alignment: alignmentType = None,
reference: referenceType = None,
groupby: groupbyType = None,
*,
metric: metricType,
comparison: comparisonType,
dim: Optional[dimType] = None,
alignment: Optional[alignmentType] = None,
reference: Optional[referenceType] = None,
groupby: Optional[groupbyType] = None,
**metric_kwargs: metric_kwargsType,
) -> xr.Dataset:
"""Verify the initialized ensemble against observations.
Expand Down Expand Up @@ -2409,12 +2416,13 @@ def _verify(

def bootstrap(
self,
metric: metricType = None,
comparison: comparisonType = None,
dim: dimType = None,
alignment: alignmentType = None,
reference: referenceType = None,
groupby: groupbyType = None,
*,
alignment: alignmentType,
metric: metricType,
comparison: comparisonType,
dim: Optional[dimType] = None,
reference: Optional[referenceType] = None,
groupby: Optional[groupbyType] = None,
iterations: Optional[int] = None,
sig: int = 95,
resample_dim: str = "member",
Expand Down Expand Up @@ -2541,7 +2549,7 @@ def bootstrap(

def remove_bias(
self,
alignment: alignmentType = None,
alignment: alignmentType,
how: str = "additive_mean",
train_test_split: str = "unfair",
train_init: Optional[Union[xr.DataArray, slice]] = None,
Expand Down
8 changes: 7 additions & 1 deletion climpred/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ def _sanitize_to_list(dim: Union[str, Set, Tuple, List, None]) -> Optional[List[
return dim


def _get_metric_comparison_dim(initialized, metric, comparison, dim, kind):
def _get_metric_comparison_dim(
initialized,
metric,
comparison,
dim: Optional[Union[List[str], str]],
kind,
):
"""Return `metric`, `comparison` and `dim` for compute functions.
Args:
Expand Down
6 changes: 3 additions & 3 deletions climpred/preprocessing/shared.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable, Union
from typing import Any, Callable, Optional, Union

import numpy as np
import xarray as xr
Expand All @@ -10,10 +10,10 @@
def load_hindcast(
inits=range(1961, 1965, 1),
members=range(1, 3, 1),
preprocess: Callable = None,
preprocess: Optional[Callable] = None,
lead_offset: int = 1,
parallel: bool = True,
engine: str = None,
engine: Optional[str] = None,
get_path: Callable = get_path_mpi,
**get_path_kwargs: Any,
) -> Union[xr.DataArray, xr.Dataset]:
Expand Down
4 changes: 2 additions & 2 deletions climpred/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def assign_attrs(
dim=None,
**kwargs,
):
"""Write information about prediction skill into attrs.
r"""Write information about prediction skill into attrs.
Args:
skill (`xarray` object): prediction skill.
Expand All @@ -75,7 +75,7 @@ def assign_attrs(
metric (class) : metric used in comparing the forecast and verification data.
comparison (class): how to compare the forecast and verification data.
dim (str): Dimension over which metric was applied.
kwargs (dict): other information
\*\*kwargs : other information
Returns:
skill (`xarray` object): prediction skill with additional attrs.
Expand Down

0 comments on commit e11d976

Please # to comment.