Skip to content
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

improve error when an object fails to serialize #1809

Merged
merged 4 commits into from
Jul 23, 2024

Conversation

braingram
Copy link
Contributor

@braingram braingram commented Jul 22, 2024

Description

asdf currently produces an unhelpful message for some objects that fail to serialize during asdf.write_to:

import asdf, collections

class MyDict(collections.UserDict):
    pass

asdf.AsdfFile({"d": MyDict({"a": 1})}).write_to("foo.asdf")

Produces:

...
yaml.representer.RepresenterError: ('cannot represent an object', {'a': 1})

This PR adds a new exception AsdfSerializationError that includes the object type in the error message when yaml emits a yaml.representer.RepresenterError (for backwards compatibility the new exception inherits from yaml.representer.RepresenterError).

Running the above example with this PR produces:

asdf.exceptions.AsdfSerializationError: ("Object of type[<class '__main__.MyDict'>] is not serializable by asdf. Please convert the object to a supported type or implement a Converter for this type to allow the tree to be serialized.", {'a': 1})

Here is a related issue where the above message would have been helpful: spacetelescope/roman_datamodels#361

Checklist:

  • pre-commit checks ran successfully
  • tests ran successfully
  • for a public change, a changelog entry was added
  • for a public change, documentation was updated
  • for any new features, unit tests were added

@braingram braingram marked this pull request as ready for review July 22, 2024 19:39
@braingram braingram requested a review from a team as a code owner July 22, 2024 19:39
Copy link
Member

@zacharyburnett zacharyburnett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self-documenting errors!

@braingram braingram merged commit 15a9f2a into asdf-format:main Jul 23, 2024
48 of 49 checks passed
@braingram braingram deleted the serialization_error branch July 23, 2024 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants