-
Notifications
You must be signed in to change notification settings - Fork 41
DataTree.lineage should be renamed to .parents #286
DataTree.lineage should be renamed to .parents #286
Conversation
Thanks for this! I will give a proper review tomorrow!
Technically we should add the new method, alias the old method to point to the new, and raise a DeprecationWarning whenever the old method is used.
No, we'll just tick off that bullet point when this done. |
Okay, I only substituted I can get inspiration from other places where warnings (DeprecationWarning) are raised, and the xarray contributing guidelines also underline the importance of not breaking the API : Backwards Compatibility |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking good so far! Just one comment about iter_parents
.
I actually can't remember how we normally do this, but I think probably better to leave them in and only remove them from
It's great that you found this! And whilst broadly we should try to follow that, in datatree we do not need to be as strict about it as we are in xarray (see https://github.com/xarray-contrib/datatree#development-roadmap). |
So, I added back |
datatree/treenode.py
Outdated
@@ -236,25 +236,48 @@ def _post_attach_children(self: Tree, children: Mapping[str, Tree]) -> None: | |||
"""Method call after attaching `children`.""" | |||
pass | |||
|
|||
def iter_lineage(self: Tree) -> Iterator[Tree]: | |||
def iter_parents(self: Tree) -> Iterator[Tree]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I don't think I was clear. I think we should never add .iter_parents
, instead recommending the new .parents
as the replacement for both .lineage
and .iter_lineage
. So there should not be any method called iter_parents
.
@@ -95,7 +95,7 @@ def test_ancestors(self): | |||
michael = TreeNode(children={"Tony": tony}) | |||
vito = TreeNode(children={"Michael": michael}) | |||
assert tony.root is vito | |||
assert tony.lineage == (tony, michael, vito) | |||
assert tony.parents == (michael, vito) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A node's parents do not include the node itself, in opposition to lineage
.
Note:lineage
is still tested in test_lineage
@@ -236,26 +235,53 @@ def _post_attach_children(self: Tree, children: Mapping[str, Tree]) -> None: | |||
"""Method call after attaching `children`.""" | |||
pass | |||
|
|||
def iter_lineage(self: Tree) -> Iterator[Tree]: | |||
def _iter_parents(self: Tree) -> Iterator[Tree]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kept the private _iter_parents
method as it is useful to insulate the "low-level" logic (while + yield) from the parents
method that just have to consume the iterator into a tuple. I could not find a way of not keeping this intermediate private method that would have been more elegant
"Please use `parents` from now on.", | ||
DeprecationWarning, | ||
) | ||
return self.iter_lineage() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Call chain: lineage
-> iter_lineage
-> parents
@@ -643,12 +668,12 @@ def _path_to_ancestor(self, ancestor: NamedNode) -> NodePath: | |||
raise NotFoundInTreeError( | |||
"Cannot find relative path to ancestor because nodes do not lie within the same tree" | |||
) | |||
if ancestor.path not in list(a.path for a in self.ancestors): | |||
if ancestor.path not in list(a.path for a in (self, *self.parents)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if the project has some test coverage tool, but I'm pretty sure this change is actually useless, as the case of the node being compared has been handled previously in the if
branch of relative_to
. So iterating on the parents, not including the node itself, might be correct
path_upwards = "../" * generation_gap if generation_gap > 0 else "/" | ||
parents_paths = list(parent.path for parent in (self, *self.parents)) | ||
generation_gap = list(parents_paths).index(ancestor.path) | ||
path_upwards = "../" * generation_gap if generation_gap > 0 else "." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same, this path was causing me issues, but the else
branch might never be reached, for the same reason. However, using a dot
fixed some tests during development. It makes sense: the path from a node itself is not the root (/
) but .
as declared in existing test_relative_paths
:
datatree/datatree/tests/test_datatree.py
Line 98 in 66397e8
result = sue.relative_to(john) |
dcf24fd
to
2c0b3ea
Compare
I don't know why I keep having to re-approve the automated tests on this PR This is looking great @etienneschalk ! Tag me when you feel it's ready for another review! |
Hello @TomNicholas , It seems that the CI is not automatically triggered on push and required manual intervention from a maintainer. I assume that having passing CI checks is a pre-condition to make a new review from your side worth it. What actions can I do on my side to fix these tests? I see some are failed or cancelled. Locally, what I do currently before pushing is:
However, I don't:
|
Yeah... I'm trying to work out what's causing that but drawing a blank so far.
I think we have another issue caused by using the most recent version of xarray, see #270. |
Co-authored-by: Tom Nicholas <tom@cworthy.org>
2c0b3ea
to
6d1d787
Compare
Thanks so much @etienneschalk ! Sorry it took me a while to look at this ❤️ |
Open Points
lineage
anditer_lineage
be removed fromapi.rst
?Checklist
DataTree.lineage should be renamed to .parents
of Consistency between DataTree methods and pathlib.PurePath methods #283test_lineage
totest_parents
test_lineage
so that it tests that thelineage
andparents
properties are equivalenttest_iter_lineage
to verify thatiter_lineage
anditer_parents
yield the same resultspre-commit run --all-files
#type: ignore
commentapi.rst
lineage
anditer_lineage
removed (see open point (3))parents
anditer_parents
addeddocs/source/whats-new.rst