Skip to content

Commit f52da1e

Browse files
authored
Flesh out more of jsonschema stubs (#7950)
Apply more detailed annotations to the format module and most of the exceptions module.
1 parent c35ec8b commit f52da1e

File tree

2 files changed

+68
-52
lines changed

2 files changed

+68
-52
lines changed
+32-28
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
from collections.abc import Iterable
2-
from typing import Any
1+
from collections.abc import Callable, Iterable
2+
from typing import Any, TypeVar
3+
4+
_F = TypeVar("_F", bound=Callable[..., Any])
35

46
class FormatChecker:
5-
checkers: Any
7+
checkers: dict[str, tuple[Callable[[Any], bool], Exception | tuple[Exception, ...]]]
8+
69
def __init__(self, formats: Iterable[str] | None = ...) -> None: ...
7-
def checks(self, format, raises=...): ...
8-
cls_checks: Any
9-
def check(self, instance, format) -> None: ...
10-
def conforms(self, instance, format) -> bool: ...
10+
def checks(self, format: str, raises: Exception | tuple[Exception, ...] = ...) -> Callable[[_F], _F]: ...
11+
@classmethod
12+
def cls_checks(cls, format: str, raises: Exception | tuple[Exception, ...] = ...) -> Callable[[_F], _F]: ...
13+
def check(self, instance: Any, format: str) -> None: ...
14+
def conforms(self, instance: Any, format: str) -> bool: ...
1115

1216
draft3_format_checker: FormatChecker
1317
draft4_format_checker: FormatChecker
@@ -16,28 +20,28 @@ draft7_format_checker: FormatChecker
1620
draft201909_format_checker: FormatChecker
1721
draft202012_format_checker: FormatChecker
1822

19-
def is_email(instance) -> bool: ...
20-
def is_ipv4(instance) -> bool: ...
21-
def is_ipv6(instance) -> bool: ...
23+
def is_email(instance: object) -> bool: ...
24+
def is_ipv4(instance: object) -> bool: ...
25+
def is_ipv6(instance: object) -> bool: ...
2226

2327
# is_host_name is only defined if fqdn is installed.
24-
def is_host_name(instance) -> bool: ...
25-
def is_idn_host_name(instance) -> bool: ...
26-
def is_uri(instance) -> bool: ...
27-
def is_uri_reference(instance) -> bool: ...
28-
def is_iri(instance) -> bool: ...
29-
def is_iri_reference(instance) -> bool: ...
30-
def is_datetime(instance) -> bool: ...
31-
def is_time(instance) -> bool: ...
32-
def is_regex(instance) -> bool: ...
33-
def is_date(instance) -> bool: ...
34-
def is_draft3_time(instance) -> bool: ...
35-
def is_css_color_code(instance) -> bool: ...
36-
def is_css21_color(instance) -> bool: ...
37-
def is_json_pointer(instance) -> bool: ...
38-
def is_relative_json_pointer(instance) -> bool: ...
39-
def is_uri_template(instance) -> bool: ...
28+
def is_host_name(instance: object) -> bool: ...
29+
def is_idn_host_name(instance: object) -> bool: ...
30+
def is_uri(instance: object) -> bool: ...
31+
def is_uri_reference(instance: object) -> bool: ...
32+
def is_iri(instance: object) -> bool: ...
33+
def is_iri_reference(instance: object) -> bool: ...
34+
def is_datetime(instance: object) -> bool: ...
35+
def is_time(instance: object) -> bool: ...
36+
def is_regex(instance: object) -> bool: ...
37+
def is_date(instance: object) -> bool: ...
38+
def is_draft3_time(instance: object) -> bool: ...
39+
def is_css_color_code(instance: object) -> bool: ...
40+
def is_css21_color(instance: object) -> bool: ...
41+
def is_json_pointer(instance: object) -> bool: ...
42+
def is_relative_json_pointer(instance: object) -> bool: ...
43+
def is_uri_template(instance: object) -> bool: ...
4044

4145
# is_duration is only defined if isoduration is installed.
42-
def is_duration(instance) -> bool: ...
43-
def is_uuid(instance) -> bool: ...
46+
def is_duration(instance: object) -> bool: ...
47+
def is_uuid(instance: object) -> bool: ...

stubs/jsonschema/jsonschema/exceptions.pyi

+36-24
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,52 @@
1+
from _typeshed import Self, SupportsRichComparison
2+
from collections import deque
3+
from collections.abc import Callable, Container, Iterable, Sequence
14
from typing import Any
25

3-
WEAK_MATCHES: Any
4-
STRONG_MATCHES: Any
6+
from jsonschema import _utils, protocols
7+
8+
_RelevanceFuncType = Callable[[ValidationError], SupportsRichComparison]
9+
10+
WEAK_MATCHES: frozenset[str]
11+
STRONG_MATCHES: frozenset[str]
512

613
class _Error(Exception):
7-
message: Any
8-
path: Any
9-
schema_path: Any
10-
context: Any
11-
cause: Any
12-
validator: Any
14+
message: str
15+
path: deque[str]
16+
relative_path: deque[str]
17+
schema_path: deque[str]
18+
relative_schema_path: deque[str]
19+
context: list[ValidationError] | None
20+
cause: Exception | None
21+
validator: protocols.Validator | None
1322
validator_value: Any
1423
instance: Any
1524
schema: Any
16-
parent: Any
25+
parent: _Error | None
1726
def __init__(
1827
self,
19-
message,
20-
validator=...,
21-
path=...,
28+
message: str,
29+
validator: _utils.Unset | None | protocols.Validator = ...,
30+
path: Sequence[str] = ...,
2231
cause: Any | None = ...,
23-
context=...,
32+
context: Sequence[ValidationError] = ...,
2433
validator_value=...,
25-
instance=...,
26-
schema=...,
27-
schema_path=...,
28-
parent: Any | None = ...,
34+
instance: Any = ...,
35+
schema: Any = ...,
36+
schema_path: Sequence[str] = ...,
37+
parent: _Error | None = ...,
2938
) -> None: ...
3039
@classmethod
31-
def create_from(cls, other): ...
40+
def create_from(cls: type[Self], other: _Error) -> Self: ...
3241
@property
33-
def absolute_path(self): ...
42+
def absolute_path(self) -> Sequence[str]: ...
3443
@property
35-
def absolute_schema_path(self): ...
44+
def absolute_schema_path(self) -> Sequence[str]: ...
3645
@property
37-
def json_path(self): ...
46+
def json_path(self) -> str: ...
47+
# TODO: this type could be made more precise using TypedDict to
48+
# enumerate the types of the members
49+
def _contents(self) -> dict[str, Any]: ...
3850

3951
class ValidationError(_Error): ...
4052
class SchemaError(_Error): ...
@@ -68,8 +80,8 @@ class ErrorTree:
6880
@property
6981
def total_errors(self): ...
7082

71-
def by_relevance(weak=..., strong=...): ...
83+
def by_relevance(weak: Container[str] = ..., strong: Container[str] = ...) -> _RelevanceFuncType: ...
7284

73-
relevance: Any
85+
relevance: _RelevanceFuncType
7486

75-
def best_match(errors, key=...): ...
87+
def best_match(errors: Iterable[ValidationError], key: _RelevanceFuncType = ...): ...

0 commit comments

Comments
 (0)