Skip to content

Commit c3729db

Browse files
committed
Enable doctests for the rest of the referencing page.
I recall this being broken previously where I couldn't simultaneously get doctesting to work while not breaking syntax highlighting, but that now appears to work.
1 parent 70a994c commit c3729db

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

docs/referencing.rst

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ We may wish to have other schemas we write be able to make use of this schema, a
9191

9292
To do so we make use of APIs from the referencing library to create a `referencing.Registry` which maps the URIs above to this schema:
9393

94-
.. code:: python
94+
.. testcode::
9595

9696
from referencing import Registry, Resource
9797
schema = Resource.from_contents(
@@ -130,7 +130,7 @@ which has the same functional effect.
130130
You can now pass this registry to your `Validator`, which allows a schema passed to it to make use of the aforementioned URIs to refer to our non-negative integer schema.
131131
Here for instance is an example which validates that instances are JSON objects with non-negative integral values:
132132

133-
.. code:: python
133+
.. testcode::
134134

135135
from jsonschema import Draft202012Validator
136136
validator = Draft202012Validator(
@@ -141,7 +141,7 @@ Here for instance is an example which validates that instances are JSON objects
141141
registry=registry, # the critical argument, our registry from above
142142
)
143143
validator.validate({"foo": 37})
144-
validator.validate({"foo": -37}) # Uh oh!
144+
assert not validator.is_valid({"foo": -37}) # Uh oh!
145145

146146
.. _ref-filesystem:
147147

@@ -154,7 +154,7 @@ If however you wish to *dynamically* read files off of the file system, perhaps
154154

155155
Here we resolve any schema beginning with ``http://localhost`` to a directory ``/tmp/schemas`` on the local filesystem (note of course that this will not work if run directly unless you have populated that directory with some schemas):
156156

157-
.. code:: python
157+
.. testcode::
158158

159159
from pathlib import Path
160160
import json
@@ -177,7 +177,7 @@ Such a registry can then be used with `Validator` objects in the same way shown
177177

178178
We can mix the two examples above if we wish for some in-memory schemas to be available in addition to the filesystem schemas, e.g.:
179179

180-
.. code:: python
180+
.. testcode::
181181

182182
from referencing.jsonschema import DRAFT7
183183
registry = Registry(retrieve=retrieve_from_filesystem).with_resource(
@@ -194,7 +194,7 @@ As long as you deserialize what you have retrieved into Python objects, you may
194194

195195
Here for instance we retrieve YAML documents in a way similar to the `above <ref-filesystem>` using PyYAML:
196196

197-
.. code:: python
197+
.. testcode::
198198

199199
from pathlib import Path
200200
import yaml
@@ -234,7 +234,7 @@ However, if you as a schema author are in a situation where you indeed do wish t
234234

235235
Here is how one would configure a registry to automatically retrieve schemas from the `JSON Schema Store <https://www.schemastore.org>`_ on the fly using the `httpx <https://www.python-httpx.org/>`_:
236236

237-
.. code:: python
237+
.. testcode::
238238

239239
from referencing import Registry, Resource
240240
import httpx
@@ -247,7 +247,24 @@ Here is how one would configure a registry to automatically retrieve schemas fro
247247

248248
Given such a registry, we can now, for instance, validate instances against schemas from the schema store by passing the ``registry`` we configured to our `Validator` as in previous examples:
249249

250-
.. code:: python
250+
.. testsetup:: *
251+
252+
import sys
253+
254+
class FakeResource:
255+
def json(self):
256+
return {
257+
"$schema": "https://json-schema.org/draft/2020-12/schema",
258+
"not": True,
259+
}
260+
261+
class FakeHTTPX:
262+
def get(self, uri):
263+
return FakeResource()
264+
265+
sys.modules["httpx"] = FakeHTTPX()
266+
267+
.. testcode::
251268

252269
from jsonschema import Draft202012Validator
253270
Draft202012Validator(
@@ -257,14 +274,10 @@ Given such a registry, we can now, for instance, validate instances against sche
257274

258275
which should in this case indicate the example data is invalid:
259276

260-
.. code:: python
277+
.. testoutput::
261278

262279
Traceback (most recent call last):
263-
File "example.py", line 14, in <module>
264-
).validate({"project": {"name": 12}})
265-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
266-
File "jsonschema/validators.py", line 345, in validate
267-
raise error
280+
...
268281
jsonschema.exceptions.ValidationError: 12 is not of type 'string'
269282

270283
Failed validating 'type' in schema['properties']['project']['properties']['name']:
@@ -315,7 +328,7 @@ The ``store`` argument
315328

316329
If you currently pass a set of schemas via e.g.:
317330

318-
.. code:: python
331+
.. code-block:: python
319332
320333
from jsonschema import Draft202012Validator, RefResolver
321334
resolver = RefResolver.from_schema(
@@ -330,7 +343,7 @@ If you currently pass a set of schemas via e.g.:
330343
331344
you should be able to simply move to something like:
332345

333-
.. code:: python
346+
.. testcode::
334347

335348
from referencing import Registry
336349
from referencing.jsonschema import DRAFT202012
@@ -345,7 +358,7 @@ you should be able to simply move to something like:
345358
{"$ref": "http://example.com"},
346359
registry=registry,
347360
)
348-
validator.validate("foo")
361+
assert not validator.is_valid("foo")
349362

350363
Handlers
351364
~~~~~~~~
@@ -355,7 +368,7 @@ The ``handlers`` functionality from `_RefResolver` was a way to support addition
355368
Here you should move to a custom ``retrieve`` function which does whatever you'd like.
356369
E.g. in pseudocode:
357370

358-
.. code:: python
371+
.. testcode::
359372

360373
from urllib.parse import urlsplit
361374

0 commit comments

Comments
 (0)