Skip to content

Commit e2c3a06

Browse files
committed
Add typing to some methods of _Error
1 parent 300b095 commit e2c3a06

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

jsonschema/exceptions.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
from collections import defaultdict, deque
77
from pprint import pformat
88
from textwrap import dedent, indent
9-
from typing import ClassVar
9+
from typing import TYPE_CHECKING, Any, ClassVar
1010
import heapq
1111
import itertools
1212

1313
import attr
1414

1515
from jsonschema import _utils
1616

17+
if TYPE_CHECKING:
18+
from jsonschema import _types
19+
1720
WEAK_MATCHES: frozenset[str] = frozenset(["anyOf", "oneOf"])
1821
STRONG_MATCHES: frozenset[str] = frozenset()
1922

@@ -66,10 +69,10 @@ def __init__(
6669
for error in context:
6770
error.parent = self
6871

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

72-
def __str__(self):
75+
def __str__(self) -> str:
7376
essential_for_verbose = (
7477
self.validator, self.validator_value, self.instance, self.schema,
7578
)
@@ -99,11 +102,11 @@ def __str__(self):
99102
)
100103

101104
@classmethod
102-
def create_from(cls, other):
105+
def create_from(cls, other: _Error):
103106
return cls(**other._contents())
104107

105108
@property
106-
def absolute_path(self):
109+
def absolute_path(self) -> deque[str | int]:
107110
parent = self.parent
108111
if parent is None:
109112
return self.relative_path
@@ -113,7 +116,7 @@ def absolute_path(self):
113116
return path
114117

115118
@property
116-
def absolute_schema_path(self):
119+
def absolute_schema_path(self) -> deque[str | int]:
117120
parent = self.parent
118121
if parent is None:
119122
return self.relative_schema_path
@@ -123,7 +126,7 @@ def absolute_schema_path(self):
123126
return path
124127

125128
@property
126-
def json_path(self):
129+
def json_path(self) -> str:
127130
path = "$"
128131
for elem in self.absolute_path:
129132
if isinstance(elem, int):
@@ -132,7 +135,11 @@ def json_path(self):
132135
path += "." + elem
133136
return path
134137

135-
def _set(self, type_checker=None, **kwargs):
138+
def _set(
139+
self,
140+
type_checker: _types.TypeChecker | None = None,
141+
**kwargs: Any,
142+
) -> None:
136143
if type_checker is not None and self._type_checker is _unset:
137144
self._type_checker = type_checker
138145

@@ -188,7 +195,7 @@ class RefResolutionError(Exception):
188195

189196
_cause = attr.ib()
190197

191-
def __str__(self):
198+
def __str__(self) -> str:
192199
return str(self._cause)
193200

194201

@@ -197,10 +204,10 @@ class UndefinedTypeCheck(Exception):
197204
A type checker was asked to check a type it did not have registered.
198205
"""
199206

200-
def __init__(self, type):
207+
def __init__(self, type: str) -> None:
201208
self.type = type
202209

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

206213

0 commit comments

Comments
 (0)