Skip to content

Commit 31b5026

Browse files
kkollsgaclaude
authored andcommitted
Raise TypeError when Dataset passed as data_vars to Dataset constructor (pydata#11139)
Dataset(ds) silently dropped attrs because Dataset inherits from Mapping, so the constructor iterated over data variable keys while ignoring attrs. This was never intended behavior. Raise TypeError with a message directing users to ds.copy() instead. Closes GH11095 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 0bf6791 commit 31b5026

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

doc/whats-new.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ Breaking Changes
189189
not intended to be visible to end-users so this version of xarray
190190
switches to using ``FutureWarning`` everywhere (:pull:`11112`).
191191
By `Julia Signell <https://github.com/jsignell>`_.
192+
- Passing a :py:class:`Dataset` as ``data_vars`` to the :py:class:`Dataset`
193+
constructor now raises :py:class:`TypeError`. This was never intended behavior
194+
and silently dropped ``attrs``. Use :py:meth:`Dataset.copy` instead
195+
(:issue:`11095`).
196+
By `Kristian Kollsga <https://github.com/kkollsga>`_.
192197

193198
Bug Fixes
194199
~~~~~~~~~

xarray/core/dataset.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,11 @@ def __init__(
385385
) -> None:
386386
if data_vars is None:
387387
data_vars = {}
388+
if isinstance(data_vars, Dataset):
389+
raise TypeError(
390+
"Passing a Dataset as `data_vars` to the Dataset constructor is"
391+
" not supported. Use `ds.copy()` to create a copy of a Dataset."
392+
)
388393
if coords is None:
389394
coords = {}
390395

xarray/tests/test_dataset.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,14 @@ def test_constructor(self) -> None:
494494
actual = Dataset({"z": expected["z"]})
495495
assert_identical(expected, actual)
496496

497+
def test_constructor_dataset_as_data_vars_raises(self) -> None:
498+
ds = Dataset({"x": ("x", [1, 2, 3])}, attrs={"key": "value"})
499+
with pytest.raises(
500+
TypeError,
501+
match=r"Passing a Dataset as `data_vars`.*Use `ds\.copy\(\)`",
502+
):
503+
Dataset(ds)
504+
497505
def test_constructor_1d(self) -> None:
498506
expected = Dataset({"x": (["x"], 5.0 + np.arange(5))})
499507
actual = Dataset({"x": 5.0 + np.arange(5)})

0 commit comments

Comments
 (0)