Skip to content

Commit

Permalink
coding convention fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeitsperre committed Oct 2, 2024
1 parent 6ebaf29 commit 73cec4a
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 18 deletions.
3 changes: 1 addition & 2 deletions src/xsdba/_adjustment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

from __future__ import annotations

from collections.abc import Sequence
from collections.abc import Callable
from collections.abc import Callable, Sequence

import numpy as np
import xarray as xr
Expand Down
7 changes: 3 additions & 4 deletions src/xsdba/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

import datetime as pydt
import itertools
from collections.abc import Sequence
from collections.abc import Callable, Sequence
from inspect import _empty, signature
from typing import Any, NewType, TypeVar
from collections.abc import Callable

import cftime
import dask.array as dsk
Expand Down Expand Up @@ -268,7 +267,7 @@ def get_calendar(obj: Any, dim: str = "time") -> str:
The Climate and Forecasting (CF) calendar name.
Will always return "standard" instead of "gregorian", following CF conventions 1.9.
"""
if isinstance(obj, (xr.DataArray, xr.Dataset)):
if isinstance(obj, (xr.DataArray | xr.Dataset)):
return obj[dim].dt.calendar
elif isinstance(obj, xr.CFTimeIndex):
obj = obj.values[0]
Expand Down Expand Up @@ -555,7 +554,7 @@ def apply(
function may add a "_group_apply_reshape" attribute set to `True` on the variables that should be reduced and
these will be re-grouped by calling `da.groupby(self.name).first()`.
"""
if isinstance(da, (dict, xr.Dataset)):
if isinstance(da, (dict | xr.Dataset)):
grpd = self.group(main_only=main_only, **da)
dim_chunks = min( # Get smallest chunking to rechunk if the operation is non-grouping
[
Expand Down
6 changes: 3 additions & 3 deletions src/xsdba/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def get_calendar(obj: Any, dim: str = "time") -> str:
The Climate and Forecasting (CF) calendar name.
Will always return "standard" instead of "gregorian", following CF conventions 1.9.
"""
if isinstance(obj, (xr.DataArray, xr.Dataset)):
if isinstance(obj, (xr.DataArray | xr.Dataset)):
return obj[dim].dt.calendar
elif isinstance(obj, xr.CFTimeIndex):
obj = obj.values[0]
Expand Down Expand Up @@ -889,9 +889,9 @@ def time_bnds( # noqa: C901
So "2000-01-31 00:00:00" with a "3h" frequency, means a period going from "2000-01-31 00:00:00" to
"2000-01-31 02:59:59.999999".
"""
if isinstance(time, (xr.DataArray, xr.Dataset)):
if isinstance(time, (xr.DataArray | xr.Dataset)):
time = time.indexes[time.name]
elif isinstance(time, (DataArrayResample, DatasetResample)):
elif isinstance(time, (DataArrayResample | DatasetResample)):
for grouper in time.groupers:
if "time" in grouper.dims:
datetime = grouper.unique_coord.data
Expand Down
7 changes: 3 additions & 4 deletions src/xsdba/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
import string
import warnings
from ast import literal_eval
from collections.abc import Sequence
from collections.abc import Callable, Sequence
from fnmatch import fnmatch
from inspect import _empty, signature
from typing import Any
from collections.abc import Callable

import xarray as xr
from boltons.funcutils import wraps
Expand Down Expand Up @@ -470,7 +469,7 @@ def _call_and_add_history(*args, **kwargs):
else:
out = outs

if not isinstance(out, (xr.DataArray, xr.Dataset)):
if not isinstance(out, (xr.DataArray | xr.Dataset)):
raise TypeError(
f"Decorated `update_xsdba_history` received a non-xarray output from {func.__name__}."
)
Expand Down Expand Up @@ -525,7 +524,7 @@ def gen_call_string(
for name, val in chain:
if isinstance(val, xr.DataArray):
rep = val.name or "<array>"
elif isinstance(val, (int, float, str, bool)) or val is None:
elif isinstance(val, (int | float | str | bool)) or val is None:
rep = repr(val)
else:
rep = repr(val)
Expand Down
2 changes: 1 addition & 1 deletion src/xsdba/locales.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def load_locale(locdata: str | Path | dict[str, dict], locale: str):
locale : str
The locale name (IETF tag).
"""
if isinstance(locdata, (str, Path)):
if isinstance(locdata, (str | Path)):
filename = Path(locdata)
locdata = read_locale_file(filename)

Expand Down
2 changes: 1 addition & 1 deletion src/xsdba/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# XC remove: metadata locales, do we need them?
from __future__ import annotations

from inspect import signature
from collections.abc import Callable
from inspect import signature

from boltons.funcutils import wraps

Expand Down
7 changes: 4 additions & 3 deletions src/xsdba/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def pint2str(value: units.Quantity | units.Unit) -> str:
-----
If cf-xarray is installed, the units will be converted to cf units.
"""
if isinstance(value, (pint.Quantity, units.Quantity)):
if isinstance(value, (pint.Quantity | units.Quantity)):
value = value.units

# Issue originally introduced in https://github.com/hgrecco/pint/issues/1486
Expand Down Expand Up @@ -286,14 +286,15 @@ def ensure_delta(unit: str) -> str:
def extract_units(arg):
"""Extract units from a string, DataArray, or scalar."""
if not (
isinstance(arg, (str, xr.DataArray, pint.Unit, units.Unit)) or np.isscalar(arg)
isinstance(arg, (str | xr.DataArray | pint.Unit | units.Unit))
or np.isscalar(arg)
):
raise TypeError(
f"Argument must be a str, DataArray, or scalar. Got {type(arg)}"
)
elif isinstance(arg, xr.DataArray):
ustr = None if "units" not in arg.attrs else arg.attrs["units"]
elif isinstance(arg, (pint.Unit, units.Unit)):
elif isinstance(arg, (pint.Unit | units.Unit)):
ustr = pint2str(arg) # XC: from pint2str
elif isinstance(arg, str):
ustr = pint2str(str2pint(arg).units)
Expand Down

0 comments on commit 73cec4a

Please sign in to comment.