Skip to content

Commit e463aee

Browse files
committed
More strict style, now via bugbear.
The unevaluated* validators will definitely need rewriting, what they do right now is completely incorrect (which bugbear points out at least, though poking at them trivially also does).
1 parent ee77589 commit e463aee

File tree

9 files changed

+29
-26
lines changed

9 files changed

+29
-26
lines changed

jsonschema/_format.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,8 @@ def is_css_color_code(instance):
402402
@_checks_drafts(draft3="color", raises=(ValueError, TypeError))
403403
def is_css21_color(instance):
404404
if (
405-
not isinstance(instance, str) or
406-
instance.lower() in CSS21_NAMES_TO_HEX
405+
not isinstance(instance, str)
406+
or instance.lower() in CSS21_NAMES_TO_HEX
407407
):
408408
return True
409409
return is_css_color_code(instance)
@@ -450,7 +450,7 @@ def is_relative_json_pointer(instance):
450450
for i, character in enumerate(instance):
451451
if character.isdigit():
452452
# digits with a leading "0" are not allowed
453-
if i > 0 and int(instance[i-1]) == 0:
453+
if i > 0 and int(instance[i - 1]) == 0:
454454
return False
455455

456456
non_negative_integer.append(character)

jsonschema/_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ def remove(self, *types):
180180
draft6_type_checker = draft4_type_checker.redefine(
181181
"integer",
182182
lambda checker, instance: (
183-
is_integer(checker, instance) or
184-
isinstance(instance, float) and instance.is_integer()
183+
is_integer(checker, instance)
184+
or isinstance(instance, float) and instance.is_integer()
185185
),
186186
)
187187
draft7_type_checker = draft6_type_checker

jsonschema/_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def find_evaluated_property_keys_by_schema(validator, instance, schema):
332332

333333
if "patternProperties" in schema:
334334
for property, value in instance.items():
335-
for pattern, subschema in schema["patternProperties"].items():
335+
for pattern, _ in schema["patternProperties"].items():
336336
if re.search(pattern, property) and validator.is_valid(
337337
{property: value}, schema["patternProperties"],
338338
):

jsonschema/_validators.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ def items(validator, items, instance, schema):
9292

9393
def additionalItems(validator, aI, instance, schema):
9494
if (
95-
not validator.is_type(instance, "array") or
96-
validator.is_type(schema.get("items", {}), "object")
95+
not validator.is_type(instance, "array")
96+
or validator.is_type(schema.get("items", {}), "object")
9797
):
9898
return
9999

@@ -225,17 +225,17 @@ def maxItems(validator, mI, instance, schema):
225225

226226
def uniqueItems(validator, uI, instance, schema):
227227
if (
228-
uI and
229-
validator.is_type(instance, "array") and
230-
not uniq(instance)
228+
uI
229+
and validator.is_type(instance, "array")
230+
and not uniq(instance)
231231
):
232232
yield ValidationError(f"{instance!r} has non-unique elements")
233233

234234

235235
def pattern(validator, patrn, instance, schema):
236236
if (
237-
validator.is_type(instance, "string") and
238-
not re.search(patrn, instance)
237+
validator.is_type(instance, "string")
238+
and not re.search(patrn, instance)
239239
):
240240
yield ValidationError(f"{instance!r} does not match {patrn!r}")
241241

@@ -435,14 +435,10 @@ def unevaluatedItems(validator, unevaluatedItems, instance, schema):
435435
evaluated_item_indexes = find_evaluated_item_indexes_by_schema(
436436
validator, instance, schema,
437437
)
438-
unevaluated_items = []
439-
for k, v in enumerate(instance):
440-
if k not in evaluated_item_indexes:
441-
for error in validator.descend(
442-
v, unevaluatedItems, schema_path="unevaluatedItems",
443-
):
444-
unevaluated_items.append(v)
445-
438+
unevaluated_items = [
439+
item for index, item in enumerate(instance)
440+
if index not in evaluated_item_indexes
441+
]
446442
if unevaluated_items:
447443
error = "Unevaluated items are not allowed (%s %s unexpected)"
448444
yield ValidationError(error % extras_msg(unevaluated_items))
@@ -453,9 +449,9 @@ def unevaluatedProperties(validator, unevaluatedProperties, instance, schema):
453449
validator, instance, schema,
454450
)
455451
unevaluated_property_keys = []
456-
for property, subschema in instance.items():
452+
for property in instance:
457453
if property not in evaluated_property_keys:
458-
for error in validator.descend(
454+
for _ in validator.descend(
459455
instance[property],
460456
unevaluatedProperties,
461457
path=property,

jsonschema/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def from_arguments(cls, arguments, stdout, stderr):
4040
def load(self, path):
4141
try:
4242
file = open(path)
43-
except (IOError, OSError) as error:
43+
except OSError as error:
4444
if error.errno != errno.ENOENT:
4545
raise
4646
self.filenotfound_error(path=path, exc_info=sys.exc_info())

jsonschema/tests/test_cli.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import sys
1111
import tempfile
1212

13+
from pyrsistent import m
14+
1315
from jsonschema import Draft4Validator, Draft202012Validator, __version__, cli
1416
from jsonschema.exceptions import (
1517
RefResolutionError,
@@ -59,7 +61,7 @@ def _message_for(non_json):
5961

6062
class TestCLI(TestCase):
6163
def run_cli(
62-
self, argv, files={}, stdin=StringIO(), exit_code=0, **override,
64+
self, argv, files=m(), stdin=StringIO(), exit_code=0, **override,
6365
):
6466
arguments = cli.parse_args(argv)
6567
arguments.update(override)

jsonschema/validators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ def _finditem(self, schema, key):
719719
if key in schema:
720720
results.append(schema)
721721

722-
for k, v in schema.items():
722+
for v in schema.values():
723723
if isinstance(v, dict):
724724
results += self._finditem(v, key)
725725

setup.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ inline-quotes = "
6565
exclude =
6666
jsonschema/__init__.py
6767
jsonschema/_reflect.py
68+
ignore =
69+
B008, # Barring function calls in default args. Ha, no.
70+
B306, # See https://github.com/PyCQA/flake8-bugbear/issues/131
71+
W503, # (flake8 default) old PEP8 boolean operator line breaks
6872
6973
[pydocstyle]
7074
match = (?!(test_|_|compat|cli)).*\.py # see PyCQA/pydocstyle#323

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ commands = {envbindir}/detect-secrets scan {toxinidir}
8383
[testenv:style]
8484
deps =
8585
flake8
86+
flake8-bugbear
8687
flake8-commas
8788
flake8-quotes
8889
flake8-broken-line

0 commit comments

Comments
 (0)