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

deprecate validation on AsdfFile.tree assignment, AsdfFile.__init__ and AsdfFile.resolve_references #1691

Merged

Conversation

braingram
Copy link
Contributor

@braingram braingram commented Nov 17, 2023

Description

This PR deprecates automatic validation on assignment to AsdfFile.tree. This long-undocumented feature (documentation added in asdf 3.0 #1599) can be replaced by a call to AsdfFile.validate after tree assignment (which is already necessary for tree assignments that don't replace the entire tree).

The eventual disabling of the deprecated feature will allow downstream code that currently uses the non-public _tree attribute (to avoid the validation on assignment) to be updated to use the public tree. Here are a few examples:

In addition to mentioning this change in the docs and changelog a deprecation warning is added which is triggered only on failed validation during AsdfFile.tree assignment. This was done to avoid raising a warning for every AsdfFile.tree assignment. As these warnings typically only show during test suite runs it will hopefully signal to any downstream developers that might be relying on the validation on AsdfFile.tree assignment. A survey of AsdfFile.tree assignment in all packages in our downstream testing suggests the deprecation and removal of this feature should only have the positive effect of removing many unnecessary validations (see below for an example).

In this example from roman_datamodels.datamodels._core:

    def to_asdf(self, init, *args, **kwargs):
        with validate.nuke_validation():
            asdffile = self.open_asdf(**kwargs)
            asdffile.tree = {"roman": self._instance}
            asdffile.write_to(init, *args, **kwargs)

While writing out this model to an ASDF file, the model is validated (the nuke_validation setting is off by default) first on assignment to asdffile.tree (due to the feature deprecated in this PR) and validated a second time during the call to asdffile.write_to.

The roman_datamodels downstream failures are due to the new warning (being turned into an error). The failing tests are mostly from the same code used in the example above. The code could be updated (in this case) to avoid the duplicate validation and pass all tests with the changes in this PR:

             asdffile["roman"] = self._instance
             asdffile.write_to(init, *args, **kwargs)

If this PR is approved a PR can be opened against roman_datamodels with the above change to allow those tests to pass both with and without this asdf PR. After the change is made in roman_datamodels and the downstream test passes here this PR can be merged.

A few tests in roman_datamodels also assume validation on AsdfFile.__init__. This branch has updates sufficient to allow all roman_datamodels tests to pass with the source branch for this PR: spacetelescope/roman_datamodels@main...braingram:roman_datamodels:duplicate_validation

Fixes #315

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 November 17, 2023 18:03
@braingram braingram requested a review from a team as a code owner November 17, 2023 18:03
asdf/asdf.py Outdated
Comment on lines 558 to 562
except ValidationError:
warnings.warn(
"Validation on tree assignment is deprecated. Please use AsdfFile.validate", AsdfDeprecationWarning
)
raise
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a nice compromise...

asdf/asdf.py Outdated
@@ -185,7 +185,8 @@ def __init__(
self._tree = tree.tree
self.find_references()
else:
self.tree = tree
self._tree = AsdfObject(tree)
self.validate()
Copy link
Contributor

Choose a reason for hiding this comment

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

You may have told me this on Friday and I just forgot, but why not also deprecate the validation here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great idea! I added a warning in: b8a80a5

This change triggered some more warnings (turned into errors) in roman_datamodels.

asdf/asdf.py Outdated
@@ -1215,7 +1222,8 @@ def resolve_references(self, **kwargs):
"""
# Set to the property self.tree so the resulting "complete"
# tree will be validated.
self.tree = reference.resolve_references(self._tree, self)
self._tree = reference.resolve_references(self._tree, self)
self.validate()
Copy link
Contributor

Choose a reason for hiding this comment

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

I also don't see the value of validating here, but maybe just a failure of imagination.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great idea! I deprecated this (and the unused kwargs) in: afe8f86

@braingram braingram force-pushed the deprecate_tree_assignment_validation branch from 77651c9 to 72cd8d5 Compare December 5, 2023 21:49
@braingram braingram changed the title deprecate validation on AsdfFile.tree assignment deprecate validation on AsdfFile.tree assignment, AsdfFile.__init__ and AsdfFile.resolve_references Dec 6, 2023
@braingram braingram force-pushed the deprecate_tree_assignment_validation branch 2 times, most recently from 4f83fd3 to dde00af Compare December 6, 2023 15:35
Comment on lines 106 to 107
(using `AsdfFile.write_to` or `AsdfFile.update`) and when constructing
a new `AsdfFile` object from a tree/dictionary (as in ``AsdfFile(tree)``).
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this bit need to change now that we've also removed validation when constructing a new AsdfFile?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes! Thanks for finding this. I removed this mention in 97a09a5

Copy link
Contributor

@eslavich eslavich left a comment

Choose a reason for hiding this comment

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

🚀

@braingram braingram force-pushed the deprecate_tree_assignment_validation branch from 39b5f09 to a7cdd3d Compare January 16, 2024 20:53
@braingram braingram force-pushed the deprecate_tree_assignment_validation branch from a7cdd3d to 7d87d78 Compare February 5, 2024 18:11
@braingram
Copy link
Contributor Author

The CI failure for stdatamodels is also seen in stdatamodels main and addressed in: spacetelescope/stdatamodels#260

@braingram braingram merged commit 270a883 into asdf-format:main Feb 9, 2024
47 of 49 checks passed
@braingram braingram deleted the deprecate_tree_assignment_validation branch February 9, 2024 16:18
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.

Optional one-time validation of ASDF file
2 participants