Skip to content

Commit

Permalink
Use pydantic.validate_arguments decorator to validate individual func…
Browse files Browse the repository at this point in the history
…tions (#377)
  • Loading branch information
andersy005 authored Oct 12, 2021
1 parent 976fc18 commit ad175fa
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
26 changes: 16 additions & 10 deletions intake_esm/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def _get_entries(self) -> typing.Dict[str, ESMDataSource]:
_ = self[key]
return self._entries

@pydantic.validate_arguments
def __getitem__(self, key: str) -> ESMDataSource:
"""
This method takes a key argument and return a data source
Expand Down Expand Up @@ -237,9 +238,10 @@ def __dir__(self):
def _ipython_key_completions_(self):
return self.__dir__()

@pydantic.validate_arguments
def search(
self, require_all_on: typing.Union[str, typing.List[str]] = None, **query
) -> 'esm_datastore':
self, require_all_on: typing.Union[str, typing.List[str]] = None, **query: typing.Any
):
"""Search for entries in the catalog.
Parameters
Expand Down Expand Up @@ -307,7 +309,13 @@ def search(
cat._requested_variables = requested_variables
return cat

def serialize(self, name: str, directory: str = None, catalog_type: str = 'dict') -> None:
@pydantic.validate_arguments
def serialize(
self,
name: pydantic.StrictStr,
directory: typing.Union[pydantic.DirectoryPath, pydantic.StrictStr] = None,
catalog_type: str = 'dict',
) -> None:
"""Serialize collection/catalog to corresponding json and csv files.
Parameters
Expand Down Expand Up @@ -369,14 +377,15 @@ def unique(self) -> pd.Series:

return self.esmcat.unique()

@pydantic.validate_arguments
def to_dataset_dict(
self,
xarray_open_kwargs: typing.Dict[str, typing.Any] = None,
xarray_combine_by_coords_kwargs: typing.Dict[str, typing.Any] = None,
preprocess: typing.Dict[str, typing.Any] = None,
storage_options: typing.Dict[str, typing.Any] = None,
progressbar: bool = None,
aggregate: bool = None,
preprocess: typing.Callable = None,
storage_options: typing.Dict[pydantic.StrictStr, typing.Any] = None,
progressbar: pydantic.StrictBool = None,
aggregate: pydantic.StrictBool = None,
**kwargs,
) -> typing.Dict[str, xr.Dataset]:
"""
Expand Down Expand Up @@ -476,9 +485,6 @@ def to_dataset_dict(
if progressbar is not None:
self.progressbar = progressbar

if preprocess is not None and not callable(preprocess):
raise ValueError('preprocess argument must be callable')

if self.progressbar:
print(
f"""\n--> The keys in the returned dictionary of datasets are constructed as follows:\n\t'{self.key_template}'"""
Expand Down
3 changes: 2 additions & 1 deletion intake_esm/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class ESMDataSource(DataSource):
name = 'esm_datasource'
partition_access = True

@pydantic.validate_arguments
def __init__(
self,
key: pydantic.StrictStr,
Expand All @@ -114,7 +115,7 @@ def __init__(
xarray_open_kwargs: typing.Dict[str, typing.Any] = None,
xarray_combine_by_coords_kwargs: typing.Dict[str, typing.Any] = None,
intake_kwargs: typing.Dict[str, typing.Any] = None,
) -> 'ESMDataSource':
):

intake_kwargs = intake_kwargs or {}
super().__init__(**intake_kwargs)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ netCDF4>=1.5.5
requests>=2.24.0
xarray>=0.19
zarr>=2.5
pydantic
pydantic>=1.8.2
3 changes: 2 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import intake
import pandas as pd
import pydantic
import pytest
import xarray as xr

Expand Down Expand Up @@ -257,5 +258,5 @@ def test_to_dataset_dict_cdf_zarr_kwargs_deprecation():

def test_to_dataset_dict_w_preprocess_error():
cat = intake.open_esm_datastore(cdf_col_sample_cmip5)
with pytest.raises(ValueError, match=r'preprocess argument must be callable'):
with pytest.raises(pydantic.ValidationError):
cat.to_dataset_dict(preprocess='foo')

0 comments on commit ad175fa

Please sign in to comment.