Skip to content

Commit a7be716

Browse files
committed
Update jsonschema stubs to track latest changes
In python-jsonschema/jsonschema#1396 , the type signature for `Validator.__init__` is updated to better match the runtime signature. This backports the fix to typeshed, keeping the copies of this data in sync. python-jsonschema/jsonschema#1396 is, itself, a response to feedback on `jsonschema` about the changes in `typeshed` python#14327. In addition to the `__init__` fix, a couple of additional small changes are made, both in `jsonschema` and here in the stubs: 1. In `jsonschema`, the type for `create()` in `validators.py` was updated to be notated with `-> type[Validator]`. This was necessary for internal testing on types to correctly read that validator classes created by this factory implement the protocol. 2. Here, in order to better guarantee that the types align, the `_Validator` class (which does not exist in `jsonschema`, but is only defined here in the stubs) now inherits from `Validator`. 3. The init signature for `_Validator` is updated to match 4. `tests/mypy_test.py` flags the `schema` instance variable annotation as mismatching between `Validator` and `_Validator`. Review against the `jsonschema` source reveals that `_Validator` was closer to correct, so `Validator` is fixed to match. Any further changes (e.g., elimination of `_Validator` or changing `create`'s return type annotation) are left as potential future work.
1 parent d270bb0 commit a7be716

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

stubs/jsonschema/jsonschema/protocols.pyi

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from _typeshed import Incomplete
22
from collections.abc import Iterator, Mapping, Sequence
3-
from typing import ClassVar, Protocol
3+
from typing import Any, ClassVar, Protocol
44
from typing_extensions import TypeAlias
55

66
import referencing.jsonschema
@@ -15,12 +15,14 @@ class Validator(Protocol):
1515
VALIDATORS: ClassVar[dict[Incomplete, Incomplete]]
1616
TYPE_CHECKER: ClassVar[TypeChecker]
1717
FORMAT_CHECKER: ClassVar[FormatChecker]
18-
schema: dict[Incomplete, Incomplete] | bool
18+
schema: referencing.jsonschema.Schema
1919
def __init__(
2020
self,
21-
schema: dict[Incomplete, Incomplete] | bool,
22-
registry: referencing.jsonschema.SchemaRegistry,
21+
schema: Mapping[Incomplete, Incomplete] | bool,
22+
resolver: Any = None, # deprecated
2323
format_checker: FormatChecker | None = None,
24+
*,
25+
registry: referencing.jsonschema.SchemaRegistry = ...,
2426
) -> None: ...
2527
@classmethod
2628
def check_schema(cls, schema: dict[Incomplete, Incomplete]) -> None: ...

stubs/jsonschema/jsonschema/validators.pyi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ _ValidatorCallback: TypeAlias = Callable[[Any, Any, _JsonValue, _JsonObject], It
2121
# This class does not exist at runtime. Compatible classes are created at
2222
# runtime by create().
2323
@type_check_only
24-
class _Validator:
24+
class _Validator(Validator):
2525
VALIDATORS: ClassVar[dict[Incomplete, Incomplete]]
2626
META_SCHEMA: ClassVar[dict[Incomplete, Incomplete]]
2727
TYPE_CHECKER: ClassVar[Incomplete]
@@ -32,12 +32,11 @@ class _Validator:
3232
format_checker: FormatChecker | None
3333
def __init__(
3434
self,
35-
schema: Schema,
36-
resolver=None,
35+
schema: Mapping[Incomplete, Incomplete] | bool,
36+
resolver: Any = None, # deprecated
3737
format_checker: FormatChecker | None = None,
3838
*,
3939
registry: SchemaRegistry = ...,
40-
_resolver=None,
4140
) -> None: ...
4241
@classmethod
4342
def check_schema(cls, schema: Schema, format_checker: FormatChecker | Unset = ...) -> None: ...

0 commit comments

Comments
 (0)