Skip to content

Commit eb633b2

Browse files
committed
Tidy up validation error messages, particularly using f-strings.
1 parent 4737847 commit eb633b2

File tree

10 files changed

+320
-152
lines changed

10 files changed

+320
-152
lines changed

jsonschema/_format.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ def check(self, instance, format):
9797
except raises as e:
9898
cause = e
9999
if not result:
100-
raise FormatError(
101-
"%r is not a %r" % (instance, format), cause=cause,
102-
)
100+
raise FormatError(f"{instance!r} is not a {format!r}", cause=cause)
103101

104102
def conforms(self, instance, format):
105103
"""

jsonschema/_legacy_validators.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ def dependencies_draft3(validator, dependencies, instance, schema):
3232
yield error
3333
elif validator.is_type(dependency, "string"):
3434
if dependency not in instance:
35-
message = "%r is a dependency of %r"
36-
yield ValidationError(message % (dependency, property))
35+
message = f"{dependency!r} is a dependency of {property!r}"
36+
yield ValidationError(message)
3737
else:
3838
for each in dependency:
3939
if each not in instance:
40-
message = "%r is a dependency of %r"
41-
yield ValidationError(message % (each, property))
40+
message = f"{each!r} is a dependency of {property!r}"
41+
yield ValidationError(message)
4242

4343

4444
def dependencies_draft4_draft6_draft7(
@@ -63,8 +63,8 @@ def dependencies_draft4_draft6_draft7(
6363
if validator.is_type(dependency, "array"):
6464
for each in dependency:
6565
if each not in instance:
66-
message = "%r is a dependency of %r"
67-
yield ValidationError(message % (each, property))
66+
message = f"{each!r} is a dependency of {property!r}"
67+
yield ValidationError(message)
6868
else:
6969
for error in validator.descend(
7070
instance, dependency, schema_path=property,
@@ -75,9 +75,8 @@ def dependencies_draft4_draft6_draft7(
7575
def disallow_draft3(validator, disallow, instance, schema):
7676
for disallowed in _utils.ensure_list(disallow):
7777
if validator.is_valid(instance, {"type": [disallowed]}):
78-
yield ValidationError(
79-
"%r is disallowed for %r" % (disallowed, instance),
80-
)
78+
message = f"{disallowed!r} is disallowed for {instance!r}"
79+
yield ValidationError(message)
8180

8281

8382
def extends_draft3(validator, extends, instance, schema):
@@ -134,9 +133,8 @@ def minimum_draft3_draft4(validator, minimum, instance, schema):
134133
cmp = "less than"
135134

136135
if failed:
137-
yield ValidationError(
138-
"%r is %s the minimum of %r" % (instance, cmp, minimum),
139-
)
136+
message = f"{instance!r} is {cmp} the minimum of {minimum!r}"
137+
yield ValidationError(message)
140138

141139

142140
def maximum_draft3_draft4(validator, maximum, instance, schema):
@@ -151,9 +149,8 @@ def maximum_draft3_draft4(validator, maximum, instance, schema):
151149
cmp = "greater than"
152150

153151
if failed:
154-
yield ValidationError(
155-
"%r is %s the maximum of %r" % (instance, cmp, maximum),
156-
)
152+
message = f"{instance!r} is {cmp} the maximum of {maximum!r}"
153+
yield ValidationError(message)
157154

158155

159156
def properties_draft3(validator, properties, instance, schema):
@@ -170,7 +167,7 @@ def properties_draft3(validator, properties, instance, schema):
170167
):
171168
yield error
172169
elif subschema.get("required", False):
173-
error = ValidationError("%r is a required property" % property)
170+
error = ValidationError(f"{property!r} is a required property")
174171
error._set(
175172
validator="required",
176173
validator_value=subschema["required"],
@@ -207,7 +204,7 @@ def contains_draft6_draft7(validator, contains, instance, schema):
207204

208205
if not any(validator.is_valid(element, contains) for element in instance):
209206
yield ValidationError(
210-
"None of %r are valid under the given schema" % (instance,),
207+
f"None of {instance!r} are valid under the given schema",
211208
)
212209

213210

jsonschema/_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def format_as_index(indices):
8686

8787
if not indices:
8888
return ""
89-
return "[%s]" % "][".join(repr(index) for index in indices)
89+
return f"[{']['.join(repr(index) for index in indices)}]"
9090

9191

9292
def find_additional_properties(instance, schema):
@@ -136,7 +136,7 @@ def types_msg(instance, types):
136136
reprs.append(repr(type["name"]))
137137
except Exception:
138138
reprs.append(repr(type))
139-
return "%r is not of type %s" % (instance, ", ".join(reprs))
139+
return f"{instance!r} is not of type {', '.join(reprs)}"
140140

141141

142142
def flatten(suitable_for_isinstance):

0 commit comments

Comments
 (0)