Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/jsignell>`_.
- 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 <https://github.com/kkollsga>`_.

Bug Fixes
~~~~~~~~~
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -407,6 +408,7 @@ fo = "fo"
iy = "iy"
vart = "vart"
ede = "ede"
WRITEABLE = "WRITEABLE"

# Organization/Institution names
Stichting = "Stichting"
Expand Down
5 changes: 5 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down
8 changes: 8 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)})
Expand Down