Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use for-loop instead of slower any() with generator expression #66

Merged
merged 10 commits into from
Sep 15, 2021
22 changes: 20 additions & 2 deletions libcove/lib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,29 @@
from cached_property import cached_property
from flattentool import unflatten
from jsonschema import FormatChecker, RefResolver
from jsonschema._utils import extras_msg, find_additional_properties, uniq
from jsonschema._utils import (
ensure_list,
extras_msg,
find_additional_properties,
types_msg,
uniq,
)
from jsonschema.exceptions import UndefinedTypeCheck, ValidationError

from .exceptions import cove_spreadsheet_conversion_error
from .tools import decimal_default, get_request


def type_validator(validator, types, instance, schema):
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":
Expand Down Expand Up @@ -52,7 +68,9 @@ 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={},
validators={
"type": type_validator,
},
type_checker=TypeChecker(),
)

Expand Down
2 changes: 1 addition & 1 deletion tests/lib/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ class DummySchemaObj:

def get_pkg_schema_obj(self):
return {
"type": "array",
"type": ["array"],
"minItems": 2,
}

Expand Down