Skip to content

Commit 0929ec4

Browse files
committed
Fix typing
1 parent ed14179 commit 0929ec4

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

xarray/coding/times.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections.abc import Callable, Hashable
66
from datetime import datetime, timedelta
77
from functools import partial
8-
from typing import TYPE_CHECKING, Union, cast
8+
from typing import TYPE_CHECKING, Literal, Union, cast
99

1010
import numpy as np
1111
import pandas as pd
@@ -25,6 +25,7 @@
2525
from xarray.core.common import contains_cftime_datetimes, is_np_datetime_like
2626
from xarray.core.duck_array_ops import array_all, asarray, ravel, reshape
2727
from xarray.core.formatting import first_n_items, format_timestamp, last_item
28+
from xarray.core.types import DatetimeUnitOptions
2829
from xarray.core.utils import attempt_import, emit_user_level_warning
2930
from xarray.core.variable import Variable
3031
from xarray.namedarray.parallelcompat import T_ChunkedArray, get_chunked_array_type
@@ -1507,6 +1508,7 @@ def decode(self, variable: Variable, name: T_Name = None) -> Variable:
15071508
dtype = pop_to(attrs, encoding, "dtype", name=name)
15081509
dtype = np.dtype(dtype)
15091510
resolution, _ = np.datetime_data(dtype)
1511+
resolution = cast(Literal["Y", "M"] | DatetimeUnitOptions, resolution)
15101512
if np.timedelta64(1, resolution) > np.timedelta64(1, "s"):
15111513
time_unit = cast(PDDatetimeUnitOptions, "s")
15121514
dtype = np.dtype("timedelta64[s]")

xarray/core/dataarray.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5372,7 +5372,7 @@ def differentiate(
53725372
self,
53735373
coord: Hashable,
53745374
edge_order: Literal[1, 2] = 1,
5375-
datetime_unit: DatetimeUnitOptions = None,
5375+
datetime_unit: DatetimeUnitOptions | None = None,
53765376
) -> Self:
53775377
"""Differentiate the array with the second order accurate central
53785378
differences.
@@ -5434,7 +5434,7 @@ def differentiate(
54345434
def integrate(
54355435
self,
54365436
coord: Hashable | Sequence[Hashable] = None,
5437-
datetime_unit: DatetimeUnitOptions = None,
5437+
datetime_unit: DatetimeUnitOptions | None = None,
54385438
) -> Self:
54395439
"""Integrate along the given coordinate using the trapezoidal rule.
54405440
@@ -5446,7 +5446,7 @@ def integrate(
54465446
----------
54475447
coord : Hashable, or sequence of Hashable
54485448
Coordinate(s) used for the integration.
5449-
datetime_unit : {'Y', 'M', 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns', \
5449+
datetime_unit : {'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns', \
54505450
'ps', 'fs', 'as', None}, optional
54515451
Specify the unit if a datetime coordinate is used.
54525452
@@ -5488,7 +5488,7 @@ def integrate(
54885488
def cumulative_integrate(
54895489
self,
54905490
coord: Hashable | Sequence[Hashable] = None,
5491-
datetime_unit: DatetimeUnitOptions = None,
5491+
datetime_unit: DatetimeUnitOptions | None = None,
54925492
) -> Self:
54935493
"""Integrate cumulatively along the given coordinate using the trapezoidal rule.
54945494
@@ -5503,7 +5503,7 @@ def cumulative_integrate(
55035503
----------
55045504
coord : Hashable, or sequence of Hashable
55055505
Coordinate(s) used for the integration.
5506-
datetime_unit : {'Y', 'M', 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns', \
5506+
datetime_unit : {'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns', \
55075507
'ps', 'fs', 'as', None}, optional
55085508
Specify the unit if a datetime coordinate is used.
55095509

xarray/core/dataset.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8247,7 +8247,7 @@ def differentiate(
82478247
The coordinate to be used to compute the gradient.
82488248
edge_order : {1, 2}, default: 1
82498249
N-th order accurate differences at the boundaries.
8250-
datetime_unit : None or {"Y", "M", "W", "D", "h", "m", "s", "ms", \
8250+
datetime_unit : None or {"W", "D", "h", "m", "s", "ms", \
82518251
"us", "ns", "ps", "fs", "as", None}, default: None
82528252
Unit to compute gradient. Only valid for datetime coordinate.
82538253
@@ -8303,7 +8303,7 @@ def differentiate(
83038303
def integrate(
83048304
self,
83058305
coord: Hashable | Sequence[Hashable],
8306-
datetime_unit: DatetimeUnitOptions = None,
8306+
datetime_unit: DatetimeUnitOptions | None = None,
83078307
) -> Self:
83088308
"""Integrate along the given coordinate using the trapezoidal rule.
83098309
@@ -8315,7 +8315,7 @@ def integrate(
83158315
----------
83168316
coord : hashable, or sequence of hashable
83178317
Coordinate(s) used for the integration.
8318-
datetime_unit : {'Y', 'M', 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns', \
8318+
datetime_unit : {'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns', \
83198319
'ps', 'fs', 'as', None}, optional
83208320
Specify the unit if datetime coordinate is used.
83218321
@@ -8423,7 +8423,7 @@ def _integrate_one(self, coord, datetime_unit=None, cumulative=False):
84238423
def cumulative_integrate(
84248424
self,
84258425
coord: Hashable | Sequence[Hashable],
8426-
datetime_unit: DatetimeUnitOptions = None,
8426+
datetime_unit: DatetimeUnitOptions | None = None,
84278427
) -> Self:
84288428
"""Integrate along the given coordinate using the trapezoidal rule.
84298429
@@ -8439,7 +8439,7 @@ def cumulative_integrate(
84398439
----------
84408440
coord : hashable, or sequence of hashable
84418441
Coordinate(s) used for the integration.
8442-
datetime_unit : {'Y', 'M', 'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns', \
8442+
datetime_unit : {'W', 'D', 'h', 'm', 's', 'ms', 'us', 'ns', \
84438443
'ps', 'fs', 'as', None}, optional
84448444
Specify the unit if datetime coordinate is used.
84458445

xarray/core/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def copy(
254254
InterpOptions = Union[Interp1dOptions, InterpolantOptions, InterpnOptions]
255255

256256
DatetimeUnitOptions = Literal[
257-
"Y", "M", "W", "D", "h", "m", "s", "ms", "us", "μs", "ns", "ps", "fs", "as", None
257+
"W", "D", "h", "m", "s", "ms", "us", "μs", "ns", "ps", "fs", "as"
258258
]
259259
NPDatetimeUnitOptions = Literal["D", "h", "m", "s", "ms", "us", "ns"]
260260
PDDatetimeUnitOptions = Literal["s", "ms", "us", "ns"]

0 commit comments

Comments
 (0)