Skip to content

Commit 6b37b3f

Browse files
committed
Fix Validator protocol init to match runtime
The runtime init signature of a validator can be seen with `inspect`: ```pycon >>> import inspect, jsonschema >>> inspect.signature(jsonschema.validators.Draft7Validator.__init__) <Signature (self, schema: 'referencing.jsonschema.Schema', resolver=None, format_checker: '_format.FormatChecker | None' = None, *, registry: 'referencing.jsonschema.SchemaRegistry' = <Registry (20 resources)>, _resolver=None) -> None> ``` This aligns the protocol's declared signature with that behavior more exactly with the following changes: `registry` is now keyword-only, not keyword-or-positional, and has a default. `resolver` is added to the declared signature, so users who are using it won't see typing-time discrepancies with the runtime. It is marked as `Any` and commented inline as deprecated, since it's unclear what else we could do to indicate its status. This means that code passing a resolver will continue to type check (previously it would not). `resolver` is the second keyword-or-positional and `format_checker` is the third, meaning that a positional-only caller who passes, for example: `Draft202012Validator(foo, None, bar)` will have `foo` slotted as the schema and `bar` as the `format_checker` This would primarily impact callers with positional-args calling conventions, but is more reflective of what they'll see at runtime. In order to remove `resolver` from the protocol signature, but match the runtime signatures well, some kind of placeholder is needed to indicate `format_checker` as positional-or-keyword. Or else, a large number of overloads for `__init__` could be declared to try to simulate its removal.
1 parent 980e09c commit 6b37b3f

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

jsonschema/protocols.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ class Validator(Protocol):
108108
def __init__(
109109
self,
110110
schema: Mapping | bool,
111-
registry: referencing.jsonschema.SchemaRegistry,
111+
resolver: Any = None, # deprecated
112112
format_checker: jsonschema.FormatChecker | None = None,
113-
) -> None:
114-
...
113+
*,
114+
registry: referencing.jsonschema.SchemaRegistry = ...,
115+
) -> None: ...
115116

116117
@classmethod
117118
def check_schema(cls, schema: Mapping | bool) -> None:

0 commit comments

Comments
 (0)