Skip to content

Commit

Permalink
Fix several warnings in the tests (#8184)
Browse files Browse the repository at this point in the history
* remove some warnings from removed Dask name in msg

* fix date_range closed > inclusive

* fix passing pd.MultiIndex to constructor
  • Loading branch information
headtr1ck authored Sep 15, 2023
1 parent d3a6b02 commit af1c709
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 93 deletions.
15 changes: 8 additions & 7 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -1966,7 +1966,7 @@ def test_auto_chunk(self) -> None:
assert v.chunks == original[k].chunks

@requires_dask
@pytest.mark.filterwarnings("ignore:The specified Dask chunks separate")
@pytest.mark.filterwarnings("ignore:The specified chunks separate:UserWarning")
def test_manual_chunk(self) -> None:
original = create_test_data().chunk({"dim1": 3, "dim2": 4, "dim3": 3})

Expand Down Expand Up @@ -3086,8 +3086,9 @@ def create_store(self):
def test_complex(self) -> None:
expected = Dataset({"x": ("y", np.ones(5) + 1j * np.ones(5))})
save_kwargs = {"invalid_netcdf": True}
with self.roundtrip(expected, save_kwargs=save_kwargs) as actual:
assert_equal(expected, actual)
with pytest.warns(UserWarning, match="You are writing invalid netcdf features"):
with self.roundtrip(expected, save_kwargs=save_kwargs) as actual:
assert_equal(expected, actual)

@pytest.mark.parametrize("invalid_netcdf", [None, False])
def test_complex_error(self, invalid_netcdf) -> None:
Expand All @@ -3101,14 +3102,14 @@ def test_complex_error(self, invalid_netcdf) -> None:
with self.roundtrip(expected, save_kwargs=save_kwargs) as actual:
assert_equal(expected, actual)

@pytest.mark.filterwarnings("ignore:You are writing invalid netcdf features")
def test_numpy_bool_(self) -> None:
# h5netcdf loads booleans as numpy.bool_, this type needs to be supported
# when writing invalid_netcdf datasets in order to support a roundtrip
expected = Dataset({"x": ("y", np.ones(5), {"numpy_bool": np.bool_(True)})})
save_kwargs = {"invalid_netcdf": True}
with self.roundtrip(expected, save_kwargs=save_kwargs) as actual:
assert_identical(expected, actual)
with pytest.warns(UserWarning, match="You are writing invalid netcdf features"):
with self.roundtrip(expected, save_kwargs=save_kwargs) as actual:
assert_identical(expected, actual)

def test_cross_engine_read_write_netcdf4(self) -> None:
# Drop dim3, because its labels include strings. These appear to be
Expand Down Expand Up @@ -5193,7 +5194,7 @@ def test_open_dataset_chunking_zarr(chunks, tmp_path: Path) -> None:
@pytest.mark.parametrize(
"chunks", ["auto", -1, {}, {"x": "auto"}, {"x": -1}, {"x": "auto", "y": -1}]
)
@pytest.mark.filterwarnings("ignore:The specified Dask chunks separate")
@pytest.mark.filterwarnings("ignore:The specified chunks separate")
def test_chunking_consintency(chunks, tmp_path: Path) -> None:
encoded_chunks: dict[str, Any] = {}
dask_arr = da.from_array(
Expand Down
67 changes: 47 additions & 20 deletions xarray/tests/test_cftime_offsets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from itertools import product
from typing import Callable, Literal

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -1215,7 +1216,7 @@ def test_cftime_range_name():


@pytest.mark.parametrize(
("start", "end", "periods", "freq", "closed"),
("start", "end", "periods", "freq", "inclusive"),
[
(None, None, 5, "A", None),
("2000", None, None, "A", None),
Expand All @@ -1226,9 +1227,22 @@ def test_cftime_range_name():
("2000", "2001", 5, "A", None),
],
)
def test_invalid_cftime_range_inputs(start, end, periods, freq, closed):
def test_invalid_cftime_range_inputs(
start: str | None,
end: str | None,
periods: int | None,
freq: str | None,
inclusive: Literal["up", None],
) -> None:
with pytest.raises(ValueError):
cftime_range(start, end, periods, freq, closed=closed)
cftime_range(start, end, periods, freq, inclusive=inclusive) # type: ignore[arg-type]


def test_invalid_cftime_arg() -> None:
with pytest.warns(
FutureWarning, match="Following pandas, the `closed` parameter is deprecated"
):
cftime_range("2000", "2001", None, "A", closed="left")


_CALENDAR_SPECIFIC_MONTH_END_TESTS = [
Expand All @@ -1246,7 +1260,9 @@ def test_invalid_cftime_range_inputs(start, end, periods, freq, closed):
_CALENDAR_SPECIFIC_MONTH_END_TESTS,
ids=_id_func,
)
def test_calendar_specific_month_end(freq, calendar, expected_month_day):
def test_calendar_specific_month_end(
freq: str, calendar: str, expected_month_day: list[tuple[int, int]]
) -> None:
year = 2000 # Use a leap-year to highlight calendar differences
result = cftime_range(
start="2000-02", end="2001", freq=freq, calendar=calendar
Expand All @@ -1273,26 +1289,28 @@ def test_calendar_specific_month_end(freq, calendar, expected_month_day):
("julian", "2001", "2002", 365),
],
)
def test_calendar_year_length(calendar, start, end, expected_number_of_days):
result = cftime_range(start, end, freq="D", closed="left", calendar=calendar)
def test_calendar_year_length(
calendar: str, start: str, end: str, expected_number_of_days: int
) -> None:
result = cftime_range(start, end, freq="D", inclusive="left", calendar=calendar)
assert len(result) == expected_number_of_days


@pytest.mark.parametrize("freq", ["A", "M", "D"])
def test_dayofweek_after_cftime_range(freq):
def test_dayofweek_after_cftime_range(freq: str) -> None:
result = cftime_range("2000-02-01", periods=3, freq=freq).dayofweek
expected = pd.date_range("2000-02-01", periods=3, freq=freq).dayofweek
np.testing.assert_array_equal(result, expected)


@pytest.mark.parametrize("freq", ["A", "M", "D"])
def test_dayofyear_after_cftime_range(freq):
def test_dayofyear_after_cftime_range(freq: str) -> None:
result = cftime_range("2000-02-01", periods=3, freq=freq).dayofyear
expected = pd.date_range("2000-02-01", periods=3, freq=freq).dayofyear
np.testing.assert_array_equal(result, expected)


def test_cftime_range_standard_calendar_refers_to_gregorian():
def test_cftime_range_standard_calendar_refers_to_gregorian() -> None:
from cftime import DatetimeGregorian

(result,) = cftime_range("2000", periods=1)
Expand All @@ -1310,15 +1328,17 @@ def test_cftime_range_standard_calendar_refers_to_gregorian():
("3400-01-01", "standard", None, CFTimeIndex),
],
)
def test_date_range(start, calendar, use_cftime, expected_type):
def test_date_range(
start: str, calendar: str, use_cftime: bool | None, expected_type
) -> None:
dr = date_range(
start, periods=14, freq="D", calendar=calendar, use_cftime=use_cftime
)

