From a7fd42d937d405088803da3fb3b301cc81cea719 Mon Sep 17 00:00:00 2001 From: Ines Montani Date: Thu, 30 May 2019 14:34:58 +0200 Subject: [PATCH] Make jsonschema dependency optional (#3784) --- requirements.txt | 3 ++- setup.py | 1 - spacy/errors.py | 2 ++ spacy/matcher/matcher.pyx | 5 ++++- spacy/util.py | 9 +++++++-- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/requirements.txt b/requirements.txt index 169fb37cd..42045a829 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,9 +9,10 @@ srsly>=0.0.5,<1.1.0 # Third party dependencies numpy>=1.15.0 requests>=2.13.0,<3.0.0 -jsonschema>=2.6.0,<3.1.0 plac<1.0.0,>=0.9.6 pathlib==1.0.1; python_version < "3.4" +# Optional dependencies +jsonschema>=2.6.0,<3.1.0 # Development dependencies cython>=0.25 pytest>=4.0.0,<4.1.0 diff --git a/setup.py b/setup.py index c440b016f..0c2b541bd 100755 --- a/setup.py +++ b/setup.py @@ -232,7 +232,6 @@ def setup_package(): "blis>=0.2.2,<0.3.0", "plac<1.0.0,>=0.9.6", "requests>=2.13.0,<3.0.0", - "jsonschema>=2.6.0,<3.1.0", "wasabi>=0.2.0,<1.1.0", "srsly>=0.0.5,<1.1.0", 'pathlib==1.0.1; python_version < "3.4"', diff --git a/spacy/errors.py b/spacy/errors.py index b28393156..f1d42adae 100644 --- a/spacy/errors.py +++ b/spacy/errors.py @@ -385,6 +385,8 @@ class Errors(object): E134 = ("Alias '{alias}' defined for unknown entity '{entity}'.") E135 = ("If you meant to replace a built-in component, use `create_pipe`: " "`nlp.replace_pipe('{name}', nlp.create_pipe('{name}'))`") + E136 = ("This additional feature requires the jsonschema library to be " + "installed:\npip install jsonschema") @add_codes diff --git a/spacy/matcher/matcher.pyx b/spacy/matcher/matcher.pyx index b58c0e072..2dd8c2940 100644 --- a/spacy/matcher/matcher.pyx +++ b/spacy/matcher/matcher.pyx @@ -48,7 +48,10 @@ cdef class Matcher: self._extra_predicates = [] self.vocab = vocab self.mem = Pool() - self.validator = get_json_validator(TOKEN_PATTERN_SCHEMA) if validate else None + if validate: + self.validator = get_json_validator(TOKEN_PATTERN_SCHEMA) + else: + self.validator = None def __reduce__(self): data = (self.vocab, self._patterns, self._callbacks) diff --git a/spacy/util.py b/spacy/util.py index 475d556d0..1a40bb5ca 100644 --- a/spacy/util.py +++ b/spacy/util.py @@ -14,8 +14,11 @@ import functools import itertools import numpy.random import srsly -from jsonschema import Draft4Validator +try: + import jsonschema +except ImportError: + jsonschema = None try: import cupy.random @@ -682,7 +685,9 @@ def get_json_validator(schema): # validator that's used (e.g. different draft implementation), without # having to change it all across the codebase. # TODO: replace with (stable) Draft6Validator, if available - return Draft4Validator(schema) + if jsonschema is None: + raise ValueError(Errors.E136) + return jsonschema.Draft4Validator(schema) def validate_schema(schema):