1
+ import jschon
1
2
from abc import abstractmethod , ABCMeta
2
3
3
4
4
- __all__ = ["Annotation" ]
5
+ __all__ = ["InvalidSchema" , "InvalidAnnotation" , "Annotation" ]
6
+
7
+
8
+ def _create_catalog ():
9
+ return jschon .create_catalog ('2020-12' )
10
+
11
+
12
+ class InvalidSchema (Exception ):
13
+ """Exception raised when an annotation with a non-conformant schema is defined."""
14
+
15
+
16
+ class InvalidAnnotation (Exception ):
17
+ """Exception raised when an annotation."""
5
18
6
19
7
20
class Annotation (metaclass = ABCMeta ):
@@ -14,7 +27,8 @@ class Annotation(metaclass=ABCMeta):
14
27
15
28
#: :class:`dict`: Schema of this annotation, expressed in the `JSON Schema`_ language.
16
29
#:
17
- #: Subclasses of :class:`Annotation` must implement this class attribute.
30
+ #: Subclasses of :class:`Annotation` must implement this class attribute. If the value of
31
+ #: the attribute is not a valid Amaranth annotation JSON Schema, :exc:`InvalidSchema` is raised.
18
32
schema = {}
19
33
20
34
def __init_subclass__ (cls , ** kwargs ):
@@ -27,14 +41,8 @@ def __init_subclass__(cls, **kwargs):
27
41
if "$id" not in cls .schema :
28
42
raise ValueError (f"'$id' keyword is missing from Annotation schema: { cls .schema } " )
29
43
30
- try :
31
- import jsonschema
32
- jsonschema .Draft202012Validator .check_schema (cls .schema )
33
- except ImportError :
34
- # Amaranth was installed in some weird way and doesn't have jsonschema installed,
35
- # despite it being a mandatory dependency. The schema will eventually get checked
36
- # by the CI, so ignore the error here.
37
- pass # :nocov:
44
+ schema_validity = jschon .JSONSchema (cls .schema , catalog = _create_catalog ()).validate ()
45
+ # TODO
38
46
39
47
@property
40
48
@abstractmethod
@@ -74,11 +82,11 @@ def validate(cls, instance):
74
82
75
83
Raises
76
84
------
77
- :exc:`jsonschema.exceptions.ValidationError `
85
+ :exc:`InvalidAnnotation `
78
86
If :py:`instance` doesn't comply with :attr:`Annotation.schema`.
79
87
"""
80
- import jsonschema
81
- jsonschema . validate ( instance , schema = cls . schema )
88
+ validity = JSONSchema ( cls . schema , catalog = _create_catalog ()). evaluate ( instance )
89
+ # TODO
82
90
83
91
def __repr__ (self ):
84
- return f"<{ type (self ).__module__ } .{ type (self ).__qualname__ } for { self .origin !r} >"
92
+ return f"<{ type (self ).__module__ } .{ type (self ).__qualname__ } for { self .origin !r} >"
0 commit comments