From e2c3a06301b119b5749968a55e494ee2cabf0ca3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Thu, 22 Dec 2022 09:35:11 +0100 Subject: [PATCH 1/3] Add typing to some methods of ``_Error`` --- jsonschema/exceptions.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index 149d8389..e46d3def 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -6,7 +6,7 @@ from collections import defaultdict, deque from pprint import pformat from textwrap import dedent, indent -from typing import ClassVar +from typing import TYPE_CHECKING, Any, ClassVar import heapq import itertools @@ -14,6 +14,9 @@ from jsonschema import _utils +if TYPE_CHECKING: + from jsonschema import _types + WEAK_MATCHES: frozenset[str] = frozenset(["anyOf", "oneOf"]) STRONG_MATCHES: frozenset[str] = frozenset() @@ -66,10 +69,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, ) @@ -99,11 +102,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 @@ -113,7 +116,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 @@ -123,7 +126,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): @@ -132,7 +135,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 @@ -188,7 +195,7 @@ class RefResolutionError(Exception): _cause = attr.ib() - def __str__(self): + def __str__(self) -> str: return str(self._cause) @@ -197,10 +204,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" From efa28c690074fe597aea039caa8831b45cf11102 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Thu, 22 Dec 2022 09:54:35 +0100 Subject: [PATCH 2/3] Add typing and check to ``_Error._matches_type`` --- jsonschema/exceptions.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index e46d3def..c3a1aa2c 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -154,12 +154,16 @@ def _contents(self): ) return dict((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) From 7fd4d38f3bc124e3b493443431c85a75cabf3b63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Thu, 22 Dec 2022 09:54:45 +0100 Subject: [PATCH 3/3] Add typing to ``_Error.__init__`` --- jsonschema/exceptions.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py index c3a1aa2c..29863232 100644 --- a/jsonschema/exceptions.py +++ b/jsonschema/exceptions.py @@ -4,6 +4,7 @@ from __future__ import annotations from collections import defaultdict, deque +from collections.abc import Iterable, Mapping from pprint import pformat from textwrap import dedent, indent from typing import TYPE_CHECKING, Any, ClassVar @@ -31,17 +32,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, - ): + 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(_Error, self).__init__( message, validator,