From 4f0ef528d482968f3648ae8ad535b9dd62f655ad Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Wed, 15 Mar 2023 14:51:09 -0400 Subject: Enable ruff's simplify rules too. --- jsonschema/_utils.py | 5 +---- jsonschema/_validators.py | 6 +----- jsonschema/tests/_suite.py | 15 ++++++--------- jsonschema/validators.py | 24 +++++++++++------------- pyproject.toml | 6 +++--- 5 files changed, 22 insertions(+), 34 deletions(-) diff --git a/jsonschema/_utils.py b/jsonschema/_utils.py index b14b70e..3f1a440 100644 --- a/jsonschema/_utils.py +++ b/jsonschema/_utils.py @@ -90,10 +90,7 @@ def extras_msg(extras): Create an error message for extra items or properties. """ - if len(extras) == 1: - verb = "was" - else: - verb = "were" + verb = "was" if len(extras) == 1 else "were" return ", ".join(repr(extra) for extra in sorted(extras)), verb diff --git a/jsonschema/_validators.py b/jsonschema/_validators.py index 7351c48..45b53c9 100644 --- a/jsonschema/_validators.py +++ b/jsonschema/_validators.py @@ -45,11 +45,7 @@ def additionalProperties(validator, aP, instance, schema): yield from validator.descend(instance[extra], aP, path=extra) elif not aP and extras: if "patternProperties" in schema: - if len(extras) == 1: - verb = "does" - else: - verb = "do" - + verb = "does" if len(extras) == 1 else "do" joined = ", ".join(repr(each) for each in sorted(extras)) patterns = ", ".join( repr(each) for each in sorted(schema["patternProperties"]) diff --git a/jsonschema/tests/_suite.py b/jsonschema/tests/_suite.py index 6be05be..a79b1e2 100644 --- a/jsonschema/tests/_suite.py +++ b/jsonschema/tests/_suite.py @@ -4,6 +4,7 @@ Python representations of the JSON Schema Test Suite tests. from __future__ import annotations from collections.abc import Iterable, Mapping +from contextlib import suppress from functools import partial from pathlib import Path from typing import TYPE_CHECKING, Any @@ -131,13 +132,11 @@ class Version: } cls = type(name, (unittest.TestCase,), methods) - try: + # We're doing crazy things, so if they go wrong, like a function + # behaving differently on some other interpreter, just make them + # not happen. + with suppress(Exception): cls.__module__ = _someone_save_us_the_module_of_the_caller() - except Exception: # pragma: no cover - # We're doing crazy things, so if they go wrong, like a function - # behaving differently on some other interpreter, just make them - # not happen. - pass return cls @@ -255,10 +254,8 @@ class _Test: validator.validate(instance=self.data) def validate_ignoring_errors(self, Validator): # pragma: no cover - try: + with suppress(jsonschema.ValidationError): self.validate(Validator=Validator) - except jsonschema.ValidationError: - pass def _someone_save_us_the_module_of_the_caller(): diff --git a/jsonschema/validators.py b/jsonschema/validators.py index 5487b84..e5b780a 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -1097,8 +1097,7 @@ class _RefResolver: part = part.replace("~1", "/").replace("~0", "~") if isinstance(document, Sequence): - # Array indexes should be turned into integers - try: + try: # noqa: SIM105 part = int(part) except ValueError: pass @@ -1323,15 +1322,14 @@ def validator_for(schema, default=_UNSET): if schema is True or schema is False or "$schema" not in schema: return DefaultValidator - if schema["$schema"] not in _META_SCHEMAS: - if default is _UNSET: - warn( - ( - "The metaschema specified by $schema was not found. " - "Using the latest draft to validate, but this will raise " - "an error in the future." - ), - DeprecationWarning, - stacklevel=2, - ) + if schema["$schema"] not in _META_SCHEMAS and default is _UNSET: + warn( + ( + "The metaschema specified by $schema was not found. " + "Using the latest draft to validate, but this will raise " + "an error in the future." + ), + DeprecationWarning, + stacklevel=2, + ) return _META_SCHEMAS.get(schema["$schema"], DefaultValidator) diff --git a/pyproject.toml b/pyproject.toml index 859ff71..784ada3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -114,7 +114,7 @@ show_error_codes = true [tool.ruff] line-length = 79 target-version = "py38" -select = ["B", "D", "D204", "E", "F", "Q", "UP", "W"] +select = ["B", "D", "D204", "E", "F", "Q", "SIM", "UP", "W"] ignore = [ # It's totally OK to call functions for default arguments. "B008", @@ -146,10 +146,10 @@ docstring-quotes = "double" [tool.ruff.per-file-ignores] "docs/*" = ["D"] -"jsonschema/cli.py" = ["D", "UP"] +"jsonschema/cli.py" = ["D", "SIM", "UP"] "jsonschema/_utils.py" = ["D"] "jsonschema/benchmarks/*" = ["D"] -"jsonschema/tests/*" = ["D"] +"jsonschema/tests/*" = ["D", "SIM"] [tool.ruff.pyupgrade] # We support 3.8 + 3.9 -- cgit v1.2.1