Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typing to functions related to data_vars #8226

Merged
merged 8 commits into from
Sep 24, 2023
4 changes: 2 additions & 2 deletions xarray/core/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
create_default_index_implicit,
)
from xarray.core.merge import merge_coordinates_without_align, merge_coords
from xarray.core.types import Self, T_DataArray, T_Xarray
from xarray.core.types import DataVars, Self, T_DataArray, T_Xarray
from xarray.core.utils import (
Frozen,
ReprObject,
Expand Down Expand Up @@ -937,7 +937,7 @@ def assert_coordinate_consistent(obj: T_Xarray, coords: Mapping[Any, Variable])


def create_coords_with_default_indexes(
coords: Mapping[Any, Any], data_vars: Mapping[Any, Any] | None = None
coords: Mapping[Any, Any], data_vars: DataVars | None = None
) -> Coordinates:
"""Returns a Coordinates object from a mapping of coordinates (arbitrary objects).

Expand Down
24 changes: 16 additions & 8 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@
from numbers import Number
from operator import methodcaller
from os import PathLike
from typing import IO, TYPE_CHECKING, Any, Callable, Generic, Literal, cast, overload
from typing import (
IO,
TYPE_CHECKING,
Any,
Callable,
Generic,
Literal,
cast,
overload,
Illviljan marked this conversation as resolved.
Show resolved Hide resolved
)

import numpy as np

Expand Down Expand Up @@ -124,7 +133,7 @@
from xarray.backends.api import T_NetcdfEngine, T_NetcdfTypes
from xarray.core.dataarray import DataArray
from xarray.core.groupby import DatasetGroupBy
from xarray.core.merge import CoercibleMapping, CoercibleValue
from xarray.core.merge import CoercibleMapping, CoercibleValue, _MergeResult
from xarray.core.parallelcompat import ChunkManagerEntrypoint
from xarray.core.resample import DatasetResample
from xarray.core.rolling import DatasetCoarsen, DatasetRolling
Expand All @@ -133,6 +142,7 @@
CoarsenBoundaryOptions,
CombineAttrsOptions,
CompatOptions,
DataVars,
DatetimeLike,
DatetimeUnitOptions,
Dims,
Expand Down Expand Up @@ -404,7 +414,7 @@ def _initialize_feasible(lb, ub):
return param_defaults, bounds_defaults


def merge_data_and_coords(data_vars, coords):
def merge_data_and_coords(data_vars: DataVars, coords) -> _MergeResult:
"""Used in Dataset.__init__."""
if isinstance(coords, Coordinates):
coords = coords.copy()
Expand Down Expand Up @@ -666,7 +676,7 @@ def __init__(
self,
# could make a VariableArgs to use more generally, and refine these
# categories
data_vars: Mapping[Any, Any] | None = None,
data_vars: DataVars | None = None,
coords: Mapping[Any, Any] | None = None,
attrs: Mapping[Any, Any] | None = None,
) -> None:
Expand Down Expand Up @@ -1220,9 +1230,7 @@ def _overwrite_indexes(
else:
return replaced

def copy(
self, deep: bool = False, data: Mapping[Any, ArrayLike] | None = None
) -> Self:
def copy(self, deep: bool = False, data: DataVars | None = None) -> Self:
"""Returns a copy of this dataset.

If `deep=True`, a deep copy is made of each of the component variables.
Expand Down Expand Up @@ -1324,7 +1332,7 @@ def copy(
def _copy(
self,
deep: bool = False,
data: Mapping[Any, ArrayLike] | None = None,
headtr1ck marked this conversation as resolved.
Show resolved Hide resolved
data: DataVars | None = None,
memo: dict[int, Any] | None = None,
) -> Self:
if data is None:
Expand Down
3 changes: 3 additions & 0 deletions xarray/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ def copy(
T_Chunks = Union[int, dict[Any, Any], Literal["auto"], None]
T_NormalizedChunks = tuple[tuple[int, ...], ...]

DataVars = Mapping[Any, Any]
headtr1ck marked this conversation as resolved.
Show resolved Hide resolved


ErrorOptions = Literal["raise", "ignore"]
ErrorOptionsWithWarn = Literal["raise", "warn", "ignore"]

Expand Down
Loading