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

Make model subscription follow the same reasoning as node subscription #322

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

- Added documentation for ``stnode``. [#316]

- Make datamodels follow the same subscription pattern as the ``stnode`` based
objects. [#322]

0.19.0 (2024-02-09)
===================

Expand Down
8 changes: 4 additions & 4 deletions src/roman_datamodels/datamodels/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,10 @@ def __getattr__(self, attr):
def __setitem__(self, key, value):
if key.startswith("_"):
raise ValueError("May not specify attributes/keys that start with _")
if hasattr(self._instance, key):
setattr(self._instance, key, value)
else:
self._instance._data[key] = value
self._instance[key] = value

def __getitem__(self, key):
return self._instance[key]

def __iter__(self):
return iter(self._instance)
Expand Down
14 changes: 13 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,19 @@ def test_add_model_attribute(tmp_path):
readnoise2.new_attribute = 88
assert readnoise2.new_attribute == 88
with pytest.raises(ValidationError):
readnoise["data"] = "bad_data_value"
readnoise.data = "bad_data_value"


def test_model_subscribable(tmp_path):
"""
Test that __getitem__ exists
"""
file_path = tmp_path / "testreadnoise.asdf"

utils.mk_readnoise(shape=(8, 8), filepath=file_path)
with datamodels.open(file_path) as readnoise:
assert readnoise["data"].shape == (8, 8)
assert readnoise.data is readnoise["data"]


# Saturation tests
Expand Down
Loading