python-jsonschema / jsonschema

An implementation of the JSON Schema specification for Python
https://python-jsonschema.readthedocs.io
MIT License
4.52k stars 574 forks source link

Documentation is out to date #1251

Closed pbordron closed 2 months ago

pbordron commented 2 months ago

I wanted to load some schemas in memory, but got following errors when reproducing the example:

from referencing import Registry, Resource
from referencing.jsonschema import DRAFT2020212
schema = DRAFT202012.create_resource({"type": "integer", "minimum": 0})
registry = Registry().with_resources(
    [
        ("http://example.com/nonneg-int-schema", schema),
        ("urn:nonneg-integer-schema", schema),
    ],
)
from jsonschema import Draft202012Validator
validator = Draft202012Validator(
    {
        "type": "object",
        "additionalProperties": {"$ref": "urn:nonneg-integer-schema"},
    },
    registry=registry,  # the critical argument, our registry from above
)
validator.validate({"foo": 37})
validator.validate({"foo": -37})  # Uh oh!

I didn't test other examples, but they might be also wrong.

Could you update the documentation to reflect current version of jsonschema?

Julian commented 2 months ago

Hi. You haven't included any version information or what command you ran / what you installed, so it's hard to know what went wrong, but those examples are correct (and run without error here on the current version of jsonschema), and the docs are doctested to ensure they stay correct hopefully.

Feel free to follow up with more info if need be, but otherwise you presumably have installed a very old version that doesn't match the one in the docs you're reading.

Julian commented 2 months ago

Oh! Indeed the example you sent is the one example not doctested, and has a typo, it's DRAFT202012 not DRAFT2020212.

pbordron commented 2 months ago
$ conda list jsonschema

# Name                    Version                   Build  Channel
jsonschema                4.21.1             pyhd8ed1ab_0    conda-forge
jsonschema-specifications 2023.6.1           pyhd8ed1ab_0    conda-forge
jsonschema-with-format    4.21.1             pyhd8ed1ab_0    conda-forge
jsonschema-with-format-nongpl 4.21.1             pyhd8ed1ab_0    conda-forge

Still it produces an error on argument 'registry' listed in API

TypeError: create.<locals>.Validator.__init__() got an unexpected keyword argument 'registry'
Julian commented 2 months ago

I've fixed the typo (and enabled doctesting for the example(s), I remember now that for that page I couldn't get things to properly syntax highlight when doctesting was enabled, but that appears fixed now).

Still it produces an error on argument 'registry' listed in API

That doesn't reproduce here.

What command are you running to install jsonschema that reproduces that error?

pbordron commented 2 months ago

OK, it was my fault.

An old jsonschema version out nowhere and installed with pip was polluting my conda installation (maybe something managed by vscode)

jsonschema                     4.17.3

Removing it solve the issue

Julian commented 2 months ago

Cool, thanks for the report.