Skip to content

Commit

Permalink
Merge remote-tracking branch 'DanielNoord/typing/base'
Browse files Browse the repository at this point in the history
* DanielNoord/typing/base:
  Add typing to ``_Error.__init__``
  Add typing and check to ``_Error._matches_type``
  Add typing to some methods of ``_Error``
  • Loading branch information
Julian committed Jun 5, 2024
2 parents 940150b + 7fd4d38 commit ceb362a
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions jsonschema/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from collections import defaultdict, deque
from pprint import pformat
from textwrap import dedent, indent
from typing import TYPE_CHECKING, ClassVar
from typing import TYPE_CHECKING, Any, ClassVar
import heapq
import itertools
import warnings
Expand All @@ -19,6 +19,8 @@
if TYPE_CHECKING:
from collections.abc import Iterable, Mapping, MutableMapping

from jsonschema import _types

WEAK_MATCHES: frozenset[str] = frozenset(["anyOf", "oneOf"])
STRONG_MATCHES: frozenset[str] = frozenset()

Expand All @@ -44,17 +46,17 @@ class _Error(Exception):
def __init__(
self,
message: str,
validator=_unset,
path=(),
cause=None,
validator: str | _utils.Unset = _unset,
path: Iterable[str | int] = (),
cause: Exception | None = None,
context=(),
validator_value=_unset,
instance=_unset,
schema=_unset,
schema_path=(),
parent=None,
type_checker=_unset,
):
validator_value: Any = _unset,
instance: Any = _unset,
schema: Mapping[str, Any] | bool | _utils.Unset = _unset,
schema_path: Iterable[str | int] = (),
parent: _Error | None = None,
type_checker: _types.TypeChecker | _utils.Unset = _unset,
) -> None:
super().__init__(
message,
validator,
Expand Down Expand Up @@ -82,10 +84,10 @@ def __init__(
for error in context:
error.parent = self

def __repr__(self):
def __repr__(self) -> str:
return f"<{self.__class__.__name__}: {self.message!r}>"

def __str__(self):
def __str__(self) -> str:
essential_for_verbose = (
self.validator, self.validator_value, self.instance, self.schema,
)
Expand Down Expand Up @@ -115,11 +117,11 @@ def __str__(self):
)

@classmethod
def create_from(cls, other):
def create_from(cls, other: _Error):
return cls(**other._contents())

@property
def absolute_path(self):
def absolute_path(self) -> deque[str | int]:
parent = self.parent
if parent is None:
return self.relative_path
Expand All @@ -129,7 +131,7 @@ def absolute_path(self):
return path

@property
def absolute_schema_path(self):
def absolute_schema_path(self) -> deque[str | int]:
parent = self.parent
if parent is None:
return self.relative_schema_path
Expand All @@ -139,7 +141,7 @@ def absolute_schema_path(self):
return path

@property
def json_path(self):
def json_path(self) -> str:
path = "$"
for elem in self.absolute_path:
if isinstance(elem, int):
Expand All @@ -148,7 +150,11 @@ def json_path(self):
path += "." + elem
return path

def _set(self, type_checker=None, **kwargs):
def _set(
self,
type_checker: _types.TypeChecker | None = None,
**kwargs: Any,
) -> None:
if type_checker is not None and self._type_checker is _unset:
self._type_checker = type_checker

Expand All @@ -163,12 +169,16 @@ def _contents(self):
)
return {attr: getattr(self, attr) for attr in attrs}

def _matches_type(self):
def _matches_type(self) -> bool:
try:
expected = self.schema["type"]
# We ignore this as we want to simply crash if this happens
expected = self.schema["type"] # type: ignore[index]
except (KeyError, TypeError):
return False

if isinstance(self._type_checker, _utils.Unset):
return False

if isinstance(expected, str):
return self._type_checker.is_type(self.instance, expected)

Expand Down Expand Up @@ -215,7 +225,7 @@ def __eq__(self, other):
return NotImplemented # pragma: no cover -- uncovered but deprecated # noqa: E501
return self._cause == other._cause

def __str__(self):
def __str__(self) -> str:
return str(self._cause)


Expand Down Expand Up @@ -248,10 +258,10 @@ class UndefinedTypeCheck(Exception):
A type checker was asked to check a type it did not have registered.
"""

def __init__(self, type):
def __init__(self, type: str) -> None:
self.type = type

def __str__(self):
def __str__(self) -> str:
return f"Type {self.type!r} is unknown to this type checker"


Expand Down

0 comments on commit ceb362a

Please sign in to comment.