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

Bugfix: Datamodels can be initialized with incorrect node. #200

Merged

Conversation

WilliamJamieson
Copy link
Contributor

@WilliamJamieson WilliamJamieson commented Jun 5, 2023

@nden noticed that she could initialize any Datamodel with any stnode, without any issues. This can lead to many unexpected issues, as the Datamodel objects are wrappers around the stnode. The stnode is what handles all the validation and serialization of the Datamodel not the Datamodel itself. Indeed, if one initializes a Datamodel with the wrong stnode then when the model is deserialized, one will not get the same datamodel back.

This PR fixes this issue by crosschecking the registered top-level stnode for each Datamodel against the stnode passed to it during construction, an error is raised if the stnode is not of the correct type.

Checklist

@WilliamJamieson WilliamJamieson requested a review from a team as a code owner June 5, 2023 21:45
This checks that it can be initiallized with the correct node, and that it cannot be
with any other node.
"""
img = create_node(node)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test is rather slow because the testing.factories constructors can be extremely slow due to lots of random number generation. #193 can make this much more efficient because it provides a maker_util that can determine which node to make, that is only available to testing.factories at this time.

@@ -85,6 +85,9 @@ def __init__(self, init=None, **kwargs):
self._asdf = asdffile
self._instance = asdffile.tree["roman"]
elif isinstance(init, stnode.TaggedObjectNode):
if not isinstance(self, model_registry.get(init.__class__)):
expected = {mdl: node for node, mdl in model_registry.items()}[self.__class__].__name__
raise ValueError(f"TaggedObjectNode: {init.__class__.__name__} is not of the type expected. Expected {expected}")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should this be a ValidationError instead.

@codecov
Copy link

codecov bot commented Jun 5, 2023

Codecov Report

❗ No coverage uploaded for pull request base (main@0e78b01). Click here to learn what that means.
Patch coverage: 100.00% of modified lines in pull request are covered.

❗ Current head e7ea0c8 differs from pull request most recent head 3c065b1. Consider uploading reports for the commit 3c065b1 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #200   +/-   ##
=======================================
  Coverage        ?   98.02%           
=======================================
  Files           ?        8           
  Lines           ?     1218           
  Branches        ?        0           
=======================================
  Hits            ?     1194           
  Misses          ?       24           
  Partials        ?        0           
Impacted Files Coverage Δ
tests/test_models.py 98.32% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

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

@WilliamJamieson WilliamJamieson merged commit d89429e into spacetelescope:main Jun 6, 2023
@WilliamJamieson WilliamJamieson deleted the bugfix/model_node_types branch June 6, 2023 18:10
mairanteodoro pushed a commit to mairanteodoro/roman_datamodels that referenced this pull request Jun 8, 2023
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.

2 participants