Ignore overrides for pipe names in config argument (#10779)

* Pipe name override in config: added check with warning, added removal of name override from config, extended tests.

* Pipoe name override in config: added pytest UserWarning.

Co-authored-by: Adriane Boyd <adrianeboyd@gmail.com>

Co-authored-by: Adriane Boyd <adrianeboyd@gmail.com>
This commit is contained in:
Raphael Mitsch 2022-05-12 11:46:08 +02:00 committed by GitHub
parent b65d652881
commit 6f9e2ca81f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 1 deletions

View File

@ -199,6 +199,7 @@ class Warnings(metaclass=ErrorsWithCodes):
W118 = ("Term '{term}' not found in glossary. It may however be explained in documentation " W118 = ("Term '{term}' not found in glossary. It may however be explained in documentation "
"for the corpora used to train the language. Please check " "for the corpora used to train the language. Please check "
"`nlp.meta[\"sources\"]` for any relevant links.") "`nlp.meta[\"sources\"]` for any relevant links.")
W119 = ("Overriding pipe name in `config` is not supported. Ignoring override '{name_in_config}'.")
class Errors(metaclass=ErrorsWithCodes): class Errors(metaclass=ErrorsWithCodes):

View File

@ -774,6 +774,9 @@ class Language:
name = name if name is not None else factory_name name = name if name is not None else factory_name
if name in self.component_names: if name in self.component_names:
raise ValueError(Errors.E007.format(name=name, opts=self.component_names)) raise ValueError(Errors.E007.format(name=name, opts=self.component_names))
# Overriding pipe name in the config is not supported and will be ignored.
if "name" in config:
warnings.warn(Warnings.W119.format(name_in_config=config.pop("name")))
if source is not None: if source is not None:
# We're loading the component from a model. After loading the # We're loading the component from a model. After loading the
# component, we know its real factory name # component, we know its real factory name

View File

@ -119,6 +119,7 @@ def test_pipe_class_component_config():
self.value1 = value1 self.value1 = value1
self.value2 = value2 self.value2 = value2
self.is_base = True self.is_base = True
self.name = name
def __call__(self, doc: Doc) -> Doc: def __call__(self, doc: Doc) -> Doc:
return doc return doc
@ -141,12 +142,14 @@ def test_pipe_class_component_config():
nlp.add_pipe(name) nlp.add_pipe(name)
with pytest.raises(ConfigValidationError): # invalid config with pytest.raises(ConfigValidationError): # invalid config
nlp.add_pipe(name, config={"value1": "10", "value2": "hello"}) nlp.add_pipe(name, config={"value1": "10", "value2": "hello"})
nlp.add_pipe(name, config={"value1": 10, "value2": "hello"}) with pytest.warns(UserWarning):
nlp.add_pipe(name, config={"value1": 10, "value2": "hello", "name": "wrong_name"})
pipe = nlp.get_pipe(name) pipe = nlp.get_pipe(name)
assert isinstance(pipe.nlp, Language) assert isinstance(pipe.nlp, Language)
assert pipe.value1 == 10 assert pipe.value1 == 10
assert pipe.value2 == "hello" assert pipe.value2 == "hello"
assert pipe.is_base is True assert pipe.is_base is True
assert pipe.name == name
nlp_en = English() nlp_en = English()
with pytest.raises(ConfigValidationError): # invalid config with pytest.raises(ConfigValidationError): # invalid config