-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow typing.NamedTuple to be serialized #534
Conversation
Hi there @vmarkovtsev 👋 - thanks for the pull request! I'm just a friendly 🤖 that checks for issues related to the changelog. I help make sure that this pull request is milestoned and labeled correctly. This is mainly intended for the maintainers, so if you are not a maintainer you can ignore this, and a maintainer will let you know if any action is required on your part 😃. Everything looks good from my point of view! 👍 If there are any issues with this message, please report them here. |
Hi @vmarkovtsev, thanks for the PR. I am fully in favor of supporting serialization of |
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.
Overall this looks good, modulo the requested changes and the philosophical considerations mentioned above. Ideally we could implement a tag for these cases, but I'm not sure it's possible.
Maybe @perrygreenfield has an opinion about whether it's okay to serialize these types without preserving true roundtrip behavior?
asdf/tests/test_yaml.py
Outdated
assert isinstance(asdf.tree['val'], list) | ||
run_tuple_test(tree, tmpdir) | ||
|
||
|
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.
There is extraneous whitespace on these two lines.
asdf/tests/test_yaml.py
Outdated
|
||
def test_named_tuple_typing(tmpdir): | ||
# Ensure that we are able to serialize a typing.NamedTuple. | ||
|
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.
There is extraneous whitespace on this line as well.
asdf/tests/test_yaml.py
Outdated
run_tuple_test(tree, tmpdir) | ||
|
||
|
||
def test_named_tuple_collections(tmpdir): |
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.
Can you please add a test showing that recursive serialization of namedtuple
members is handled correctly? For example, you could create a namedtuple
where one of the members is an np.ndarray
.
Signed-off-by: Vadim Markovtsev <[email protected]>
Added more tests and removed whitespaces. |
@drdavella ping |
@vmarkovtsev I'd like some input from @perrygreenfield before merging. I have some lingering concerns over whether this is okay since it does not preserve true round-trip behavior, although maybe it doesn't matter too much. Just for the sake of argument: what would prevent a user from performing this conversion explicitly before passing to ASDF? I realize it's probably less convenient and/or elegant, but it would put the burden of information loss on the end user rather than requiring ASDF to strip away information implicitly under the hood. |
Yep, I see the point. On the other side, the exact subtypes of ASDF already strips items with |
This seems like a design flaw to me, but there's not much we can do about it. Unfortunately it means we can't write a tag for these types, which would be the ideal solution.
Interesting point, although I think it's really a separate issue (e.g. #536 😉). |
I’ll take a look at this today (though I have github authentication issue due to getting a new phone…)
|
@vmarkovtsev maybe as a compromise for now we could add a top-level option like |
@@ -17,7 +17,7 @@ open_files_ignore = test.fits asdf.fits | |||
# Account for both the astropy test runner case and the native pytest case | |||
asdf_schema_root = asdf-standard/schemas asdf/schemas | |||
asdf_schema_skip_names = asdf-schema-1.0.0 draft-01 | |||
addopts = --doctest-rst | |||
#addopts = --doctest-rst |
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 believe this was changed by mistake.
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.
Yep
How about |
If we support that, why not implicit_dicts as well? I tend to agree with not requiring the warning. These mechanisms would be advertised as an easy way to save python info, but only in one direction (without the user reconstructing the objects outside of ASDF). |
Do we have an example in the wild when this code fails in if isinstance(tree, dict):
result = tree.__class__()
seen.add(id_tree)
for key, val in tree.items():
val = recurse(val)
if val is not None:
result[key] = val
seen.remove(id_tree) All dicts have |
Sorry to waffle, but here's one more proposal: perform the conversion by default, but with a warning, and add a top-level option to suppress the warnings like In this case I like the generality of "implicit conversion" vs "implicit list" or "implicit dict" since it lets us overlook the This also parallels existing top-level options |
I like this proposal. |
@vmarkovtsev cool. I can finish up this PR if you want, as long as I'm able to push to your branch. |
I sent you the push permission invitation just in case. |
Thanks @vmarkovtsev! |
This allows to serialize
typing.NamedTuple
-s and other crazy inheritors fromlist
andtuple
which do not preserve the original constructor signature.We catch
TypeError
in the constructor and use the auxiliary list.