21
21
22
22
23
23
class _Error (Exception ):
24
+
25
+ _word_for_schema_in_error_message : ClassVar [str ]
26
+ _word_for_instance_in_error_message : ClassVar [str ]
27
+
24
28
def __init__ (
25
29
self ,
26
30
message : str ,
@@ -65,8 +69,34 @@ def __init__(
65
69
def __repr__ (self ):
66
70
return f"<{ self .__class__ .__name__ } : { self .message !r} >"
67
71
68
- def __str__ (self ) -> str :
69
- return self .message
72
+ def __str__ (self ):
73
+ essential_for_verbose = (
74
+ self .validator , self .validator_value , self .instance , self .schema ,
75
+ )
76
+ if any (m is _unset for m in essential_for_verbose ):
77
+ return self .message
78
+
79
+ schema_path = _utils .format_as_index (
80
+ container = self ._word_for_schema_in_error_message ,
81
+ indices = list (self .relative_schema_path )[:- 1 ],
82
+ )
83
+ instance_path = _utils .format_as_index (
84
+ container = self ._word_for_instance_in_error_message ,
85
+ indices = self .relative_path ,
86
+ )
87
+ prefix = 16 * " "
88
+
89
+ return dedent (
90
+ f"""\
91
+ { self .message }
92
+
93
+ Failed validating { self .validator !r} in { schema_path } :
94
+ { indent (pformat (self .schema , width = 72 ), prefix ).lstrip ()}
95
+
96
+ On { instance_path } :
97
+ { indent (pformat (self .instance , width = 72 ), prefix ).lstrip ()}
98
+ """ .rstrip (),
99
+ )
70
100
71
101
@classmethod
72
102
def create_from (cls , other ):
@@ -112,16 +142,8 @@ def _set(self, type_checker=None, **kwargs):
112
142
113
143
def _contents (self ):
114
144
attrs = (
115
- "message" ,
116
- "cause" ,
117
- "context" ,
118
- "validator" ,
119
- "validator_value" ,
120
- "path" ,
121
- "schema_path" ,
122
- "instance" ,
123
- "schema" ,
124
- "parent" ,
145
+ "message" , "cause" , "context" , "validator" , "validator_value" ,
146
+ "path" , "schema_path" , "instance" , "schema" , "parent" ,
125
147
)
126
148
return dict ((attr , getattr (self , attr )) for attr in attrs )
127
149
@@ -140,44 +162,7 @@ def _matches_type(self):
140
162
)
141
163
142
164
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 ):
165
+ class ValidationError (_Error ):
181
166
"""
182
167
An instance was invalid under a provided schema.
183
168
"""
@@ -186,7 +171,7 @@ class ValidationError(_VerboseError):
186
171
_word_for_instance_in_error_message = "instance"
187
172
188
173
189
- class SchemaError (_VerboseError ):
174
+ class SchemaError (_Error ):
190
175
"""
191
176
A schema was invalid under its corresponding metaschema.
192
177
"""
0 commit comments