Raise if on_match is not callable or None

This commit is contained in:
Ines Montani 2019-09-24 23:06:24 +02:00
parent 38de08c7a9
commit 52904b7270
3 changed files with 12 additions and 0 deletions

View File

@ -476,6 +476,8 @@ class Errors(object):
E168 = ("Unknown field: {field}") E168 = ("Unknown field: {field}")
E169 = ("Can't find module: {module}") E169 = ("Can't find module: {module}")
E170 = ("Cannot apply transition {name}: invalid for the current state.") 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 @add_codes

View File

@ -103,6 +103,8 @@ cdef class Matcher:
*patterns (list): List of token descriptions. *patterns (list): List of token descriptions.
""" """
errors = {} 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): for i, pattern in enumerate(patterns):
if len(pattern) == 0: if len(pattern) == 0:
raise ValueError(Errors.E012.format(key=key)) raise ValueError(Errors.E012.format(key=key))

View File

@ -410,3 +410,11 @@ def test_matcher_schema_token_attributes(en_vocab, pattern, text):
assert len(matcher) == 1 assert len(matcher) == 1
matches = matcher(doc) matches = matcher(doc)
assert len(matches) == 1 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"]))