diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e19b55ed562..217c60e7098 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -24,7 +24,7 @@ repos: - id: rst-inline-touching-normal - id: text-unicode-replacement-char - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.15.4 + rev: v0.15.9 hooks: - id: ruff-check args: ["--fix", "--show-fixes"] @@ -42,7 +42,7 @@ repos: - id: prettier args: ["--cache-location=.prettier_cache/cache"] - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.19.1 + rev: v1.20.0 hooks: - id: mypy # Copied from setup.cfg @@ -76,7 +76,7 @@ repos: - id: validate-pyproject additional_dependencies: ["validate-pyproject-schema-store[all]"] - repo: https://github.com/adhtruong/mirrors-typos - rev: v1.44.0 + rev: v1.45.0 hooks: - id: typos - repo: https://github.com/zizmorcore/zizmor-pre-commit diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 15cfb8e5d81..1116940a4cc 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -186,6 +186,11 @@ Breaking Changes not intended to be visible to end-users so this version of xarray switches to using ``FutureWarning`` everywhere (:pull:`11112`). By `Julia Signell `_. +- Passing a :py:class:`Dataset` as ``data_vars`` to the :py:class:`Dataset` + constructor now raises :py:class:`TypeError`. This was never intended behavior + and silently dropped ``attrs``. Use :py:meth:`Dataset.copy` instead + (:issue:`11095`). + By `Kristian Kollsga `_. Bug Fixes ~~~~~~~~~ diff --git a/pyproject.toml b/pyproject.toml index a1feb10e8c1..640e4d3846c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -398,7 +398,8 @@ aso = "aso" # Technical terms nd = "nd" nin = "nin" -nclusive = "nclusive" # part of "inclusive" in error messages +nclusive = "nclusive" # part of "inclusive" in error messages +writeable = "writeable" # Variable names ba = "ba" @@ -407,6 +408,7 @@ fo = "fo" iy = "iy" vart = "vart" ede = "ede" +WRITEABLE = "WRITEABLE" # Organization/Institution names Stichting = "Stichting" diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 78a897b8891..c1e1b961f22 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -385,6 +385,11 @@ def __init__( ) -> None: if data_vars is None: data_vars = {} + if isinstance(data_vars, Dataset): + raise TypeError( + "Passing a Dataset as `data_vars` to the Dataset constructor is" + " not supported. Use `ds.copy()` to create a copy of a Dataset." + ) if coords is None: coords = {} diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 6be6c75dc5f..cad40e78821 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -494,6 +494,14 @@ def test_constructor(self) -> None: actual = Dataset({"z": expected["z"]}) assert_identical(expected, actual) + def test_constructor_dataset_as_data_vars_raises(self) -> None: + ds = Dataset({"x": ("x", [1, 2, 3])}, attrs={"key": "value"}) + with pytest.raises( + TypeError, + match=r"Passing a Dataset as `data_vars`.*Use `ds\.copy\(\)`", + ): + Dataset(ds) + def test_constructor_1d(self) -> None: expected = Dataset({"x": (["x"], 5.0 + np.arange(5))}) actual = Dataset({"x": 5.0 + np.arange(5)})