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

Remove duplicate validation, replace assumed validation on AsdfFile.__init__ with AsdfFile.validate #301

Merged
merged 3 commits into from
Feb 9, 2024

Conversation

braingram
Copy link
Collaborator

@braingram braingram commented Jan 8, 2024

This PR removes duplicate validation during to_asdf:

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

The assignment to tree triggers the first validation and the write_to triggers a second.

This PR also updates several uses of AsdfFile.__init__ to validate tree contents (replacing it with an explicit call to AsdfFile.validate).

These changes are to prepare roman_datamodels for an upcoming deprecation in asdf (see asdf-format/asdf#1691).

Regression tests running with no errors: https://plwishmaster.stsci.edu:8081/job/RT/job/Roman-Developers-Pull-Requests/515/

Checklist

Copy link

codecov bot commented Jan 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (ee11c53) 97.44% compared to head (8bfd39a) 97.50%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #301      +/-   ##
==========================================
+ Coverage   97.44%   97.50%   +0.06%     
==========================================
  Files          28       29       +1     
  Lines        2698     2726      +28     
==========================================
+ Hits         2629     2658      +29     
+ Misses         69       68       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@braingram braingram marked this pull request as ready for review January 15, 2024 16:12
@braingram braingram requested review from WilliamJamieson and a team as code owners January 15, 2024 16:12
Comment on lines +41 to +44
af = asdf.AsdfFile()
af["roman"] = self._instance
af.validate()
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems a bit verbose. Is there something wrong with:

self._asdf = asdf.AsdfFile({"roman": self._instance})
self._asdf.validate()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for taking a look and good question. Unfortunately, the code you proposed would validate the instance twice. Once during AsdfFile.__init__ and again during AsdfFile.validate. Also, the reason for this PR is that asdf is deprecating validation during AsdfFile.__init__. The code you proposed would result in a DeprecationWarning and test failure when the validation fails. The code proposed in this PR avoids the possibility of the DeprecationWarning by not providing the _instance during AsdfFile.__init__..

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is asdf deprecating passing a dictionary directly to the AsdfFile initializer? If so, then that is a huge change of behavior!

If it's simply raising DepreciationWarning because of the validation change then a more thoughtful change should be considered as initializing with a dictionary is an extremely common usage of AsdfFile.

I would request that a validate=True option should be added to the initializer which preserves the current behavior without deprecation. You can set it so it defaults to None and raises the warning of impending behavior changes in that case.

Copy link
Collaborator Author

@braingram braingram Jan 16, 2024

Choose a reason for hiding this comment

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

asdf is not deprecating initializing with a dictionary. However if that dictionary is invalid one will see a DeprecationWarning noting that validation on AsdfFile.__init__ is deprecated and a ValidationError. If the dictionary is valid no warning or error will be seen. This was done to allow test suites (like the one in roman_datamodels) to detect the change.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@WilliamJamieson I'm not sure I addressed your comments here. asdf is deprecating (for removal) validation on AsdfFile.__init__ to reduce the duplication of functionality in asdf. Adding a validate keyword that triggers validation overlaps with functionality that is already provided by AsdfFile.validate.

Copy link
Collaborator Author

@braingram braingram Feb 5, 2024

Choose a reason for hiding this comment

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

@WilliamJamieson does this PR look good to you? I'd like to resolve this so I can merge the PR in asdf adding the deprecation warning without breaking the CI here.

@braingram braingram force-pushed the duplicate_validation branch from 68355d0 to 5d9a8fc Compare January 16, 2024 17:52
Copy link
Collaborator

@PaulHuwe PaulHuwe left a comment

Choose a reason for hiding this comment

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

LGTM modulo William's comments.

@braingram braingram force-pushed the duplicate_validation branch from 5d9a8fc to 8ca2580 Compare January 22, 2024 21:10
@braingram braingram force-pushed the duplicate_validation branch 2 times, most recently from e79c44a to d99ddf7 Compare February 5, 2024 18:10
@WilliamJamieson WilliamJamieson enabled auto-merge (squash) February 9, 2024 15:05
@WilliamJamieson WilliamJamieson merged commit dae9b66 into spacetelescope:main Feb 9, 2024
16 checks passed
@braingram braingram deleted the duplicate_validation branch February 9, 2024 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants