diff --git a/CHANGES.rst b/CHANGES.rst index 2ff38ae81..f0ce712b0 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,6 @@ 0.17.2 (unreleased) =================== -- Allow assignment to or creation of node attributes using dot notation of object instances +- Allow assignment to or creation of node attributes using dot notation of object instances with validation. [] - Allow DNode and LNode subclass instances to be assigned to tree attributes and support diff --git a/src/roman_datamodels/stnode/_node.py b/src/roman_datamodels/stnode/_node.py index 2b4d0a650..0824d9ee6 100644 --- a/src/roman_datamodels/stnode/_node.py +++ b/src/roman_datamodels/stnode/_node.py @@ -18,6 +18,7 @@ from astropy.time import Time from roman_datamodels.validate import ValidationWarning, _check_type, _error_message, will_strict_validate, will_validate + from ._registry import SCALAR_NODE_CLASSES_BY_KEY __all__ = ["DNode", "LNode"] @@ -63,9 +64,9 @@ def _validate(attr, instance, schema, ctx): # Note that the following checks cannot use isinstance since the TaggedObjectNode # and TaggedListNode subclasses will break as a result. And currently there is no # non-tagged subclasses of these classes that exist, nor are any envisioned yet. - if type(instance) == DNode: # noqa E721 + if type(instance) == DNode: # noqa E721 instance = instance._data - elif type(instance) == LNode: # noqa E721 + elif type(instance) == LNode: # noqa E721 instance = instance.data tagged_tree = yamlutil.custom_tree_to_tagged_tree(instance, ctx) return _value_change(attr, tagged_tree, schema, False, will_strict_validate(), ctx) diff --git a/tests/test_models.py b/tests/test_models.py index 7fe45379a..33241eae3 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -641,11 +641,12 @@ def test_node_assignment(): darkexp = darkmodel.meta.exposure assert isinstance(darkexp, stnode.DNode) darkexp.ngroups = darkexp.ngroups + 1 - assert(darkexp.ngroups == 7) + assert darkexp.ngroups == 7 darkmodel.meta.exposure = darkexp + # Test that a currently undefined attribute can be assigned using dot notation -# so long as the attribute is defined in the coresponding schema. +# so long as the attribute is defined in the corresponding schema. def test_node_new_attribute_assignment(): exp = stnode.Exposure() with pytest.raises(AttributeError): @@ -655,11 +656,11 @@ def test_node_new_attribute_assignment(): # Test patternProperties attribute case photmod = utils.mk_wfi_img_photom() phottab = photmod.phot_table - newphottab = {'F062': phottab['F062']} + newphottab = {"F062": phottab["F062"]} photmod.phot_table = newphottab - photmod.phot_table.F213 = phottab['F213'] + photmod.phot_table.F213 = phottab["F213"] with pytest.raises(AttributeError): - photmod.phot_table.F214 = phottab['F213'] + photmod.phot_table.F214 = phottab["F213"] with pytest.raises(ValidationError): photmod.phot_table.F106 = 0