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
10
import heapq
10
11
import itertools
11
12
22
23
class _Error (Exception ):
23
24
def __init__ (
24
25
self ,
25
- message ,
26
+ message : str ,
26
27
validator = _unset ,
27
28
path = (),
28
29
cause = None ,
@@ -64,34 +65,8 @@ def __init__(
64
65
def __repr__ (self ):
65
66
return f"<{ self .__class__ .__name__ } : { self .message !r} >"
66
67
67
- def __str__ (self ):
68
- essential_for_verbose = (
69
- self .validator , self .validator_value , self .instance , self .schema ,
70
- )
71
- if any (m is _unset for m in essential_for_verbose ):
72
- return self .message
73
-
74
- schema_path = _utils .format_as_index (
75
- container = self ._word_for_schema_in_error_message ,
76
- indices = list (self .relative_schema_path )[:- 1 ],
77
- )
78
- instance_path = _utils .format_as_index (
79
- container = self ._word_for_instance_in_error_message ,
80
- indices = self .relative_path ,
81
- )
82
- prefix = 16 * " "
83
-
84
- return dedent (
85
- f"""\
86
- { self .message }
87
-
88
- Failed validating { self .validator !r} in { schema_path } :
89
- { indent (pformat (self .schema , width = 72 ), prefix ).lstrip ()}
90
-
91
- On { instance_path } :
92
- { indent (pformat (self .instance , width = 72 ), prefix ).lstrip ()}
93
- """ .rstrip (),
94
- )
68
+ def __str__ (self ) -> str :
69
+ return self .message
95
70
96
71
@classmethod
97
72
def create_from (cls , other ):
@@ -137,8 +112,16 @@ def _set(self, type_checker=None, **kwargs):
137
112
138
113
def _contents (self ):
139
114
attrs = (
140
- "message" , "cause" , "context" , "validator" , "validator_value" ,
141
- "path" , "schema_path" , "instance" , "schema" , "parent" ,
115
+ "message" ,
116
+ "cause" ,
117
+ "context" ,
118
+ "validator" ,
119
+ "validator_value" ,
120
+ "path" ,
121
+ "schema_path" ,
122
+ "instance" ,
123
+ "schema" ,
124
+ "parent" ,
142
125
)
143
126
return dict ((attr , getattr (self , attr )) for attr in attrs )
144
127
@@ -157,7 +140,44 @@ def _matches_type(self):
157
140
)
158
141
159
142
160
- class ValidationError (_Error ):
143
+ class _VerboseError (_Error ):
144
+ _word_for_schema_in_error_message : ClassVar [str ]
145
+ _word_for_instance_in_error_message : ClassVar [str ]
146
+
147
+ def __str__ (self ):
148
+ essential_for_verbose = (
149
+ self .validator ,
150
+ self .validator_value ,
151
+ self .instance ,
152
+ self .schema ,
153
+ )
154
+ if any (m is _unset for m in essential_for_verbose ):
155
+ return self .message
156
+
157
+ schema_path = _utils .format_as_index (
158
+ container = self ._word_for_schema_in_error_message ,
159
+ indices = list (self .relative_schema_path )[:- 1 ],
160
+ )
161
+ instance_path = _utils .format_as_index (
162
+ container = self ._word_for_instance_in_error_message ,
163
+ indices = self .relative_path ,
164
+ )
165
+ prefix = 16 * " "
166
+
167
+ return dedent (
168
+ f"""\
169
+ { self .message }
170
+
171
+ Failed validating { self .validator !r} in { schema_path } :
172
+ { indent (pformat (self .schema , width = 72 ), prefix ).lstrip ()}
173
+
174
+ On { instance_path } :
175
+ { indent (pformat (self .instance , width = 72 ), prefix ).lstrip ()}
176
+ """ .rstrip (),
177
+ )
178
+
179
+
180
+ class ValidationError (_VerboseError ):
161
181
"""
162
182
An instance was invalid under a provided schema.
163
183
"""
@@ -166,7 +186,7 @@ class ValidationError(_Error):
166
186
_word_for_instance_in_error_message = "instance"
167
187
168
188
169
- class SchemaError (_Error ):
189
+ class SchemaError (_VerboseError ):
170
190
"""
171
191
A schema was invalid under its corresponding metaschema.
172
192
"""
@@ -328,6 +348,7 @@ def by_relevance(weak=WEAK_MATCHES, strong=STRONG_MATCHES):
328
348
a collection of validation keywords to consider to be
329
349
"strong"
330
350
"""
351
+
331
352
def relevance (error ):
332
353
validator = error .validator
333
354
return (
@@ -336,6 +357,7 @@ def relevance(error):
336
357
validator in strong ,
337
358
not error ._matches_type (),
338
359
)
360
+
339
361
return relevance
340
362
341
363
0 commit comments