From 9eb436804cf903ca272d2282eae7d48015862c13 Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Sat, 7 Sep 2024 13:10:51 -0700 Subject: [PATCH] Make the first argument in DataTree.from_dict positional only The fact that it has the non-descriptive name "d" is a good indication that users should not be supplying it as a keyword argument. (Alternatively, we could make a more descriptive name, but I think positional only is also fine for now.) --- xarray/core/datatree.py | 1 + xarray/tests/test_datatree.py | 4 ++-- xarray/tests/test_datatree_mapping.py | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/xarray/core/datatree.py b/xarray/core/datatree.py index 3a3fb19daa4..3a5783cff39 100644 --- a/xarray/core/datatree.py +++ b/xarray/core/datatree.py @@ -1051,6 +1051,7 @@ def drop_nodes( def from_dict( cls, d: Mapping[str, Dataset | DataArray | DataTree | None], + /, name: str | None = None, ) -> DataTree: """ diff --git a/xarray/tests/test_datatree.py b/xarray/tests/test_datatree.py index 1a840dbb9e4..a443d4b0f20 100644 --- a/xarray/tests/test_datatree.py +++ b/xarray/tests/test_datatree.py @@ -1059,7 +1059,7 @@ def test_match(self): def test_filter(self): simpsons: DataTree = DataTree.from_dict( - d={ + { "/": xr.Dataset({"age": 83}), "/Herbert": xr.Dataset({"age": 40}), "/Homer": xr.Dataset({"age": 39}), @@ -1070,7 +1070,7 @@ def test_filter(self): name="Abe", ) expected = DataTree.from_dict( - d={ + { "/": xr.Dataset({"age": 83}), "/Herbert": xr.Dataset({"age": 40}), "/Homer": xr.Dataset({"age": 39}), diff --git a/xarray/tests/test_datatree_mapping.py b/xarray/tests/test_datatree_mapping.py index 62fada4ab4f..1d2595cd013 100644 --- a/xarray/tests/test_datatree_mapping.py +++ b/xarray/tests/test_datatree_mapping.py @@ -19,8 +19,8 @@ def test_not_a_tree(self): check_isomorphic("s", 1) # type: ignore[arg-type] def test_different_widths(self): - dt1 = DataTree.from_dict(d={"a": empty}) - dt2 = DataTree.from_dict(d={"b": empty, "c": empty}) + dt1 = DataTree.from_dict({"a": empty}) + dt2 = DataTree.from_dict({"b": empty, "c": empty}) expected_err_str = ( "Number of children on node '/' of the left object: 1\n" "Number of children on node '/' of the right object: 2" @@ -320,7 +320,7 @@ def weighted_mean(ds): def test_alter_inplace_forbidden(self): simpsons = DataTree.from_dict( - d={ + { "/": xr.Dataset({"age": 83}), "/Herbert": xr.Dataset({"age": 40}), "/Homer": xr.Dataset({"age": 39}),