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: allow setting options for config without profiles #5544

Merged
merged 2 commits into from
May 30, 2022

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented May 27, 2022

Fixes #5543

The verdi config set command would except if the config had no
configured profiles. The reason is that the command would attempt to
retrieve the Config instance from the ctx.obj object. The problem is
however that this is created and set on the context in the convert
method of the ProfileParamType, which is used for the --profile
option.

The option specifies a default, which is the default profile, but if
that is not defined, it doesn't provide anything and so the convert
method is never called, resulting in the ctx.obj never being
initialized and the config not being set.

Since the config should always be present for all verdi comments, we
should ensure that it is always loaded and set on ctx.obj. Therefore
it is removed from ProfileParamType.convert and added to the
constructor of a custom implementation of the click.Context class,
called VerdiContext. The VerdiCommandGroup is updated to set the
context_class attribute to this VerdiContext which will ensure that
all commands will be invoked using a context that has the user defined
object constructure with the config loaded and added to it. Since all
commands in verdi will implement this class, this should guarantee
that the config will always be initialized.

@sphuber sphuber requested a review from ltalirz May 27, 2022 18:01
@sphuber sphuber force-pushed the fix/5543/verdi-config-empty-config branch 2 times, most recently from 39651b6 to 8d94ecc Compare May 27, 2022 18:22
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.

Fantastic, thanks a lot @sphuber for fixing this!

I only have one main comment


This parameter type requires the command that uses it to define the ``context_class`` class attribute to be the
:class:`aiida.cmdline.groups.verdi.VerdiContext` class, as that is responsible for creating the user defined object
``obj`` on the context and loads the instance config.
Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to validate this?
If this is straightforward (even if just indirect by checking the ctx.obj) a check + exception might be a more effective way of enforcing this than via documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can do, will add it.

tests/cmdline/commands/test_config.py Outdated Show resolved Hide resolved
tests/cmdline/commands/test_config.py Show resolved Hide resolved
The `verdi config set` command would except if the config had no
configured profiles. The reason is that the command would attempt to
retrieve the `Config` instance from the `ctx.obj` object. The problem is
however that this is created and set on the context in the `convert`
method of the `ProfileParamType`, which is used for the `--profile`
option.

The option specifies a default, which is the default profile, but if
that is not defined, it doesn't provide anything and so the `convert`
method is never called, resulting in the `ctx.obj` never being
initialized and the `config` not being set.

Since the config should always be present for all `verdi` comments, we
should ensure that it is always loaded and set on `ctx.obj`. Therefore
it is removed from `ProfileParamType.convert` and added to the
constructor of a custom implementation of the `click.Context` class,
called `VerdiContext`. The `VerdiCommandGroup` is updated to set the
`context_class` attribute to this `VerdiContext` which will ensure that
all commands will be invoked using a context that has the user defined
object constructure with the config loaded and added to it. Since all
commands in `verdi` will implement this class, this should guarantee
that the config will always be initialized.
@sphuber sphuber force-pushed the fix/5543/verdi-config-empty-config branch from 8d94ecc to 3238125 Compare May 30, 2022 11:01
@sphuber sphuber requested a review from ltalirz May 30, 2022 11:02
@sphuber
Copy link
Contributor Author

sphuber commented May 30, 2022

Thanks for the review @ltalirz . I have added the check

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.

Great - looks good to me!

@sphuber sphuber merged commit c934efd into aiidateam:main May 30, 2022
@sphuber sphuber deleted the fix/5543/verdi-config-empty-config branch May 30, 2022 12:38
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 config should work on configurations without profiles
2 participants