From 9a19d11b0930a7895948cfde9879bdfb33ec7298 Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Sun, 8 Sep 2024 12:59:52 -0700 Subject: [PATCH] Make the first argument in DataTree.from_dict positional only (#9446) 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 cd0c647f491..458d0efa0c3 100644 --- a/xarray/core/datatree.py +++ b/xarray/core/datatree.py @@ -1046,6 +1046,7 @@ def drop_nodes( def from_dict( cls, d: Mapping[str, Dataset | DataTree | None], + /, name: str | None = None, ) -> DataTree: """ diff --git a/xarray/tests/test_datatree.py b/xarray/tests/test_datatree.py index 1c47726b470..3368dd1f19b 100644 --- a/xarray/tests/test_datatree.py +++ b/xarray/tests/test_datatree.py @@ -1072,7 +1072,7 @@ def test_match(self): def test_filter(self): simpsons = DataTree.from_dict( - d={ + { "/": xr.Dataset({"age": 83}), "/Herbert": xr.Dataset({"age": 40}), "/Homer": xr.Dataset({"age": 39}), @@ -1083,7 +1083,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}),