Skip to content

Commit 88fde5e

Browse files
committed
Adopt typing information from typeshed (PEP-561)
Avoids problems with mypy by officially declaring the library as typed and copies the current type information from types-jsonschema. From now on the library itself is typed and we will be able to ensure that this types are correct.
1 parent b1c1d00 commit 88fde5e

13 files changed

+505
-0
lines changed

jsonschema/__init__.pyi

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from jsonschema._format import (
2+
FormatChecker as FormatChecker,
3+
draft3_format_checker as draft3_format_checker,
4+
draft4_format_checker as draft4_format_checker,
5+
draft6_format_checker as draft6_format_checker,
6+
draft7_format_checker as draft7_format_checker,
7+
draft201909_format_checker as draft201909_format_checker,
8+
draft202012_format_checker as draft202012_format_checker,
9+
)
10+
from jsonschema._types import TypeChecker as TypeChecker
11+
from jsonschema.exceptions import (
12+
ErrorTree as ErrorTree,
13+
FormatError as FormatError,
14+
RefResolutionError as RefResolutionError,
15+
SchemaError as SchemaError,
16+
ValidationError as ValidationError,
17+
)
18+
from jsonschema.protocols import Validator as Validator
19+
from jsonschema.validators import (
20+
Draft3Validator as Draft3Validator,
21+
Draft4Validator as Draft4Validator,
22+
Draft6Validator as Draft6Validator,
23+
Draft7Validator as Draft7Validator,
24+
Draft201909Validator as Draft201909Validator,
25+
Draft202012Validator as Draft202012Validator,
26+
RefResolver as RefResolver,
27+
validate as validate,
28+
)

jsonschema/_format.pyi

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from collections.abc import Callable, Iterable
2+
from typing import Any, TypeVar, Union
3+
from typing_extensions import TypeAlias
4+
5+
_F = TypeVar("_F", bound=Callable[..., Any])
6+
_RaisesType: TypeAlias = Union[type[Exception], tuple[type[Exception], ...]]
7+
8+
class FormatChecker:
9+
checkers: dict[str, tuple[Callable[[Any], bool], _RaisesType]]
10+
11+
def __init__(self, formats: Iterable[str] | None = ...) -> None: ...
12+
def checks(self, format: str, raises: _RaisesType = ...) -> Callable[[_F], _F]: ...
13+
@classmethod
14+
def cls_checks(cls, format: str, raises: _RaisesType = ...) -> Callable[[_F], _F]: ...
15+
def check(self, instance: Any, format: str) -> None: ...
16+
def conforms(self, instance: Any, format: str) -> bool: ...
17+
18+
draft3_format_checker: FormatChecker
19+
draft4_format_checker: FormatChecker
20+
draft6_format_checker: FormatChecker
21+
draft7_format_checker: FormatChecker
22+
draft201909_format_checker: FormatChecker
23+
draft202012_format_checker: FormatChecker
24+
25+
def is_email(instance: object) -> bool: ...
26+
def is_ipv4(instance: object) -> bool: ...
27+
def is_ipv6(instance: object) -> bool: ...
28+
29+
# is_host_name is only defined if fqdn is installed.
30+
def is_host_name(instance: object) -> bool: ...
31+
def is_idn_host_name(instance: object) -> bool: ...
32+
def is_uri(instance: object) -> bool: ...
33+
def is_uri_reference(instance: object) -> bool: ...
34+
def is_iri(instance: object) -> bool: ...
35+
def is_iri_reference(instance: object) -> bool: ...
36+
def is_datetime(instance: object) -> bool: ...
37+
def is_time(instance: object) -> bool: ...
38+
def is_regex(instance: object) -> bool: ...
39+
def is_date(instance: object) -> bool: ...
40+
def is_draft3_time(instance: object) -> bool: ...
41+
def is_css_color_code(instance: object) -> bool: ...
42+
def is_css21_color(instance: object) -> bool: ...
43+
def is_json_pointer(instance: object) -> bool: ...
44+
def is_relative_json_pointer(instance: object) -> bool: ...
45+
def is_uri_template(instance: object) -> bool: ...
46+
47+
# is_duration is only defined if isoduration is installed.
48+
def is_duration(instance: object) -> bool: ...
49+
def is_uuid(instance: object) -> bool: ...

