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

CLI: Fail command if --config file contains unknown key #5939

Merged

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented Mar 21, 2023

Fixes #5929

Up till now, the --config option would accept configuration files with keys that did not correspond to existing options on the command. This would lead to incorrect options to be swallowed silently, which can be surprising to users if they accidentally misspelled an option or used one that didn't exist.

Here the callback is updated to check the specified keys in the config and cross-reference them to the params that are defined on the command. If any unknown keys are specified a BadParameter exception is raised which displays which keys are not supported.

N.B.: The use of the parameter name to filter out the config option itself from valid options is a bit fragile, but I couldn't find a better way for now since the type is not stored and there is no other uniquely defining attribute or type that I could distinguish it with. This is solved, the name of the config option is actually passed as an argument in the callback so we can use that.

@sphuber
Copy link
Contributor Author

sphuber commented Mar 21, 2023

@ltalirz @ConradJohnston proof of principle implementation that I think should work. As noted in the issue, due to the design of click_config_file I am unfortunately forced to essentially copy-paste all code to make the changes we need. If the fix works and we decide we want this, we can of course try to contribute the solution to the package, or just decide to incorporate the code wholesale if it is difficult to get a new release and the license permits it. Since it is a demo, I haven't removed the dependency yet.

@ltalirz
Copy link
Member

ltalirz commented Mar 21, 2023

Thanks @sphuber !

I personally don't see a great downside to vendoring this code in AiiDA; I'd suggest to add a few tests here & merge the PR (removing the dependency).

In the meanwhile, open a PR to click-config-file; if they're happy to accept it, we can remove the code from AiiDA again. How does that sound?

@ConradJohnston
Copy link
Contributor

Good idea @ltalirz , I suspect it will be well received by click-config-file.

@sphuber Looks great! I played a little and see only improvements.

@sphuber sphuber force-pushed the fix/5929/cli-config-option-detect-unknown-keys branch from 676415d to 3c90197 Compare March 22, 2023 08:52
Up till now, the `--config` option would accept configuration files with
keys that did not correspond to existing options on the command. This
would lead to incorrect options to be swallowed silently, which can be
surprising to users if they accidentally misspelled an option or used
one that didn't exist.

Here the callback is updated to check the specified keys in the config
and cross-reference them to the params that are defined on the command.
If any unknown keys are specified a `BadParameter` exception is raised
which displays which keys are not supported.

The implementation was originally provided by the `click-config-file`
dependency, however, in order to customize it with the desired behavior
we had to essentially vendor the entire code, since there was no
available hook for the customizations. The code is taken from
https://github.com/phha/click_config_file/blob/7b93a20b4c79458987fac116418859f30a16d82a
which corresponds to `v0.6.0`. Besides the main changes, the default
provider is changed to `yaml_config_file_provider`, the docstrings are
reformated and type hints are added.
@sphuber sphuber force-pushed the fix/5929/cli-config-option-detect-unknown-keys branch from 3c90197 to 0b784e6 Compare March 22, 2023 09:21
@sphuber
Copy link
Contributor Author

sphuber commented Mar 22, 2023

Opened a PR on click_config_file as well: phha/click_config_file#35

Copy link
Member

@ltalirz ltalirz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot @sphuber !

looks good to me

Copy link
Contributor

@ConradJohnston ConradJohnston left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work thus far.

So I do the following test:
I take an existing code, and do verdi code export to generate a yml.
Then I try to generate a new code from this yml:
verdi code setup --config test.yml.

It chokes on two keys:

Error: Invalid value for '--config': Invalid configuration file, the following keys are not supported: {'default_calc_job_plugin', 'filepath_executable'}

I would have expected a reversible process, but something isn't working as expected.

It seems to be that the options in the CLI do not match the keys.
filepath_executable is given when click expects --remote-abs-path
default_calc_job_plugin should be --input-plugin

All of this said, I think this should be a separate issue and is not a problem with this PR.
It seems that the hardcoded dict _get_cli_options()is out of sync with the CLI.


if unknown_params:
raise click.BadParameter(
f'Invalid configuration file, the following keys are not supported: {unknown_params}', ctx, param
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nitpick:
Is BadParameter the best error class to use here?
I feel like the parameter to the option --config is the path or URL.
That said, the other click error classes are no better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am open to suggestions for better exceptions but I couldn't find one. Note that for the exception to be properly handled, it needs to be a click exception. I think it makes sense to say there is a problem with the --config option, because its value (the content of the file) is incorrect. Since the problem is likely that it contains unsupported keys, we couldn't have it be coupled to any other option of the command, as they don't exist.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm convinced!

@sphuber
Copy link
Contributor Author

sphuber commented Mar 22, 2023

I would have expected a reversible process, but something isn't working as expected.

This is expected though. The verdi code setup and verdi code create commands actually create different subtypes of AbstractCode. If you run verdi code export for a Code (created with verdi code setup) then that YAML will only work with verdi code setup. And if you have an InstalledCode, PortableCode or ContainerizedCode (created with verdi code create) they can only be used with verdi code create. The verdi code setup is deprecated and will at some point be removed and the old Code instances will be automatically migrated to an InstalledCode or PortableCode. So the fact that it chokes on two keys is expected.

@ConradJohnston
Copy link
Contributor

ConradJohnston commented Mar 22, 2023

. So the fact that it chokes on two keys is expected.

Ok, I understand now, thank you!
Send it!

@sphuber sphuber merged commit b11fc15 into aiidateam:main Mar 22, 2023
@sphuber sphuber deleted the fix/5929/cli-config-option-detect-unknown-keys branch March 22, 2023 22:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

verdi code setup --config ignores the yaml file
3 participants