From 89018142489386cbff5e5146b5997e4ed6315aec Mon Sep 17 00:00:00 2001 From: ines Date: Tue, 30 Jan 2018 15:43:03 +0100 Subject: [PATCH] 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. --- spacy/language.py | 8 ++++++++ spacy/tests/pipeline/test_pipe_methods.py | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/spacy/language.py b/spacy/language.py index ec0c5d68f..d4204294e 100644 --- a/spacy/language.py +++ b/spacy/language.py @@ -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 diff --git a/spacy/tests/pipeline/test_pipe_methods.py b/spacy/tests/pipeline/test_pipe_methods.py index c0165d004..6c12e162e 100644 --- a/spacy/tests/pipeline/test_pipe_methods.py +++ b/spacy/tests/pipeline/test_pipe_methods.py @@ -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)