Repository: spacetelescope/stdatamodels (language: python)

Issue:
IRS2Model does not inherit from ReferenceFileModel
https://github.com/spacetelescope/stdatamodels/blob/503320f6325f7dd454dc2d7c177043ddf069c6f6/src/stdatamodels/jwst/datamodels/irs2.py#L7

This is described as a reference file but doesn't inherit from ReferenceFileModel.


Discussion / hints:

Fix patch:
diff --git a/changes/348.bugfix.rst b/changes/348.bugfix.rst
new file mode 100644
index 00000000..73836824
--- /dev/null
+++ b/changes/348.bugfix.rst
@@ -0,0 +1,1 @@
+Change IRS2Model parent class to ReferenceFileModel
diff --git a/src/stdatamodels/jwst/datamodels/irs2.py b/src/stdatamodels/jwst/datamodels/irs2.py
index a4e83644..fda7cc3c 100644
--- a/src/stdatamodels/jwst/datamodels/irs2.py
+++ b/src/stdatamodels/jwst/datamodels/irs2.py
@@ -1,10 +1,10 @@
-from .model_base import JwstDataModel
+from .reference import ReferenceFileModel
 
 
 __all__ = ['IRS2Model']
 
 
-class IRS2Model(JwstDataModel):
+class IRS2Model(ReferenceFileModel):
     """
     A data model for the IRS2 refpix reference file.
 


Test patch:
diff --git a/src/stdatamodels/jwst/datamodels/tests/test_models.py b/src/stdatamodels/jwst/datamodels/tests/test_models.py
index c9dc20c5..cedd4ba2 100644
--- a/src/stdatamodels/jwst/datamodels/tests/test_models.py
+++ b/src/stdatamodels/jwst/datamodels/tests/test_models.py
@@ -3,6 +3,7 @@
 import warnings
 
 from asdf.exceptions import ValidationError
+from asdf.schema import load_schema
 from astropy.io import fits
 from astropy.table import Table
 from astropy.time import Time
@@ -19,6 +20,7 @@
                                           Level1bModel)
 from stdatamodels.jwst import datamodels
 from stdatamodels.jwst.datamodels import _defined_models as defined_models
+from stdatamodels.schema import walk_schema
 
 ROOT_DIR = os.path.join(os.path.dirname(__file__), 'data')
 FITS_FILE = os.path.join(ROOT_DIR, 'test.fits')
@@ -317,6 +319,28 @@ def test_abvega_offset_model():
         model.validate()
 
 
+def test_defined_models_up_to_date():
+    """
+    Test that defined_models contains all JwstDataModel classes
+    """
+    def get_classes(klass, classes=None):
+        if classes is None:
+            classes = set()
+        classes.add(klass)
+        for subclass in klass.__subclasses__():
+            get_classes(subclass, classes)
+        return classes
+
+    # get all datamodel classes including JwstDataModel
+    # and all subclasses (ignoring any that start with "_")
+    all_classes = {
+        klass for
+        klass in get_classes(JwstDataModel)
+        if klass.__name__[0] != "_"
+    }
+    assert set(defined_models.values()) == all_classes
+
+
 @pytest.mark.parametrize("model", [v for v in defined_models.values()])
 def test_all_datamodels_init(model):
     """
@@ -325,6 +349,40 @@ def test_all_datamodels_init(model):
     model()
 
 
+@pytest.mark.parametrize("model", [v for v in defined_models.values()])
+def test_reference_model_schema_inheritance(model):
+    """
+    Test that models that inherit from ReferenceFileModel use
+    referencefile.schema and that all models that use
+    referencefile.schema inherit from ReferenceFileModel.
+    """
+    is_ref_model = issubclass(model, datamodels.ReferenceFileModel)
+    if not model.schema_url:  # this model has no schema
+        # so it shouldn't be a subclass
+        assert not is_ref_model
+        # and we're dont testing this model
+        return
+
+    # crawl the schema looking for all ids
+    schema = load_schema(model.schema_url, resolve_references=True)
+
+    def cb(subschema, path, combiner, ctx, recurse):
+        if not isinstance(subschema, dict):
+            return
+        if 'id' not in subschema:
+            return
+        if subschema["id"] == "http://stsci.edu/schemas/jwst_datamodel/referencefile.schema":
+            ctx['has_ref'] = True
+
+    ctx = {'has_ref': False}
+    walk_schema(schema, cb, ctx=ctx)
+    refs_referencefile_schema = ctx['has_ref']
+    if is_ref_model:
+        assert refs_referencefile_schema, f"Reference model {model} does not ref referencefile.schema"
+    else:
+        assert not refs_referencefile_schema, f"Model {model} does not inherit from ReferenceFileModel"
+
+
 def test_meta_date_management(tmp_path):
     model = JwstDataModel({"meta": {"date": "2000-01-01T00:00:00.000"}})
     assert model.meta.date == "2000-01-01T00:00:00.000"