assert isinstance(dr, expected_type)


def test_date_range_errors():
def test_date_range_errors() -> None:
with pytest.raises(ValueError, match="Date range is invalid"):
date_range(
"1400-01-01", periods=1, freq="D", calendar="standard", use_cftime=False
Expand Down Expand Up @@ -1412,7 +1432,7 @@ def as_timedelta_not_implemented_error():


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

Expand All @@ -1421,22 +1441,29 @@ def test_cftime_or_date_range_closed_and_inclusive_error(function) -> None:


@pytest.mark.parametrize("function", [cftime_range, date_range])
def test_cftime_or_date_range_invalid_closed_value(function) -> None:
def test_cftime_or_date_range_invalid_inclusive_value(function: Callable) -> None:
if function == cftime_range and not has_cftime:
pytest.skip("requires cftime")

with pytest.raises(ValueError, match="Argument `closed` must be"):
function("2000", periods=3, closed="foo")
with pytest.raises(ValueError, match="nclusive"):
function("2000", periods=3, inclusive="foo")


@pytest.mark.parametrize("function", [cftime_range, date_range])
@pytest.mark.parametrize(
"function",
[
pytest.param(cftime_range, id="cftime", marks=requires_cftime),
pytest.param(date_range, id="date"),
],
)
@pytest.mark.parametrize(
("closed", "inclusive"), [(None, "both"), ("left", "left"), ("right", "right")]
)
def test_cftime_or_date_range_closed(function, closed, inclusive) -> None:
if function == cftime_range and not has_cftime:
pytest.skip("requires cftime")

def test_cftime_or_date_range_closed(
function: Callable,
closed: Literal["left", "right", None],
inclusive: Literal["left", "right", "both"],
) -> None:
with pytest.warns(FutureWarning, match="Following pandas"):
result_closed = function("2000-01-01", "2000-01-04", freq="D", closed=closed)
result_inclusive = function(
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_cftimeindex_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def test_closed_label_defaults(freq, expected) -> None:
@pytest.mark.parametrize(
"calendar", ["gregorian", "noleap", "all_leap", "360_day", "julian"]
)
def test_calendars(calendar) -> None:
def test_calendars(calendar: str) -> None:
# Limited testing for non-standard calendars
freq, closed, label, base = "8001T", None, None, 17
loffset = datetime.timedelta(hours=12)
Expand Down
11 changes: 7 additions & 4 deletions xarray/tests/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from xarray import DataArray, Dataset, Variable, concat
from xarray.core import dtypes, merge
from xarray.core.coordinates import Coordinates
from xarray.core.indexes import PandasIndex
from xarray.tests import (
InaccessibleArray,
Expand Down Expand Up @@ -909,8 +910,9 @@ def test_concat_dim_is_dataarray(self) -> None:
assert_identical(actual, expected)

def test_concat_multiindex(self) -> None:
x = pd.MultiIndex.from_product([[1, 2, 3], ["a", "b"]])
expected = Dataset(coords={"x": x})
midx = pd.MultiIndex.from_product([[1, 2, 3], ["a", "b"]])
midx_coords = Coordinates.from_pandas_multiindex(midx, "x")
expected = Dataset(coords=midx_coords)
actual = concat(
[expected.isel(x=slice(2)), expected.isel(x=slice(2, None))], "x"
)
Expand All @@ -920,8 +922,9 @@ def test_concat_multiindex(self) -> None:
def test_concat_along_new_dim_multiindex(self) -> None:
# see https://github.com/pydata/xarray/issues/6881
level_names = ["x_level_0", "x_level_1"]
x = pd.MultiIndex.from_product([[1, 2, 3], ["a", "b"]], names=level_names)
ds = Dataset(coords={"x": x})
midx = pd.MultiIndex.from_product([[1, 2, 3], ["a", "b"]], names=level_names)
midx_coords = Coordinates.from_pandas_multiindex(midx, "x")
ds = Dataset(coords=midx_coords)
concatenated = concat([ds], "new")
actual = list(concatenated.xindexes.get_all_coords("x"))
expected = ["x"] + level_names
Expand Down
Loading

0 comments on commit af1c709

Please sign in to comment.