Skip to content

Commit c4cea87

Browse files
committed
Config: Remove use of NO_DEFAULT for Option.default
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`.
1 parent 7e1ea7b commit c4cea87

3 files changed

Lines changed: 5 additions & 10 deletions

File tree

‎aiida/cmdline/commands/cmd_config.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,14 @@ def _join(val):
7777
def verdi_config_show(ctx, option):
7878
"""Show details of an AiiDA option for the current profile."""
7979
from aiida.manage.configuration import Config, Profile
80-
from aiida.manage.configuration.options import NO_DEFAULT
8180

8281
config: Config = ctx.obj.config
8382
profile: Profile | None = ctx.obj.profile
8483

8584
dct = {
8685
'schema': option.schema,
8786
'values': {
88-
'default': '<NOTSET>' if option.default is NO_DEFAULT else option.default,
87+
'default': '<NOTSET>' if option.default is None else option.default,
8988
'global': config.options.get(option.name, '<NOTSET>'),
9089
}
9190
}

‎aiida/manage/configuration/config.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from aiida.common.exceptions import ConfigurationError
2121

2222
from . import schema as schema_module
23-
from .options import NO_DEFAULT, Option, get_option, get_option_names, parse_option
23+
from .options import Option, get_option, get_option_names, parse_option
2424
from .profile import Profile
2525

2626
__all__ = ('Config', 'config_schema', 'ConfigValidationError')
@@ -407,7 +407,7 @@ def set_option(self, option_name, option_value, scope=None, override=True):
407407

408408
if parsed_value is not None:
409409
value = parsed_value
410-
elif option.default is not NO_DEFAULT:
410+
elif option.default is not None:
411411
value = option.default
412412
else:
413413
return
@@ -442,9 +442,7 @@ def get_option(self, option_name, scope=None, default=True):
442442
:return: the option value or None if not set for the given scope
443443
"""
444444
option = get_option(option_name)
445-
446-
# Default value is `None` unless `default=True` and the `option.default` is not `NO_DEFAULT`
447-
default_value = option.default if default and option.default is not NO_DEFAULT else None
445+
default_value = option.default if default else None
448446

449447
if scope is not None:
450448
value = self.get_profile(scope).get_option(option.name, default_value)

‎aiida/manage/configuration/options.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
__all__ = ('get_option', 'get_option_names', 'parse_option', 'Option')
1616

17-
NO_DEFAULT = ()
18-
1917

2018
class Option:
2119
"""Represent a configuration option schema."""
@@ -41,7 +39,7 @@ def valid_type(self) -> Any:
4139

4240
@property
4341
def default(self) -> Any:
44-
return self._schema.get('default', NO_DEFAULT)
42+
return self._schema.get('default', None)
4543

4644
@property
4745
def description(self) -> str:

0 commit comments

Comments
 (0)