Skip to content

Commit c11966a

Browse files
committed
Harmonize annotations with python/typeshed#8608
python/typeshed#8608 introduced annotations for `create` which are not fully reflected here. In order to reflect that state into `jsonschema`, a new module, `jsonschema._typing` is introduced. The purpose of `_typing` is to be a singular place for the library to define type aliases and any typing-related utilities which may be needed. This will let us use aliases like `_typing.JsonValue` in many locations where any valid JSON datatype is accepted. The definitions can be refined over time as necessary. Initial definitions in `_typing` are: - Schema (any JSON object) - JsonObject (any JSON object) - JsonValue (any JSON value, including objects or sequences) `Schema` is just another name for `JsonObject`. Perhaps it is not needed, but the name may help clarify things to a reader. It is not obvious at present whether or not it is a good or bad idea to notate it as such, but a similar Schema alias is defined in typeshed and seems to be working there to annotate things accurately. These types are using `Mapping` and `Sequence` rather than `dict` or `list`. The rationale is that `jsonschema`'s logic does not dictate that the objects used *must* be defined in stdlib types or subtypes thereof. For example, a `collections.UserDict` could be used and should be accepted by the library (`UserDict` wraps but does not inherit from `dict`.)
1 parent e24f0ee commit c11966a

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

jsonschema/_typing.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
Type aliases and utilities to help manage `typing` usage and type annotations.
3+
"""
4+
5+
from collections.abc import Mapping, Sequence
6+
import typing
7+
8+
Schema = Mapping[str, typing.Any]
9+
10+
JsonObject = Mapping[str, typing.Any]
11+
12+
JsonValue = typing.Union[
13+
JsonObject, Sequence[typing.Any], str, int, float, bool, None
14+
]

jsonschema/validators.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@
2323
_format,
2424
_legacy_validators,
2525
_types,
26+
_typing,
2627
_utils,
2728
_validators,
2829
exceptions,
2930
)
3031

3132
_UNSET = _utils.Unset()
3233

34+
_ValidatorCallback = typing.Callable[
35+
[typing.Any, typing.Any, _typing.JsonValue, _typing.JsonObject],
36+
typing.Iterator[exceptions.ValidationError],
37+
]
38+
3339
_VALIDATORS: dict[str, typing.Any] = {}
3440
_META_SCHEMAS = _utils.URIDict()
3541
_VOCABULARIES: list[tuple[str, typing.Any]] = []
@@ -114,13 +120,16 @@ def _store_schema_list():
114120

115121

116122
def create(
117-
meta_schema,
118-
validators=(),
123+
meta_schema: _typing.Schema,
124+
validators: Mapping[str, _ValidatorCallback] | tuple[()] = (),
119125
version=None,
120-
type_checker=_types.draft202012_type_checker,
121-
format_checker=_format.draft202012_format_checker,
122-
id_of=_id_of,
123-
applicable_validators=methodcaller("items"),
126+
type_checker: _types.TypeChecker = _types.draft202012_type_checker,
127+
format_checker: _format.FormatChecker = _format.draft202012_format_checker,
128+
id_of: typing.Callable[[_typing.Schema], str] = _id_of,
129+
applicable_validators: typing.Callable[
130+
[_typing.JsonObject],
131+
typing.Iterable[tuple[str, _ValidatorCallback]]
132+
] = methodcaller("items"),
124133
):
125134
"""
126135
Create a new validator class.
@@ -186,7 +195,7 @@ def create(
186195
@attr.s
187196
class Validator:
188197

189-
VALIDATORS = dict(validators)
198+
VALIDATORS: dict[str, _ValidatorCallback] = dict(validators)
190199
META_SCHEMA = dict(meta_schema)
191200
TYPE_CHECKER = type_checker
192201
FORMAT_CHECKER = format_checker_arg
@@ -339,7 +348,7 @@ def is_valid(self, instance, _schema=None):
339348
if version is not None:
340349
safe = version.title().replace(" ", "").replace("-", "")
341350
Validator.__name__ = Validator.__qualname__ = f"{safe}Validator"
342-
Validator = validates(version)(Validator)
351+
Validator = validates(version)(Validator) # type: ignore[misc]
343352

344353
return Validator
345354

0 commit comments

Comments
 (0)