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 deprecated_properties #303

Merged
merged 3 commits into from
Jun 13, 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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
- Remove unnecessary references to overwritten datamodel
attributes to free up memory [#301]

- Remove unused ``deprecated_properties`` [#303]


1.10.1 (2024-03-25)
===================
Expand Down
52 changes: 0 additions & 52 deletions src/stdatamodels/properties.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

import copy
import warnings
import numpy as np
from collections.abc import Mapping
from astropy.io import fits
Expand Down Expand Up @@ -297,54 +296,11 @@ def __eq__(self, other):
else:
return self._instance == other

def _is_deprecated(self, attr):
return "deprecated_properties" in self._schema and attr in self._schema["deprecated_properties"]

def _warn_deprecated_attr(self, attr):
node = self
path = []
while node._parent is not None:
if not isinstance(node, ListNode):
path.append(node._name)
node = node._parent

path.reverse()

old_attr = ".".join(path + [attr])

property_path = self._schema["deprecated_properties"][attr]
parts = property_path.split("/")
for part in parts:
if part == "..":
path.pop()
else:
path.append(part)

new_attr = ".".join(path)

warnings.warn(f"Attribute '{old_attr}' has been deprecated. Use '{new_attr}' instead.", DeprecationWarning)

def _get_deprecated(self, attr):
property_path = self._schema["deprecated_properties"][attr]
node = self
parts = property_path.split("/")
for part in parts[:-1]:
if part == "..":
node = node._parent
else:
node = getattr(node, part)
return node, parts[-1]

def __getattr__(self, attr):

if attr.startswith('_'):
raise AttributeError('No attribute {0}'.format(attr))

if self._is_deprecated(attr):
self._warn_deprecated_attr(attr)
node, new_attr = self._get_deprecated(attr)
return getattr(node, new_attr)

schema = _get_schema_for_property(self._schema, attr)
try:
val = self._instance[attr]
Expand All @@ -368,10 +324,6 @@ def __getattr__(self, attr):
def __setattr__(self, attr, val):
if attr.startswith('_'):
self.__dict__[attr] = val
elif self._is_deprecated(attr):
self._warn_deprecated_attr(attr)
node, new_attr = self._get_deprecated(attr)
setattr(node, new_attr, val)
else:
schema = _get_schema_for_property(self._schema, attr)
if val is None:
Expand All @@ -388,10 +340,6 @@ def __setattr__(self, attr, val):
def __delattr__(self, attr):
if attr.startswith('_'):
del self.__dict__[attr]
elif self._is_deprecated(attr):
self._warn_deprecated_attr(attr)
node, new_attr = self._get_deprecated(attr)
delattr(node, new_attr)
else:
schema = _get_schema_for_property(self._schema, attr)
if validate.value_change(attr, None, schema, self._ctx) or self._ctx._pass_invalid_values:
Expand Down
9 changes: 0 additions & 9 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,3 @@ class TableModel(DataModel):
Model that includes a recarray-style table.
"""
schema_url = "http://example.com/schemas/table_model"


class DeprecatedModel(DataModel):
"""
Model with a top-level "old_origin" property that has
moved to "meta.origin", and a "meta.old_data" property that has
moved to "data".
"""
schema_url = "http://example.com/schemas/deprecated_model"
45 changes: 0 additions & 45 deletions tests/schemas/deprecated_model.yaml

This file was deleted.

41 changes: 1 addition & 40 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from stdatamodels import DataModel

from models import BasicModel, AnyOfModel, DeprecatedModel, TableModel, TransformModel
from models import BasicModel, AnyOfModel, TableModel, TransformModel


def test_init_from_pathlib(tmp_path):
Expand Down Expand Up @@ -287,45 +287,6 @@ def on_save(self, init):
assert model.meta.foo == "bar"


def test_deprecated_properties():
model = DeprecatedModel()

model.meta.origin = "somewhere"

with pytest.warns(DeprecationWarning, match="Attribute 'old_origin' has been deprecated. Use 'meta.origin' instead."):
assert model.old_origin == "somewhere"

with pytest.warns(DeprecationWarning, match="Attribute 'old_origin' has been deprecated. Use 'meta.origin' instead."):
model.old_origin = "somewhere else"
assert model.meta.origin == "somewhere else"

data = np.arange(100, dtype=np.float32).reshape(10, 10)
model.data = data
with pytest.warns(DeprecationWarning, match="Attribute 'meta.old_data' has been deprecated. Use 'data' instead."):
assert model.meta.old_data is data

data = np.arange(80, dtype=np.float32).reshape(8, 10)
with pytest.warns(DeprecationWarning, match="Attribute 'meta.old_data' has been deprecated. Use 'data' instead."):
model.meta.old_data = data
assert model.data is data

model.meta.array_property.append(model.meta.array_property.item())
model.meta.array_property[0].bam = "some value"
with pytest.warns(
DeprecationWarning,
match="Attribute 'meta.array_property.baz' has been deprecated. Use 'meta.array_property.bam' instead.",
):
assert model.meta.array_property[0].baz == "some value"

with pytest.warns(
DeprecationWarning,
match="Attribute 'meta.array_property.baz' has been deprecated. Use 'meta.array_property.bam' instead.",
):
model.meta.array_property[0].baz = "some other value"

assert model.meta.array_property[0].bam == "some other value"


@pytest.mark.parametrize("ModelType", [DataModel, BasicModel, TableModel, TransformModel])
def test_garbage_collectable(ModelType, tmp_path):
# This is a regression test to attempt to avoid future changes that might
Expand Down
Loading