Skip to content

Commit a9e7b03

Browse files
committed
begin migration to jschon
1 parent 4e7bb9f commit a9e7b03

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

amaranth/lib/meta.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1+
import jschon
12
from abc import abstractmethod, ABCMeta
23

34

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."""
518

619

720
class Annotation(metaclass=ABCMeta):
@@ -14,7 +27,8 @@ class Annotation(metaclass=ABCMeta):
1427

1528
#: :class:`dict`: Schema of this annotation, expressed in the `JSON Schema`_ language.
1629
#:
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.
1832
schema = {}
1933

2034
def __init_subclass__(cls, **kwargs):
@@ -27,14 +41,8 @@ def __init_subclass__(cls, **kwargs):
2741
if "$id" not in cls.schema:
2842
raise ValueError(f"'$id' keyword is missing from Annotation schema: {cls.schema}")
2943

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
3846

3947
@property
4048
@abstractmethod
@@ -74,11 +82,11 @@ def validate(cls, instance):
7482
7583
Raises
7684
------
77-
:exc:`jsonschema.exceptions.ValidationError`
85+
:exc:`InvalidAnnotation`
7886
If :py:`instance` doesn't comply with :attr:`Annotation.schema`.
7987
"""
80-
import jsonschema
81-
jsonschema.validate(instance, schema=cls.schema)
88+
validity = JSONSchema(cls.schema, catalog=_create_catalog()).evaluate(instance)
89+
# TODO
8290

8391
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}>"

docs/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
intersphinx_mapping = {
2929
"python": ("https://docs.python.org/3", None),
30-
"jsonschema": (f"https://python-jsonschema.readthedocs.io/en/v{package_version('jsonschema')}/", None),
3130
}
3231

3332
todo_include_todos = True

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ license = {file = "LICENSE.txt"}
1515
requires-python = "~=3.8"
1616
dependencies = [
1717
"importlib_resources; python_version<'3.9'", # for amaranth._toolchain.yosys
18-
"jsonschema~=4.20.0", # for amaranth.lib.meta
18+
"jschon~=0.11.1", # for amaranth.lib.meta
1919
"pyvcd>=0.2.2,<0.5", # for amaranth.sim.pysim
2020
"Jinja2~=3.0", # for amaranth.build
2121
]

0 commit comments

Comments
 (0)