jsonschema/_legacy_validators.pyi

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from collections.abc import ItemsView
2+
from typing import Any
3+
4+
def ignore_ref_siblings(schema) -> list[tuple[str, Any]] | ItemsView[str, Any]: ...
5+
def dependencies_draft3(validator, dependencies, instance, schema) -> None: ...
6+
def dependencies_draft4_draft6_draft7(validator, dependencies, instance, schema) -> None: ...
7+
def disallow_draft3(validator, disallow, instance, schema) -> None: ...
8+
def extends_draft3(validator, extends, instance, schema) -> None: ...
9+
def items_draft3_draft4(validator, items, instance, schema) -> None: ...
10+
def items_draft6_draft7_draft201909(validator, items, instance, schema) -> None: ...
11+
def minimum_draft3_draft4(validator, minimum, instance, schema) -> None: ...
12+
def maximum_draft3_draft4(validator, maximum, instance, schema) -> None: ...
13+
def properties_draft3(validator, properties, instance, schema) -> None: ...
14+
def type_draft3(validator, types, instance, schema) -> None: ...
15+
def contains_draft6_draft7(validator, contains, instance, schema) -> None: ...
16+
def recursiveRef(validator, recursiveRef, instance, schema) -> None: ...

jsonschema/_reflect.pyi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class _NoModuleFound(Exception): ...
2+
class InvalidName(ValueError): ...
3+
class ModuleNotFound(InvalidName): ...
4+
class ObjectNotFound(InvalidName): ...
5+
6+
def reraise(exception, traceback) -> None: ...
7+
def namedAny(name): ...

jsonschema/_types.pyi

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from collections.abc import Callable, Iterable, Mapping
2+
3+
def is_array(checker, instance) -> bool: ...
4+
def is_bool(checker, instance) -> bool: ...
5+
def is_integer(checker, instance) -> bool: ...
6+
def is_null(checker, instance) -> bool: ...
7+
def is_number(checker, instance) -> bool: ...
8+
def is_object(checker, instance) -> bool: ...
9+
def is_string(checker, instance) -> bool: ...
10+
def is_any(checker, instance) -> bool: ...
11+
12+
class TypeChecker:
13+
def __init__(self, type_checkers: Mapping[str, Callable[[object], bool]] = ...) -> None: ...
14+
def is_type(self, instance, type: str) -> bool: ...
15+
def redefine(self, type: str, fn: Callable[..., bool]) -> TypeChecker: ...
16+
def redefine_many(self, definitions=...) -> TypeChecker: ...
17+
def remove(self, *types: Iterable[str]) -> TypeChecker: ...
18+
19+
draft3_type_checker: TypeChecker
20+
draft4_type_checker: TypeChecker
21+
draft6_type_checker: TypeChecker
22+
draft7_type_checker: TypeChecker
23+
draft201909_type_checker: TypeChecker
24+
draft202012_type_checker: TypeChecker

jsonschema/_utils.pyi

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from _typeshed import SupportsKeysAndGetItem
2+
from collections.abc import Generator, Iterable, Iterator, Mapping, MutableMapping, Sized
3+
from typing import Any
4+
5+
class URIDict(MutableMapping[str, str]):
6+
def normalize(self, uri: str) -> str: ...
7+
store: dict[str, str]
8+
def __init__(self, __m: SupportsKeysAndGetItem[str, str] | Iterable[tuple[str, str]], **kwargs: str) -> None: ...
9+
def __getitem__(self, uri: str) -> str: ...
10+
def __setitem__(self, uri: str, value: str) -> None: ...
11+
def __delitem__(self, uri: str) -> None: ...
12+
def __iter__(self) -> Iterator[str]: ...
13+
def __len__(self) -> int: ...
14+
15+
class Unset: ...
16+
17+
def load_schema(name): ...
18+
def format_as_index(container: str, indices) -> str: ...
19+
def find_additional_properties(instance: Iterable[Any], schema: Mapping[Any, Any]) -> Generator[Any, None, None]: ...
20+
def extras_msg(extras: Iterable[Any] | Sized) -> str: ...
21+
def ensure_list(thing) -> list[Any]: ...
22+
def equal(one, two) -> bool: ...
23+
def unbool(element, true=..., false=...): ...
24+
def uniq(container) -> bool: ...
25+
def find_evaluated_item_indexes_by_schema(validator, instance, schema) -> list[Any]: ...
26+
def find_evaluated_property_keys_by_schema(validator, instance, schema) -> list[Any]: ...

