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

Config: Remove use of NO_DEFAULT for Option.default #6145

Merged
merged 3 commits into from
Oct 11, 2023

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented Oct 11, 2023

The Option.default property would return the global constant
NO_DEFAULT in case the option does not specify a default. The idea was
that it could be used to distinguish between an option not defining a
default and one defining the default to be None.

The problem is that in the various methods that return a config option
value, such as Config.get_option could also return this value. This
would be problematic for certain CLI command options that used the
aiida.manage.configuration.get_config_option function to set the
default. If the config option was not defined, the function would return
() which is the value of the NO_DEFAULT constant. When the option
accepts string values, this value would often even silently be accepted
although it almost certainly is not what the user intended.

This would actually happen for the tests of verdi setup, which has the
options --email, --first-name, --last-name and --institution
that all define a default through get_config_option and therefore the
default would be actually set to () in case the config did not specify
these global config options.

Since for config options there is no current use-case for actually
setting a default to None, there is no need to distinguish between
this case and a default never having been defined, and so the NO_DEFAULT
global constant is removed and replaced by None.

Before, the test would just fail and say that an exception was raised
but it would not display the actual exception, making it difficult to
debug the problem. In the case of a non-zero exit code, the stderr is
printed as well.
The `Option.default` property would return the global constant
`NO_DEFAULT` in case the option does not specify a default. The idea was
that it could be used to distinguish between an option not defining a
default and one defining the default to be `None`.

The problem is that in the various methods that return a config option
value, such as `Config.get_option` could also return this value. This
would be problematic for certain CLI command options that used the
`aiida.manage.configuration.get_config_option` function to set the
default. If the config option was not defined, the function would return
`()` which is the value of the `NO_DEFAULT` constant. When the option
accepts string values, this value would often even silently be accepted
although it almost certainly is not what the user intended.

This would actually happen for the tests of `verdi setup`, which has the
options `--email`, `--first-name`, `--last-name` and `--institution`
that all define a default through `get_config_option` and therefore the
default would be actually set to `()` in case the config did not specify
these global config options.

Since for config options there is no current use-case for actually
setting a default to `None`, there is no need to distinguish between
this case and a default never having been defined, and so the `NO_DEFAULT`
global constant is removed and replaced by `None`.
The previous commit fixed a bug in the evaluation of defaults for
various options of the `verdi setup` command. Due to the bug, these
options would set a default even if the corresponding config option was
not defined. Instead of no default being defined, the empty tuple `()`
would be set as string value.

As soon as the bug was fixed, the `test_setup_profile_uuid` test started
failing since it doesn't explicitly defined values for the options
`--email`, `--first-name`, `--last-name` and `--institution`.
@sphuber
Copy link
Contributor Author

sphuber commented Oct 11, 2023

@danielhollas this reveals the real source of the test breaking in your previous PR where you made the definition of verdi setup option defaults lazy. Would you mind taking a look at this PR?

Copy link
Collaborator

@danielhollas danielhollas left a comment

Choose a reason for hiding this comment

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

Oh wow 😲 I did wonder where the hell was the empty tuple coming from.

Couple of questions and minor suggestions

aiida/cmdline/commands/cmd_config.py Outdated Show resolved Hide resolved
aiida/manage/configuration/config.py Outdated Show resolved Hide resolved
tests/cmdline/commands/test_setup.py Outdated Show resolved Hide resolved
@@ -206,11 +206,15 @@ def test_setup_profile_uuid(self):

profile_name = 'profile-copy'
user_email = '[email protected]'
user_first_name = 'John'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did this test work previously because these options are not required? Or because these options were not properly validated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it worked properly because the type for these options is NON_EMPTY_STRING so any string goes and () is cast to a string and then accepted 😅

tests/conftest.py Outdated Show resolved Hide resolved
@sphuber sphuber force-pushed the fix/test-verdi-setup branch 2 times, most recently from d375c2e to 60758c2 Compare October 11, 2023 21:13
@sphuber sphuber merged commit b6f7ec1 into aiidateam:main Oct 11, 2023
17 checks passed
@sphuber sphuber deleted the fix/test-verdi-setup branch October 11, 2023 21: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.

2 participants