mirror of
https://github.com/explosion/spaCy.git
synced 2024-12-24 17:06:29 +03:00
Raise if on_match is not callable or None
This commit is contained in:
parent
38de08c7a9
commit
52904b7270
|
@ -476,6 +476,8 @@ class Errors(object):
|
|||
E168 = ("Unknown field: {field}")
|
||||
E169 = ("Can't find module: {module}")
|
||||
E170 = ("Cannot apply transition {name}: invalid for the current state.")
|
||||
E171 = ("Matcher.add received invalid on_match callback argument: expected "
|
||||
"callable or None, but got: {arg_type}")
|
||||
|
||||
|
||||
@add_codes
|
||||
|
|
|
@ -103,6 +103,8 @@ cdef class Matcher:
|
|||
*patterns (list): List of token descriptions.
|
||||
"""
|
||||
errors = {}
|
||||
if on_match is not None and not hasattr(on_match, "__call__"):
|
||||
raise ValueError(Errors.E171.format(arg_type=type(on_match)))
|
||||
for i, pattern in enumerate(patterns):
|
||||
if len(pattern) == 0:
|
||||
raise ValueError(Errors.E012.format(key=key))
|
||||
|
|
|
@ -410,3 +410,11 @@ def test_matcher_schema_token_attributes(en_vocab, pattern, text):
|
|||
assert len(matcher) == 1
|
||||
matches = matcher(doc)
|
||||
assert len(matches) == 1
|
||||
|
||||
|
||||
def test_matcher_valid_callback(en_vocab):
|
||||
"""Test that on_match can only be None or callable."""
|
||||
matcher = Matcher(en_vocab)
|
||||
with pytest.raises(ValueError):
|
||||
matcher.add("TEST", [], [{"TEXT": "test"}])
|
||||
matcher(Doc(en_vocab, words=["test"]))
|
||||
|
|
Loading…
Reference in New Issue
Block a user