The signature of jsonschema.protocols.Validator is backward incompatible with earlier versions and definitions such as jsonschema.Draft202012Validator in static type checking. Specifically, an registry argument is now needed, as far as static checking is concerned, in instantiating an object of a class that is casted as a jsonschema.protocols.Validator.
# as_validator.py
from typing import cast, Type
from jsonschema import Draft202012Validator
from jsonschema.protocols import Validator
validator_cls = cast(Type[Validator], Draft202012Validator)
schema = {"maxItems" : 2}
validator_cls(schema).validate([2, 3, 4])
Running mypy on tmp.py produces the following error.
(dandi-schema) ➜ dandi-schema git:(devendorize) ✗ mypy as_validator.py
as_validator.py:12: error: Missing positional argument "registry" in call to "Validator" [call-arg]
validator_cls(schema).validate([2, 3, 4])
^~~~~~~~~~~~~~~~~~~~~
Found 1 error in 1 file (checked 1 source file)
# as_draft_202012_validator.py
from jsonschema import Draft202012Validator
schema = {"maxItems" : 2}
Draft202012Validator(schema).validate([2, 3, 4])
On the other hand, running mypy on as_draft_202012_validator.py doesn't produce any error.
(dandi-schema) ➜ dandi-schema git:(devendorize) ✗ mypy as_draft_202012_validator.py
Success: no issues found in 1 source file
This incompatibility exists, at least, in three places source code, documentation, and the types-jsonschema package.
Context:
Python used: 3.9.16
The signature of
jsonschema.protocols.Validatoris backward incompatible with earlier versions and definitions such asjsonschema.Draft202012Validatorin static type checking. Specifically, anregistryargument is now needed, as far as static checking is concerned, in instantiating an object of a class that is casted as ajsonschema.protocols.Validator.Running mypy on
tmp.pyproduces the following error.On the other hand, running mypy on
as_draft_202012_validator.pydoesn't produce any error.This incompatibility exists, at least, in three places source code, documentation, and the
types-jsonschemapackage.types-jsonschemaat the current version of4.24.0.20250708Context:
Python used: 3.9.16