From 989bd8b0eb925b5c82e5570b2e21f4d4982fabe4 Mon Sep 17 00:00:00 2001 From: Stephen Rosen Date: Wed, 24 Aug 2022 23:18:49 +0000 Subject: [PATCH 1/2] Improve annotation of jsonschema.validators.create --- stubs/jsonschema/jsonschema/validators.pyi | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/stubs/jsonschema/jsonschema/validators.pyi b/stubs/jsonschema/jsonschema/validators.pyi index 33ddc0bdc91e..e4370021f656 100644 --- a/stubs/jsonschema/jsonschema/validators.pyi +++ b/stubs/jsonschema/jsonschema/validators.pyi @@ -1,9 +1,17 @@ from _typeshed import SupportsKeysAndGetItem -from collections.abc import Callable, Generator, Iterable, Mapping +from collections.abc import Callable, Generator, Iterable, Iterator, Mapping from typing import Any, ClassVar from typing_extensions import TypeAlias +from ._format import FormatChecker +from ._types import TypeChecker from ._utils import URIDict +from .exceptions import ValidationError + +# these type aliases do not exist at runtime, they're only defined here in the stub +_JSON_OBJECT: TypeAlias = Mapping[str, Any] +_JSON_VALUE: TypeAlias = _JSON_OBJECT | list[Any] | str | int | float | bool | None +_ValidatorCallback: TypeAlias = Callable[[Any, Any, _JSON_VALUE, _JSON_OBJECT], Iterator[ValidationError]] _Schema: TypeAlias = Mapping[str, Any] @@ -33,13 +41,13 @@ class _Validator: def validates(version: str) -> Callable[..., Any]: ... def create( - meta_schema, - validators=..., + meta_schema: Mapping[str, Any], + validators: Mapping[str, _ValidatorCallback] | tuple[()] = ..., version: Any | None = ..., - type_checker=..., - format_checker=..., - id_of=..., - applicable_validators=..., + type_checker: TypeChecker = ..., + format_checker: FormatChecker = ..., + id_of: Callable[[bool | _JSON_OBJECT], str] = ..., + applicable_validators: Callable[[_JSON_OBJECT], Iterable[tuple[str, _ValidatorCallback]]] = ..., ) -> type[_Validator]: ... def extend( validator, validators=..., version: Any | None = ..., type_checker: Any | None = ..., format_checker: Any | None = ... From 817eff4fbbf3a0127a9f80ce367c80cbe7309fc4 Mon Sep 17 00:00:00 2001 From: Stephen Rosen Date: Thu, 25 Aug 2022 13:15:55 +0000 Subject: [PATCH 2/2] Apply feedback per review --- stubs/jsonschema/jsonschema/validators.pyi | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/stubs/jsonschema/jsonschema/validators.pyi b/stubs/jsonschema/jsonschema/validators.pyi index e4370021f656..c95dc0befa05 100644 --- a/stubs/jsonschema/jsonschema/validators.pyi +++ b/stubs/jsonschema/jsonschema/validators.pyi @@ -9,9 +9,9 @@ from ._utils import URIDict from .exceptions import ValidationError # these type aliases do not exist at runtime, they're only defined here in the stub -_JSON_OBJECT: TypeAlias = Mapping[str, Any] -_JSON_VALUE: TypeAlias = _JSON_OBJECT | list[Any] | str | int | float | bool | None -_ValidatorCallback: TypeAlias = Callable[[Any, Any, _JSON_VALUE, _JSON_OBJECT], Iterator[ValidationError]] +_JsonObject: TypeAlias = Mapping[str, Any] +_JsonValue: TypeAlias = _JsonObject | list[Any] | str | int | float | bool | None +_ValidatorCallback: TypeAlias = Callable[[Any, Any, _JsonValue, _JsonObject], Iterator[ValidationError]] _Schema: TypeAlias = Mapping[str, Any] @@ -41,13 +41,13 @@ class _Validator: def validates(version: str) -> Callable[..., Any]: ... def create( - meta_schema: Mapping[str, Any], + meta_schema: _Schema, validators: Mapping[str, _ValidatorCallback] | tuple[()] = ..., version: Any | None = ..., type_checker: TypeChecker = ..., format_checker: FormatChecker = ..., - id_of: Callable[[bool | _JSON_OBJECT], str] = ..., - applicable_validators: Callable[[_JSON_OBJECT], Iterable[tuple[str, _ValidatorCallback]]] = ..., + id_of: Callable[[_Schema], str] = ..., + applicable_validators: Callable[[_Schema], Iterable[tuple[str, _ValidatorCallback]]] = ..., ) -> type[_Validator]: ... def extend( validator, validators=..., version: Any | None = ..., type_checker: Any | None = ..., format_checker: Any | None = ...