Skip to content

Commit beff73a

Browse files
committed
Add types
1 parent 64fa0d0 commit beff73a

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

xarray/coding/cftime_offsets.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
from datetime import datetime, timedelta
4646
from enum import Enum
4747
from functools import partial
48-
from typing import ClassVar
48+
from typing import TYPE_CHECKING, ClassVar
4949

5050
import numpy as np
5151
import pandas as pd
@@ -67,6 +67,10 @@
6767
cftime = None
6868

6969

70+
if TYPE_CHECKING:
71+
from xarray.core.types import InclusiveOptions, SideOptions
72+
73+
7074
def get_date_type(calendar, use_cftime=True):
7175
"""Return the cftime date type for a given calendar name."""
7276
if cftime is None:
@@ -903,8 +907,8 @@ def cftime_range(
903907
freq="D",
904908
normalize=False,
905909
name=None,
906-
closed=_NoDefault,
907-
inclusive=None,
910+
closed: _NoDefault | SideOptions = _NoDefault,
911+
inclusive: None | InclusiveOptions = None,
908912
calendar="standard",
909913
):
910914
"""Return a fixed frequency CFTimeIndex.
@@ -945,23 +949,22 @@ def cftime_range(
945949
Notes
946950
-----
947951
This function is an analog of ``pandas.date_range`` for use in generating
948-
sequences of ``cftime.datetime`` objects. It supports most of the features
949-
of ``pandas.date_range`` (e.g. specifying how the index is ``closed`` on
950-
either side, or whether or not to ``normalize`` the start and end bounds);
951-
however, there are some notable exceptions:
952-
952+
sequences of ``cftime.datetime`` objects. It supports most of the
953+
features of ``pandas.date_range`` (e.g. specifying how the index is
954+
``closed`` on either side, or whether or not to ``normalize`` the start and
955+
end bounds); however, there are some notable exceptions:
953956
- You cannot specify a ``tz`` (time zone) argument.
954957
- Start or end dates specified as partial-datetime strings must use the
955958
`ISO-8601 format <https://en.wikipedia.org/wiki/ISO_8601>`_.
956959
- It supports many, but not all, frequencies supported by
957960
``pandas.date_range``. For example it does not currently support any of
958961
the business-related or semi-monthly frequencies.
959962
- Compound sub-monthly frequencies are not supported, e.g. '1H1min', as
960-
these can easily be written in terms of the finest common resolution, e.g.
961-
'61min'.
963+
these can easily be written in terms of the finest common resolution,
964+
e.g. '61min'.
962965
963-
Valid simple frequency strings for use with ``cftime``-calendars include any
964-
multiples of the following.
966+
Valid simple frequency strings for use with ``cftime``-calendars include
967+
any multiples of the following.
965968
966969
+--------+--------------------------+
967970
| Alias | Description |
@@ -1140,8 +1143,8 @@ def date_range(
11401143
tz=None,
11411144
normalize=False,
11421145
name=None,
1143-
closed=_NoDefault,
1144-
inclusive=None,
1146+
closed: _NoDefault | SideOptions = _NoDefault,
1147+
inclusive: None | InclusiveOptions = None,
11451148
calendar="standard",
11461149
use_cftime=None,
11471150
):

xarray/core/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ def dtype(self) -> np.dtype:
172172

173173
CoarsenBoundaryOptions = Literal["exact", "trim", "pad"]
174174
SideOptions = Literal["left", "right"]
175+
InclusiveOptions = Literal["both", "neither", "left", "right"]
175176

176177
ScaleOptions = Literal["linear", "symlog", "log", "logit", None]
177178
HueStyleOptions = Literal["continuous", "discrete", None]

xarray/tests/test_cftime_offsets.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ def as_timedelta_not_implemented_error():
14111411

14121412

14131413
@pytest.mark.parametrize("function", [cftime_range, date_range])
1414-
def test_cftime_or_date_range_closed_and_inclusive_error(function):
1414+
def test_cftime_or_date_range_closed_and_inclusive_error(function) -> None:
14151415
if function == cftime_range and not has_cftime:
14161416
pytest.skip("requires cftime")
14171417

@@ -1420,7 +1420,7 @@ def test_cftime_or_date_range_closed_and_inclusive_error(function):
14201420

14211421

14221422
@pytest.mark.parametrize("function", [cftime_range, date_range])
1423-
def test_cftime_or_date_range_invalid_closed_value(function):
1423+
def test_cftime_or_date_range_invalid_closed_value(function) -> None:
14241424
if function == cftime_range and not has_cftime:
14251425
pytest.skip("requires cftime")
14261426

@@ -1432,7 +1432,7 @@ def test_cftime_or_date_range_invalid_closed_value(function):
14321432
@pytest.mark.parametrize(
14331433
("closed", "inclusive"), [(None, "both"), ("left", "left"), ("right", "right")]
14341434
)
1435-
def test_cftime_or_date_range_closed(function, closed, inclusive):
1435+
def test_cftime_or_date_range_closed(function, closed, inclusive) -> None:
14361436
if function == cftime_range and not has_cftime:
14371437
pytest.skip("requires cftime")
14381438

@@ -1445,7 +1445,7 @@ def test_cftime_or_date_range_closed(function, closed, inclusive):
14451445

14461446

14471447
@pytest.mark.parametrize("function", [cftime_range, date_range])
1448-
def test_cftime_or_date_range_inclusive_None(function):
1448+
def test_cftime_or_date_range_inclusive_None(function) -> None:
14491449
if function == cftime_range and not has_cftime:
14501450
pytest.skip("requires cftime")
14511451

0 commit comments

Comments
 (0)