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:
ines 2018-01-30 15:43:03 +01:00
parent 0d8a3c9b59
commit 8901814248
2 changed files with 14 additions and 0 deletions

View File

@ -236,6 +236,14 @@ class Language(object):
>>> nlp.add_pipe(component, before='ner') >>> nlp.add_pipe(component, before='ner')
>>> nlp.add_pipe(component, name='custom_name', last=True) >>> 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 name is None:
if hasattr(component, 'name'): if hasattr(component, 'name'):
name = component.name name = component.name

View File

@ -107,3 +107,9 @@ def test_add_lots_of_pipes(nlp, n_pipes):
for i in range(n_pipes): for i in range(n_pipes):
nlp.add_pipe(lambda doc: doc, name='pipe_%d' % i) nlp.add_pipe(lambda doc: doc, name='pipe_%d' % i)
assert len(nlp.pipe_names) == n_pipes 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)