From 2c2e0ad1f317e570380702fd5d7e6b02d93e6bcd Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Tue, 11 Jul 2023 16:40:12 -0400 Subject: [PATCH] Restore jsonschema's type validator, as its performance has improved in recent Python versions --- CHANGELOG.md | 4 ++++ libcove/lib/common.py | 42 +----------------------------------------- 2 files changed, 5 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bfaf5c4..cb661fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Dropped support for Python 3.6 & 3.7, as these are now end of life. +### Changed + +- Restore jsonschema's type validator, as its performance has improved in recent Python versions https://github.com/OpenDataServices/lib-cove/pull/127 + ## [0.31.0] - 2023-07-06 ### Changed diff --git a/libcove/lib/common.py b/libcove/lib/common.py index 4465342..63ddc9c 100644 --- a/libcove/lib/common.py +++ b/libcove/lib/common.py @@ -24,7 +24,7 @@ from flattentool import unflatten from jsonschema import FormatChecker, RefResolver -from jsonschema._utils import ensure_list, extras_msg, find_additional_properties, uniq +from jsonschema._utils import extras_msg, find_additional_properties, uniq from jsonschema.exceptions import UndefinedTypeCheck, ValidationError from .exceptions import cove_spreadsheet_conversion_error @@ -33,43 +33,6 @@ REQUIRED_RE = re.compile(r"^'([^']+)'") -# This function was inlined in jsonschema 4. -def types_msg(instance, types): - """ - Create an error message for a failure to match the given types. - - If the ``instance`` is an object and contains a ``name`` property, it will - be considered to be a description of that object and used as its type. - - Otherwise the message is simply the reprs of the given ``types``. - """ - - reprs = [] - for type in types: - try: - reprs.append(repr(type["name"])) - except Exception: - reprs.append(repr(type)) - return "%r is not of type %s" % (instance, ", ".join(reprs)) - - -def type_validator(validator, types, instance, schema): - """ - Replace the jsonschema type validator to use for-loop instead of slower - any() with generator expression. - - https://github.com/OpenDataServices/lib-cove/pull/66 - - """ - types = ensure_list(types) - - for type in types: - if validator.is_type(instance, type): - break - else: - yield ValidationError(types_msg(instance, types)) - - class TypeChecker: def is_type(self, instance, type): if type == "string": @@ -97,9 +60,6 @@ def is_type(self, instance, type): # Otherwise we could cause conflicts with other software in the same process. validator = jsonschema.validators.extend( jsonschema.validators.Draft4Validator, - validators={ - "type": type_validator, - }, type_checker=TypeChecker(), )