-
Notifications
You must be signed in to change notification settings - Fork 20
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
Bugfix: Datamodels can be initialized with incorrect node. #200
Conversation
This checks that it can be initiallized with the correct node, and that it cannot be | ||
with any other node. | ||
""" | ||
img = create_node(node) |
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.
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.
src/roman_datamodels/datamodels.py
Outdated
@@ -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}") |
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.
Should this be a ValidationError
instead.
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #200 +/- ##
=======================================
Coverage ? 98.02%
=======================================
Files ? 8
Lines ? 1218
Branches ? 0
=======================================
Hits ? 1194
Misses ? 24
Partials ? 0
☔ View full report in Codecov by Sentry. |
6e6447d
to
e7ea0c8
Compare
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.
LGTM
e7ea0c8
to
3c065b1
Compare
@nden noticed that she could initialize any
Datamodel
with anystnode
, without any issues. This can lead to many unexpected issues, as theDatamodel
objects are wrappers around thestnode
. Thestnode
is what handles all the validation and serialization of theDatamodel
not theDatamodel
itself. Indeed, if one initializes aDatamodel
with the wrongstnode
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 eachDatamodel
against thestnode
passed to it during construction, an error is raised if thestnode
is not of the correct type.Checklist
CHANGES.rst
under the corresponding subsection