From 8316bc7d4a6dbd989d53f97a8c7a06758c8d356c Mon Sep 17 00:00:00 2001 From: svlandeg Date: Fri, 9 Oct 2020 12:06:20 +0200 Subject: [PATCH] bugfix DisabledPipes --- spacy/language.py | 3 +++ spacy/tests/pipeline/test_pipe_methods.py | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/spacy/language.py b/spacy/language.py index 1fb559657..24e593043 100644 --- a/spacy/language.py +++ b/spacy/language.py @@ -1034,6 +1034,9 @@ class Language: ) ) disable = to_disable + # DisabledPipes will restore the pipes in 'disable' when it's done, so we need to exclude + # those pipes that were already disabled. + disable = [d for d in disable if d not in self._disabled] return DisabledPipes(self, disable) def make_doc(self, text: str) -> Doc: diff --git a/spacy/tests/pipeline/test_pipe_methods.py b/spacy/tests/pipeline/test_pipe_methods.py index c693a7487..cd18b0159 100644 --- a/spacy/tests/pipeline/test_pipe_methods.py +++ b/spacy/tests/pipeline/test_pipe_methods.py @@ -129,6 +129,7 @@ def test_enable_pipes_method(nlp, name): @pytest.mark.parametrize("name", ["my_component"]) def test_disable_pipes_context(nlp, name): + """Test that an enabled component stays enabled after running the context manager.""" nlp.add_pipe("new_pipe", name=name) assert nlp.has_pipe(name) with nlp.select_pipes(disable=name): @@ -136,6 +137,19 @@ def test_disable_pipes_context(nlp, name): assert nlp.has_pipe(name) +@pytest.mark.parametrize("name", ["my_component"]) +def test_disable_pipes_context_restore(nlp, name): + """Test that a disabled component stays disabled after running the context manager.""" + nlp.add_pipe("new_pipe", name=name) + assert nlp.has_pipe(name) + nlp.disable_pipes(name) + assert not nlp.has_pipe(name) + with nlp.select_pipes(disable=name): + assert not nlp.has_pipe(name) + assert not nlp.has_pipe(name) + + + def test_select_pipes_list_arg(nlp): for name in ["c1", "c2", "c3"]: nlp.add_pipe("new_pipe", name=name)