mirror of
https://github.com/explosion/spaCy.git
synced 2024-12-25 17:36:30 +03:00
Improve error handling if pipeline component is not callable (resolves #1911)
Also add help message if user accidentally calls nlp.add_pipe() with a string of a built-in component name.
This commit is contained in:
parent
0d8a3c9b59
commit
8901814248
|
@ -236,6 +236,14 @@ class Language(object):
|
|||
>>> nlp.add_pipe(component, before='ner')
|
||||
>>> nlp.add_pipe(component, name='custom_name', last=True)
|
||||
"""
|
||||
if not hasattr(component, '__call__'):
|
||||
msg = ("Not a valid pipeline component. Expected callable, but "
|
||||
"got {}. ".format(repr(component)))
|
||||
if component in self.factories.keys():
|
||||
msg += ("If you meant to add a built-in component, use "
|
||||
"create_pipe: nlp.add_pipe(nlp.create_pipe('{}'))"
|
||||
.format(component))
|
||||
raise ValueError(msg)
|
||||
if name is None:
|
||||
if hasattr(component, 'name'):
|
||||
name = component.name
|
||||
|
|
|
@ -107,3 +107,9 @@ def test_add_lots_of_pipes(nlp, n_pipes):
|
|||
for i in range(n_pipes):
|
||||
nlp.add_pipe(lambda doc: doc, name='pipe_%d' % i)
|
||||
assert len(nlp.pipe_names) == n_pipes
|
||||
|
||||
|
||||
@pytest.mark.parametrize('component', ['ner', {'hello': 'world'}])
|
||||
def test_raise_for_invalid_components(nlp, component):
|
||||
with pytest.raises(ValueError):
|
||||
nlp.add_pipe(component)
|
||||
|
|
Loading…
Reference in New Issue
Block a user