22import os
33import pathlib
44import warnings
5+ from dataclasses import dataclass
56
67import numpy as np
78import pytest
@@ -122,7 +123,7 @@ def find_examples_in_schema(self):
122123
123124 for node in treeutil .iter_tree (schema_tree ):
124125 if isinstance (node , dict ) and "examples" in node and isinstance (node ["examples" ], list ):
125- for desc , example in node ["examples" ]:
126+ for example in node ["examples" ]:
126127 yield example
127128
128129
@@ -153,6 +154,44 @@ def reportinfo(self):
153154 return self .fspath , 0 , ""
154155
155156
157+ @dataclass
158+ class SchemaExample :
159+ description : str
160+ example : str
161+ _version : str = None
162+ other : any = None
163+
164+ @classmethod
165+ def from_schema (cls , example : list ):
166+ if len (example ) == 1 :
167+ _description = ""
168+ _example = example [0 ]
169+ elif len (example ) == 2 :
170+ _description = example [0 ]
171+ _example = example [1 ]
172+ _version = None
173+ _other = None
174+ elif len (example ) > 2 :
175+ _description = example [0 ]
176+ _example = example [2 ]
177+ _version = example [1 ]
178+ _other = example [3 :] if len (example ) > 3 else None
179+ else :
180+ raise RuntimeError ("Invalid example" )
181+
182+ return cls (_description , _example , _version , _other )
183+
184+ @property
185+ def version (self ):
186+ import asdf .versioning as versioning
187+
188+ if self ._version is None :
189+ return versioning .default_version
190+
191+ version = self ._version .lower ().split ("asdf-standard-" )[1 ]
192+ return versioning .AsdfVersion (version )
193+
194+
156195class AsdfSchemaExampleItem (pytest .Item ):
157196 @classmethod
158197 def from_parent (
@@ -172,7 +211,7 @@ def from_parent(
172211 result = AsdfSchemaExampleItem (name , parent , ** kwargs )
173212
174213 result .filename = str (schema_path )
175- result .example = example
214+ result .example = SchemaExample . from_schema ( example )
176215 result .ignore_unrecognized_tag = ignore_unrecognized_tag
177216 result .ignore_version_mismatch = ignore_version_mismatch
178217 return result
@@ -183,7 +222,7 @@ def runtest(self):
183222
184223 # Make sure that the examples in the schema files (and thus the
185224 # ASDF standard document) are valid.
186- buff = helpers .yaml_to_asdf ("example: " + self .example .strip ())
225+ buff = helpers .yaml_to_asdf ("example: " + self .example .example . strip (), standard_version = self . example . version )
187226
188227 ff = AsdfFile (
189228 uri = util .filepath_to_url (os .path .abspath (self .filename )),
@@ -212,7 +251,7 @@ def runtest(self):
212251
213252 ff ._open_impl (ff , buff , mode = "rw" )
214253 except Exception :
215- print (" From file:" , self .filename )
254+ print (f"Example: { self . example . description } \n From file: { self .filename } " )
216255 raise
217256
218257 # Just test we can write it out. A roundtrip test
0 commit comments