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 much faster TypeChecker #63

Merged
merged 4 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ jobs:
if: matrix.cove == 'bods'
run: |
git clone https://github.com/openownership/cove-bods.git
cd cove-bods
git checkout update-requirements-jsonschema
cd ..
git clone https://github.com/openownership/lib-cove-bods.git

- name: Install
Expand Down
35 changes: 31 additions & 4 deletions libcove/lib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import functools
import json
import logging
import numbers
import os
import re
from collections import OrderedDict
from tempfile import NamedTemporaryFile
from urllib.parse import urljoin, urlparse
from urllib.parse import urljoin, urlparse, urlsplit
from urllib.request import urlopen

import jsonref
import jsonschema.validators
Expand All @@ -18,16 +20,41 @@
from flattentool import unflatten
from jsonschema import FormatChecker, RefResolver
from jsonschema._utils import extras_msg, find_additional_properties, uniq
from jsonschema.compat import urlopen, urlsplit
from jsonschema.exceptions import ValidationError
from jsonschema.exceptions import UndefinedTypeCheck, ValidationError

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


class TypeChecker:
def is_type(self, instance, type):
if type == "string":
return isinstance(instance, str)
if type == "array":
return isinstance(instance, list)
if type == "object":
return isinstance(instance, dict)
if type == "integer":
if isinstance(instance, bool):
return False
return isinstance(instance, int)
if type == "number":
if isinstance(instance, bool):
return False
return isinstance(instance, numbers.Number)
if type == "boolean":
return isinstance(instance, bool)
if type == "null":
return instance is None
raise UndefinedTypeCheck(type)


# Because we will be changing items on this validator, it's important we take a copy!
# Otherwise we could cause conflicts with other software in the same process.
validator = jsonschema.validators.extend(
jsonschema.validators.Draft4Validator, validators={}
jsonschema.validators.Draft4Validator,
validators={},
type_checker=TypeChecker(),
)

uniqueItemsValidator = validator.VALIDATORS.pop("uniqueItems")
Expand Down