Skip to content

Commit

Permalink
Make jsonschema dependency optional (#3784)
Browse files Browse the repository at this point in the history
  • Loading branch information
ines authored and honnibal committed May 30, 2019
1 parent 89379a7 commit a7fd42d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"',
Expand Down
2 changes: 2 additions & 0 deletions spacy/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion spacy/matcher/matcher.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions spacy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
import itertools
import numpy.random
import srsly
from jsonschema import Draft4Validator

try:
import jsonschema
except ImportError:
jsonschema = None

try:
import cupy.random
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit a7fd42d

Please sign in to comment.