Skip to content

Commit b8b3da6

Browse files
TomNicholasaijams
authored andcommitted
Type hints for combine functions (pydata#5519)
* Added test for combine_by_coords changes. * Modified test case to expect a dataset instead of a DataArray. Added converter to combine_by_coords to check for all DataArray case and convert to datasets. * Added tests to check combine_by_coords for exception with mixed DataArrays and dataset input and with empty list. * Formatting changes after running black * Added underscore to helper function to label as private. * Black formatting changes for whats-new doc file. * Removed imports in docstring that were automatically added by code styling tools to match the other docstrings. * Removed duplicate new item line in whats-new. * combine methods now accept unnamed DataArrays as input. * combine nested test checks nested lists of unnamed DataArrays. * Made combine_by_coords more readable. * Cosmetic changes to code style. * Removed extra test from merge with previous PR. * Updated test to use pytest.raises instead of raises_regex. * Added breaking-change entry to whats new page. * Added deprecation warning to combine_coords * Removed index monotonicity checking temporarily. * Removed duplicate entries from whats new page. * Removed TODO message * Added test for combine_nested. * Added check to combine methods to clarify parameter requirements. * Reassigned description of changes to bug fixes category. * Minor style changes. * Added blank line for style purposes. * added type hints * force CI Co-authored-by: Augustus Ijams <[email protected]>
1 parent 7a0ab8d commit b8b3da6

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

xarray/core/combine.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import itertools
22
import warnings
33
from collections import Counter
4+
from typing import Iterable, Sequence, Union
45

56
import pandas as pd
67

@@ -369,16 +370,23 @@ def _nested_combine(
369370
return combined
370371

371372

373+
# Define type for arbitrarily-nested list of lists recursively
374+
# Currently mypy cannot handle this but other linters can (https://stackoverflow.com/a/53845083/3154101)
375+
DATASET_HYPERCUBE = Union[Dataset, Iterable["DATASET_HYPERCUBE"]] # type: ignore
376+
377+
372378
def combine_nested(
373-
datasets,
374-
concat_dim,
375-
compat="no_conflicts",
376-
data_vars="all",
377-
coords="different",
378-
fill_value=dtypes.NA,
379-
join="outer",
380-
combine_attrs="drop",
381-
):
379+
datasets: DATASET_HYPERCUBE,
380+
concat_dim: Union[
381+
str, DataArray, None, Sequence[Union[str, "DataArray", pd.Index, None]]
382+
],
383+
compat: str = "no_conflicts",
384+
data_vars: str = "all",
385+
coords: str = "different",
386+
fill_value: object = dtypes.NA,
387+
join: str = "outer",
388+
combine_attrs: str = "drop",
389+
) -> Dataset:
382390
"""
383391
Explicitly combine an N-dimensional grid of datasets into one by using a
384392
succession of concat and merge operations along each dimension of the grid.
@@ -651,16 +659,17 @@ def _combine_single_variable_hypercube(
651659

652660
# TODO remove empty list default param after version 0.21, see PR4696
653661
def combine_by_coords(
654-
data_objects=[],
655-
compat="no_conflicts",
656-
data_vars="all",
657-
coords="different",
658-
fill_value=dtypes.NA,
659-
join="outer",
660-
combine_attrs="no_conflicts",
661-
datasets=None,
662-
):
662+
data_objects: Sequence[Union[Dataset, DataArray]] = [],
663+
compat: str = "no_conflicts",
664+
data_vars: str = "all",
665+
coords: str = "different",
666+
fill_value: object = dtypes.NA,
667+
join: str = "outer",
668+
combine_attrs: str = "no_conflicts",
669+
datasets: Sequence[Dataset] = None,
670+
) -> Union[Dataset, DataArray]:
663671
"""
672+
664673
Attempt to auto-magically combine the given datasets (or data arrays)
665674
into one by using dimension coordinates.
666675
@@ -755,7 +764,7 @@ def combine_by_coords(
755764
756765
Returns
757766
-------
758-
combined : xarray.Dataset
767+
combined : xarray.Dataset or xarray.DataArray
759768
760769
See also
761770
--------

0 commit comments

Comments
 (0)