This repository was archived by the owner on Oct 24, 2024. It is now read-only.
actually allow DataTree objects as values in from_dict#159
Merged
TomNicholas merged 5 commits intoxarray-contrib:mainfrom Dec 7, 2022
Merged
actually allow DataTree objects as values in from_dict#159TomNicholas merged 5 commits intoxarray-contrib:mainfrom
DataTree objects as values in from_dict#159TomNicholas merged 5 commits intoxarray-contrib:mainfrom
Conversation
TomNicholas
reviewed
Nov 9, 2022
Comment on lines
+793
to
+797
| if isinstance(data, cls): | ||
| new_node = data.copy() | ||
| new_node.orphan() | ||
| else: | ||
| new_node = cls(name=node_name, data=data) |
Member
There was a problem hiding this comment.
This is a neat and sensible solution!
Member
|
Thanks for catching this @keewis ! Looks like I forgot to write a unit test for this intended behaviour and so never implemented it 😅 I think your solution is neat though.
So we could just explicitly write out the copy method on |
4 tasks
flamingbear
pushed a commit
to flamingbear/rewritten-datatree
that referenced
this pull request
Jan 19, 2024
…trib/datatree#159 * allow passing `DataTree` objects as `dict` values * add a test verifying that DataTree objects are actually allowed * ignore mypy error with copied copy method * whatsnew Co-authored-by: Tom Nicholas <thomas.nicholas@columbia.edu>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The docstring of
DataTree.from_dictclaims that it allows havingDataTreeobjects as values, but both the type hints and the code disagree (the code fails becauseDataTree(DataTree())does not work).The implementation I chose in this PR is to special-case
DataTreeobjects to copy and orphan the node instead of passing it to theDataTreeconstructor.Apparently this does not please
mypybecause whileDataTreehas acopymethod, that is inherited fromDataset. Thecopyis only necessary becauseorphanworks in-place (but then again you'd probably needcopyto implement a side-effect freeorphan).pre-commit run --all-filesNew functions/methods are listed inapi.rstdocs/source/whats-new.rstFor reference, I'm currently working around this limitation using