6
6
from collections import defaultdict , deque
7
7
from pprint import pformat
8
8
from textwrap import dedent , indent
9
- from typing import ClassVar
9
+ from typing import TYPE_CHECKING , Any , ClassVar
10
10
import heapq
11
11
import itertools
12
12
13
13
import attr
14
14
15
15
from jsonschema import _utils
16
16
17
+ if TYPE_CHECKING :
18
+ from jsonschema import _types
19
+
17
20
WEAK_MATCHES : frozenset [str ] = frozenset (["anyOf" , "oneOf" ])
18
21
STRONG_MATCHES : frozenset [str ] = frozenset ()
19
22
@@ -66,10 +69,10 @@ def __init__(
66
69
for error in context :
67
70
error .parent = self
68
71
69
- def __repr__ (self ):
72
+ def __repr__ (self ) -> str :
70
73
return f"<{ self .__class__ .__name__ } : { self .message !r} >"
71
74
72
- def __str__ (self ):
75
+ def __str__ (self ) -> str :
73
76
essential_for_verbose = (
74
77
self .validator , self .validator_value , self .instance , self .schema ,
75
78
)
@@ -99,11 +102,11 @@ def __str__(self):
99
102
)
100
103
101
104
@classmethod
102
- def create_from (cls , other ):
105
+ def create_from (cls , other : _Error ):
103
106
return cls (** other ._contents ())
104
107
105
108
@property
106
- def absolute_path (self ):
109
+ def absolute_path (self ) -> deque [ str | int ] :
107
110
parent = self .parent
108
111
if parent is None :
109
112
return self .relative_path
@@ -113,7 +116,7 @@ def absolute_path(self):
113
116
return path
114
117
115
118
@property
116
- def absolute_schema_path (self ):
119
+ def absolute_schema_path (self ) -> deque [ str | int ] :
117
120
parent = self .parent
118
121
if parent is None :
119
122
return self .relative_schema_path
@@ -123,7 +126,7 @@ def absolute_schema_path(self):
123
126
return path
124
127
125
128
@property
126
- def json_path (self ):
129
+ def json_path (self ) -> str :
127
130
path = "$"
128
131
for elem in self .absolute_path :
129
132
if isinstance (elem , int ):
@@ -132,7 +135,11 @@ def json_path(self):
132
135
path += "." + elem
133
136
return path
134
137
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 :
136
143
if type_checker is not None and self ._type_checker is _unset :
137
144
self ._type_checker = type_checker
138
145
@@ -188,7 +195,7 @@ class RefResolutionError(Exception):
188
195
189
196
_cause = attr .ib ()
190
197
191
- def __str__ (self ):
198
+ def __str__ (self ) -> str :
192
199
return str (self ._cause )
193
200
194
201
@@ -197,10 +204,10 @@ class UndefinedTypeCheck(Exception):
197
204
A type checker was asked to check a type it did not have registered.
198
205
"""
199
206
200
- def __init__ (self , type ) :
207
+ def __init__ (self , type : str ) -> None :
201
208
self .type = type
202
209
203
- def __str__ (self ):
210
+ def __str__ (self ) -> str :
204
211
return f"Type { self .type !r} is unknown to this type checker"
205
212
206
213
0 commit comments