Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warn for invalid keys for components #7134

Merged
merged 18 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/6966.improvement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added warning for when an option is provided for a [component](https://rasa.com/docs/rasa/components) that is not listed as a key in the defaults for that component.
indam23 marked this conversation as resolved.
Show resolved Hide resolved
29 changes: 28 additions & 1 deletion rasa/nlu/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,40 @@ def validate_requirements(component_names: List[Optional[Text]]) -> None:
)


def validate_component_keys(
component: "Component", component_config: Dict[Text, Any]
) -> None:
"""Validates that all keys for a component are valid.

Raises:
UserWarning: If the component has a key specified that is not used
by the component class, it is likely a mistake in the pipeline

indam23 marked this conversation as resolved.
Show resolved Hide resolved
Args:
component: The component class

indam23 marked this conversation as resolved.
Show resolved Hide resolved
component_config: The user-provided config for the component in the pipeline
"""
component_name = component_config.get("name")
allowed_keys = set(component.defaults.keys())
provided_keys = set(component_config.keys())
provided_keys.remove("name")
indam23 marked this conversation as resolved.
Show resolved Hide resolved
list_separator = "\n- "
for key in provided_keys:
if key not in allowed_keys:
rasa.shared.utils.io.raise_warning(
f"You have provided an invalid key `{key}` for component `{component_name}` in your pipeline. "
f"Valid options for `{component_name}` are:\n- "
f"{list_separator.join(allowed_keys)}"
)


def validate_empty_pipeline(pipeline: List["Component"]) -> None:
"""Ensures the pipeline is not empty.

Args:
pipeline: the list of the :class:`rasa.nlu.components.Component`.
"""

if len(pipeline) == 0:
raise InvalidConfigError(
"Can not train an empty pipeline. "
Expand Down
2 changes: 1 addition & 1 deletion rasa/nlu/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ def _build_pipeline(
self, cfg: RasaNLUModelConfig, component_builder: ComponentBuilder
) -> List[Component]:
"""Transform the passed names of the pipeline components into classes."""

pipeline = []

# Transform the passed names of the pipeline components into classes
for i in range(len(cfg.pipeline)):
indam23 marked this conversation as resolved.
Show resolved Hide resolved
component_cfg = cfg.for_component(i)
component = component_builder.create_component(component_cfg, cfg)
components.validate_component_keys(component, cfg.pipeline[i])
pipeline.append(component)

if not self.skip_validation:
Expand Down
14 changes: 14 additions & 0 deletions tests/nlu/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,17 @@ async def test_validate_requirements_raises_exception_on_component_without_name(
await train(
_config, data="./data/examples/rasa/demo-rasa.json", path=str(tmp_path),
)


async def test_validate_component_keys_raises_warning_on_invalid_key(tmp_path: Path,):
_config = RasaNLUModelConfig(
# config with a component that does not have a `confidence_threshold ` property
{"pipeline": [{"name": "WhitespaceTokenizer", "confidence_threshold": 0.7}]}
wochinge marked this conversation as resolved.
Show resolved Hide resolved
)

with pytest.warns(UserWarning) as record:
await train(
_config, data="./data/examples/rasa/demo-rasa.json", path=str(tmp_path),
indam23 marked this conversation as resolved.
Show resolved Hide resolved
)

assert "You have provided an invalid key" in record[0].message.args[0]