-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Flesh out more of jsonschema stubs #7950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,52 @@ | ||
from _typeshed import Self, SupportsRichComparison | ||
from collections import deque | ||
from collections.abc import Callable, Container, Iterable, Sequence | ||
from typing import Any | ||
|
||
WEAK_MATCHES: Any | ||
STRONG_MATCHES: Any | ||
from jsonschema import _utils, protocols | ||
|
||
_RelevanceFuncType = Callable[[ValidationError], SupportsRichComparison] | ||
|
||
WEAK_MATCHES: frozenset[str] | ||
STRONG_MATCHES: frozenset[str] | ||
|
||
class _Error(Exception): | ||
message: Any | ||
path: Any | ||
schema_path: Any | ||
context: Any | ||
cause: Any | ||
validator: Any | ||
message: str | ||
path: deque[str] | ||
relative_path: deque[str] | ||
schema_path: deque[str] | ||
relative_schema_path: deque[str] | ||
context: list[ValidationError] | None | ||
cause: Exception | None | ||
validator: protocols.Validator | None | ||
validator_value: Any | ||
instance: Any | ||
schema: Any | ||
parent: Any | ||
parent: _Error | None | ||
def __init__( | ||
self, | ||
message, | ||
validator=..., | ||
path=..., | ||
message: str, | ||
validator: _utils.Unset | None | protocols.Validator = ..., | ||
path: Sequence[str] = ..., | ||
cause: Any | None = ..., | ||
context=..., | ||
context: Sequence[ValidationError] = ..., | ||
validator_value=..., | ||
instance=..., | ||
schema=..., | ||
schema_path=..., | ||
parent: Any | None = ..., | ||
instance: Any = ..., | ||
schema: Any = ..., | ||
schema_path: Sequence[str] = ..., | ||
parent: _Error | None = ..., | ||
) -> None: ... | ||
@classmethod | ||
def create_from(cls, other): ... | ||
def create_from(cls: type[Self], other: _Error) -> Self: ... | ||
@property | ||
def absolute_path(self): ... | ||
def absolute_path(self) -> Sequence[str]: ... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is incorrect (at least with jsonschema 4.4.0) because path entries may be indexes into a JSON array. For example: import jsonschema
SCHEMA = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "array",
"items": {"type": "string"},
}
document = ["a", 12345, "c", "d"]
try:
jsonschema.validate(document, SCHEMA)
except jsonschema.ValidationError as e:
print(repr(e))
for path_entry in e.absolute_path:
print(repr(path_entry), type(path_entry))
I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! Apologies for the oversight. These can be ints in precisely the case you mention. I'll double-check for other cases of this. (I'm not a typeshed maintainer, but I'll also go 👍 your PR.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not to worry, glad it was straightforward to sort out! |
||
@property | ||
def absolute_schema_path(self): ... | ||
def absolute_schema_path(self) -> Sequence[str]: ... | ||
@property | ||
def json_path(self): ... | ||
def json_path(self) -> str: ... | ||
# TODO: this type could be made more precise using TypedDict to | ||
# enumerate the types of the members | ||
def _contents(self) -> dict[str, Any]: ... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be a bit involved, but you could consider returning a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to punt on this for now, and just add a TODO comment to make the type more precise with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure |
||
|
||
class ValidationError(_Error): ... | ||
class SchemaError(_Error): ... | ||
|
@@ -68,8 +80,8 @@ class ErrorTree: | |
@property | ||
def total_errors(self): ... | ||
|
||
def by_relevance(weak=..., strong=...): ... | ||
def by_relevance(weak: Container[str] = ..., strong: Container[str] = ...) -> _RelevanceFuncType: ... | ||
|
||
relevance: Any | ||
relevance: _RelevanceFuncType | ||
|
||
def best_match(errors, key=...): ... | ||
def best_match(errors: Iterable[ValidationError], key: _RelevanceFuncType = ...): ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally prefer to be as precise as possible with return types in typeshed, so that would imply using
deque[str]
here instead ofSequence[str]
. If you think it's an implementation detail which precise type is returned here, however, then you may want to ignore me here :)The same point applies to
absolute_schema_path
below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I considered annotating it as
deque
-- that was in my first draft -- but then decided exactly this. Callers should not be relying on that value being adeque
IMO.In a way, your comment helps validate my decision. It means I was thinking the right kinds of things!