Add strict unknown-parameter validation mode to notebook execution#869
Add strict unknown-parameter validation mode to notebook execution#869huyhoang171106 wants to merge 3 commits intonteract:mainfrom
Conversation
Signed-off-by: Nguyen Huy Hoang <181364121+huyhoang171106@users.noreply.github.com>
Signed-off-by: Nguyen Huy Hoang <181364121+huyhoang171106@users.noreply.github.com>
Signed-off-by: Nguyen Huy Hoang <181364121+huyhoang171106@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds an opt-in “strict” mode to execute_notebook(...) so callers can fail fast when passing parameters that are not declared in the notebook’s parameters cell (default remains warning-only), addressing #818.
Changes:
- Extend
execute_notebook(...)with araise_on_unknown_parametersflag and raise on unknown keys when enabled. - Add a corresponding CLI flag
--raise-on-unknown-parameters/--no-raise-on-unknown-parameters. - Add a CLI wiring test to ensure the flag is forwarded to
execute_notebook.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| papermill/execute.py | Implements strict unknown-parameter validation (raise vs warn). |
| papermill/cli.py | Exposes strict mode via a new Click option and forwards it into execution. |
| papermill/tests/test_cli.py | Adds a CLI test verifying the new option is passed through. |
Comments suppressed due to low confidence (1)
papermill/execute.py:22
raise_on_unknown_parameterswas inserted into the middle of the publicexecute_notebooksignature (betweenparametersandengine_name). This is a backwards-incompatible API change for any callers that previously passedengine_name(or later args) positionally; those values will now be interpreted asraise_on_unknown_parametersand shift the rest of the arguments. To preserve positional-call compatibility, add the new parameter at the end of the signature (or otherwise avoid shifting existing positional parameters) and update call sites accordingly.
def execute_notebook(
input_path,
output_path,
parameters=None,
raise_on_unknown_parameters=False,
engine_name=None,
request_save_on_cell_execute=True,
prepare_only=False,
kernel_name=None,
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if parameters: | ||
| parameter_predefined = _infer_parameters(nb, name=kernel_name, language=language) | ||
| parameter_predefined = {p.name for p in parameter_predefined} | ||
| for p in parameters: | ||
| if p not in parameter_predefined: | ||
| unknown_parameters = [p for p in parameters if p not in parameter_predefined] | ||
| if unknown_parameters: | ||
| if raise_on_unknown_parameters: | ||
| unknown_parameters_message = ", ".join(str(p) for p in sorted(unknown_parameters, key=str)) | ||
| raise PapermillException(f"Passed unknown parameters: {unknown_parameters_message}") | ||
| for p in unknown_parameters: | ||
| logger.warning(f"Passed unknown parameter: {p}") | ||
| nb = parameterize_notebook( |
There was a problem hiding this comment.
The new strict-unknown-parameter behavior (raising instead of warning) isn’t covered by unit tests. Since papermill/tests/test_execute.py already exercises execute_notebook, please add tests that (1) verify raise_on_unknown_parameters=True raises for an undeclared key, and (2) verify the default behavior continues to only warn and still parameterizes successfully.
Summary
Extend
execute_notebook(...)with a new boolean argument (defaultFalse) that controls behavior when user-supplied parameters are not declared in the notebook’sparameterscell. Today papermill warns for unknown keys; this change should preserve that default but allow strict failure when requested.Files changed
papermill/execute.py(modified)papermill/cli.py(modified)papermill/tests/test_cli.py(modified)Testing
What does this PR do?
Fixes #<issue_number>
Closes #818