From b88c5c701ae5d074edc40a2cef9110745e9ebd54 Mon Sep 17 00:00:00 2001 From: Sofie Van Landeghem Date: Wed, 5 Aug 2020 09:30:58 +0200 Subject: [PATCH] Bugfix in nlp.replace_pipe (#5875) * bugfix and unit test * merge two conditions --- spacy/language.py | 3 ++- spacy/tests/pipeline/test_pipe_methods.py | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/spacy/language.py b/spacy/language.py index 4196e25a4..31bb744db 100644 --- a/spacy/language.py +++ b/spacy/language.py @@ -805,7 +805,8 @@ class Language: # to Language.pipeline to make sure the configs are handled correctly pipe_index = self.pipe_names.index(name) self.remove_pipe(name) - if not len(self.pipeline): # we have no components to insert before/after + if not len(self.pipeline) or pipe_index == len(self.pipeline): + # we have no components to insert before/after, or we're replacing the last component self.add_pipe(factory_name, name=name) else: self.add_pipe(factory_name, name=name, before=pipe_index) diff --git a/spacy/tests/pipeline/test_pipe_methods.py b/spacy/tests/pipeline/test_pipe_methods.py index e37375bf1..0141708b4 100644 --- a/spacy/tests/pipeline/test_pipe_methods.py +++ b/spacy/tests/pipeline/test_pipe_methods.py @@ -70,6 +70,14 @@ def test_replace_pipe(nlp, name, replacement, invalid_replacement): assert nlp.get_pipe(name) == nlp.create_pipe(replacement) +def test_replace_last_pipe(nlp): + nlp.add_pipe("sentencizer") + nlp.add_pipe("ner") + assert nlp.pipe_names == ["sentencizer", "ner"] + nlp.replace_pipe("ner", "ner") + assert nlp.pipe_names == ["sentencizer", "ner"] + + @pytest.mark.parametrize("old_name,new_name", [("old_pipe", "new_pipe")]) def test_rename_pipe(nlp, old_name, new_name): with pytest.raises(ValueError):