jsonschema/_validators.pyi

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
def patternProperties(validator, patternProperties, instance, schema) -> None: ...
2+
def propertyNames(validator, propertyNames, instance, schema) -> None: ...
3+
def additionalProperties(validator, aP, instance, schema) -> None: ...
4+
def items(validator, items, instance, schema) -> None: ...
5+
def additionalItems(validator, aI, instance, schema) -> None: ...
6+
def const(validator, const, instance, schema) -> None: ...
7+
def contains(validator, contains, instance, schema) -> None: ...
8+
def exclusiveMinimum(validator, minimum, instance, schema) -> None: ...
9+
def exclusiveMaximum(validator, maximum, instance, schema) -> None: ...
10+
def minimum(validator, minimum, instance, schema) -> None: ...
11+
def maximum(validator, maximum, instance, schema) -> None: ...
12+
def multipleOf(validator, dB, instance, schema) -> None: ...
13+
def minItems(validator, mI, instance, schema) -> None: ...
14+
def maxItems(validator, mI, instance, schema) -> None: ...
15+
def uniqueItems(validator, uI, instance, schema) -> None: ...
16+
def pattern(validator, patrn, instance, schema) -> None: ...
17+
def format(validator, format, instance, schema) -> None: ...
18+
def minLength(validator, mL, instance, schema) -> None: ...
19+
def maxLength(validator, mL, instance, schema) -> None: ...
20+
def dependentRequired(validator, dependentRequired, instance, schema) -> None: ...
21+
def dependentSchemas(validator, dependentSchemas, instance, schema) -> None: ...
22+
def enum(validator, enums, instance, schema) -> None: ...
23+
def ref(validator, ref, instance, schema) -> None: ...
24+
def dynamicRef(validator, dynamicRef, instance, schema) -> None: ...
25+
def type(validator, types, instance, schema) -> None: ...
26+
def properties(validator, properties, instance, schema) -> None: ...
27+
def required(validator, required, instance, schema) -> None: ...
28+
def minProperties(validator, mP, instance, schema) -> None: ...
29+
def maxProperties(validator, mP, instance, schema) -> None: ...
30+
def allOf(validator, allOf, instance, schema) -> None: ...
31+
def anyOf(validator, anyOf, instance, schema) -> None: ...
32+
def oneOf(validator, oneOf, instance, schema) -> None: ...
33+
def not_(validator, not_schema, instance, schema) -> None: ...
34+
def if_(validator, if_schema, instance, schema) -> None: ...
35+
def unevaluatedItems(validator, unevaluatedItems, instance, schema) -> None: ...
36+
def unevaluatedProperties(validator, unevaluatedProperties, instance, schema) -> None: ...
37+
def prefixItems(validator, prefixItems, instance, schema) -> None: ...

jsonschema/cli.pyi

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import Any
2+
3+
class _CannotLoadFile(Exception): ...
4+
5+
class _Outputter:
6+
def __init__(self, formatter, stdout, stderr): ...
7+
@classmethod
8+
def from_arguments(cls, arguments, stdout, stderr): ...
9+
def load(self, path): ...
10+
def filenotfound_error(self, **kwargs) -> None: ...
11+
def parsing_error(self, **kwargs) -> None: ...
12+
def validation_error(self, **kwargs) -> None: ...
13+
def validation_success(self, **kwargs) -> None: ...
14+
15+
class _PrettyFormatter:
16+
def filenotfound_error(self, path, exc_info): ...
17+
def parsing_error(self, path, exc_info): ...
18+
def validation_error(self, instance_path, error): ...
19+
def validation_success(self, instance_path): ...
20+
21+
class _PlainFormatter:
22+
def __init__(self, error_format): ...
23+
def filenotfound_error(self, path, exc_info): ...
24+
def parsing_error(self, path, exc_info): ...
25+
def validation_error(self, instance_path, error): ...
26+
def validation_success(self, instance_path): ...
27+
28+
parser: Any
29+
30+
def parse_args(args): ...
31+
def main(args=...) -> None: ...
32+
def run(arguments, stdout=..., stderr=..., stdin=...): ...

jsonschema/exceptions.pyi

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from _typeshed import Self, SupportsRichComparison
2+
from collections import deque
3+
from collections.abc import Callable, Container, Iterable, Sequence
4+
from typing import Any
5+
from typing_extensions import TypeAlias
6+
7+
from jsonschema import _utils, protocols
8+
9+
_RelevanceFuncType: TypeAlias = Callable[[ValidationError], SupportsRichComparison]
10+
11+
WEAK_MATCHES: frozenset[str]
12+
STRONG_MATCHES: frozenset[str]
13+
14+
class _Error(Exception):
15+
message: str
16+
path: deque[str | int]
17+
relative_path: deque[str | int]
18+
schema_path: deque[str | int]
19+
relative_schema_path: deque[str | int]
20+
context: list[ValidationError] | None
21+
cause: Exception | None
22+
validator: protocols.Validator | None
23+
validator_value: Any
24+
instance: Any
25+
schema: Any
26+
parent: _Error | None
27+
def __init__(
28+
self,
29+
message: str,
30+
validator: _utils.Unset | None | protocols.Validator = ...,
31+
path: Sequence[str | int] = ...,
32+
cause: Any | None = ...,
33+
context: Sequence[ValidationError] = ...,
34+
validator_value=...,
35+
instance: Any = ...,
36+
schema: Any = ...,
37+
schema_path: Sequence[str | int] = ...,
38+
parent: _Error | None = ...,
39+
) -> None: ...
40+
@classmethod
41+
def create_from(cls: type[Self], other: _Error) -> Self: ...
42+
@property
43+
def absolute_path(self) -> Sequence[str | int]: ...
44+
@property
45+
def absolute_schema_path(self) -> Sequence[str | int]: ...
46+
@property
47+
def json_path(self) -> str: ...
48+
# TODO: this type could be made more precise using TypedDict to
49+
# enumerate the types of the members
50+
def _contents(self) -> dict[str, Any]: ...
51+
52+
class ValidationError(_Error): ...
53+
class SchemaError(_Error): ...
54+
55+
class RefResolutionError(Exception):
56+
def __init__(self, cause: str) -> None: ...
57+
58+
class UndefinedTypeCheck(Exception):
59+
type: Any
60+
def __init__(self, type) -> None: ...
61+
62+
class UnknownType(Exception):
63+
type: Any
64+
instance: Any
65+
schema: Any
66+
def __init__(self, type, instance, schema) -> None: ...
67+
68+
class FormatError(Exception):
69+
message: Any
70+
cause: Any
71+
def __init__(self, message, cause: Any | None = ...) -> None: ...
72+
73+
class ErrorTree:
74+
errors: Any
75+
def __init__(self, errors=...) -> None: ...
76+
def __contains__(self, index): ...
77+
def __getitem__(self, index): ...
78+
def __setitem__(self, index, value) -> None: ...
79+
def __iter__(self): ...
80+
def __len__(self): ...
81+
@property
82+
def total_errors(self): ...
83+
84+
def by_relevance(weak: Container[str] = ..., strong: Container[str] = ...) -> _RelevanceFuncType: ...
85+
86+
relevance: _RelevanceFuncType
87+
88+
def best_match(errors: Iterable[ValidationError], key: _RelevanceFuncType = ...): ...

jsonschema/protocols.pyi

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from collections.abc import Iterator
2+
from typing import Any, ClassVar, Protocol
3+
4+
from jsonschema._format import FormatChecker
5+
from jsonschema._types import TypeChecker
6+
from jsonschema.exceptions import ValidationError
7+
from jsonschema.validators import RefResolver
8+
9+
class Validator(Protocol):
10+
META_SCHEMA: ClassVar[dict[Any, Any]]
11+
VALIDATORS: ClassVar[dict[Any, Any]]
12+
TYPE_CHECKER: ClassVar[TypeChecker]
13+
schema: dict[Any, Any] | bool
14+
def __init__(
15+
self, schema: dict[Any, Any] | bool, resolver: RefResolver | None = ..., format_checker: FormatChecker | None = ...
16+
) -> None: ...
17+
@classmethod
18+
def check_schema(cls, schema: dict[Any, Any]) -> None: ...
19+
def is_type(self, instance: Any, type: str) -> bool: ...
20+
def is_valid(self, instance: dict[Any, Any]) -> bool: ...
21+
def iter_errors(self, instance: dict[Any, Any]) -> Iterator[ValidationError]: ...
22+
def validate(self, instance: dict[Any, Any]) -> None: ...
23+
def evolve(self, **kwargs) -> Validator: ...

jsonschema/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)