From 4adec8f93a1828d4683680cf7d805ff28ea9c972 Mon Sep 17 00:00:00 2001 From: Vasileios Karakasis Date: Wed, 25 Nov 2026 23:35:55 +0100 Subject: [PATCH] Do not re-apply default option arguments when mode options are parsed Signed-off-by: Vasileios Karakasis --- reframe/frontend/argparse.py | 40 ++++++++++++++---------------------- unittests/test_argparser.py | 15 ++++++++++++++ unittests/test_cli.py | 19 +++++++++++++++++ 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/reframe/frontend/argparse.py b/reframe/frontend/argparse.py index b1a1dc6d20..1685271be8 100644 --- a/reframe/frontend/argparse.py +++ b/reframe/frontend/argparse.py @@ -121,8 +121,9 @@ def __getattr__(self, name): f'cannot convert environment variable {envvar!r} ' f'to {arg_type.__name__!r}' ) from err - else: - ret = default + + if _undefined(ret): + ret = default return ret @@ -154,7 +155,6 @@ def __repr__(self): class _ArgumentHolder: def __init__(self, holder, shared_options=None): self._holder = holder - self._defaults = argparse.Namespace() # Map command-line options to environment variables and configuration # options. Values are tuples of the form (envvar, configvar) @@ -235,12 +235,8 @@ def add_argument(self, *flags, **kwargs): kwargs['const'] = True if action == 'store_true' else False kwargs['const'] = False if action == 'store_false' else True - # Remove defaults - try: - self._defaults.__dict__[opt_name] = kwargs['default'] - del kwargs['default'] - except KeyError: - self._defaults.__dict__[opt_name] = None + # Remove defaults; they are applied by `_Namespace` on access + kwargs.pop('default', None) if not flags: return None @@ -291,10 +287,6 @@ def _resolve_attr(self, attr, namespaces): return None - def _update_defaults(self): - for g in self._groups: - self._defaults.__dict__.update(g._defaults.__dict__) - def parse_args(self, args=None, namespace=None, suppress_required=False): '''Convert argument strings to objects and return them as attributes of a namespace. @@ -304,10 +296,13 @@ def parse_args(self, args=None, namespace=None, suppress_required=False): If `namespace` is not `None` and an attribute has not been assigned a value during the parsing process of argument strings `args`, a value - for it will be looked up first in `namespace` and if not found there, - it will be assigned the default value as specified in its corresponding - `add_argument()` call. If no default value was specified either, the - attribute will be set to `None`. + for it will be looked up in `namespace`. + + Defaults are not assigned during parsing, so that the raw parsed + options contain only the options actually passed. The default value + specified in the corresponding `add_argument()` call is applied when + the option is accessed in the returned namespace, if it is not set + otherwise. If `suppress_required` is true, required mutually-exclusive groups will be treated as optional for this parsing operation. @@ -348,16 +343,11 @@ def __exit__(this, *args, **kwargs): if isinstance(namespace, _Namespace): namespace = namespace.cmd_options - # Update parser's defaults with groups' defaults - self._update_defaults() - - # Update the parsed options of those from the given namespace and/or - # the defaults + # Update the parsed options of those from the given namespace; defaults + # are not assigned here, they are applied by `_Namespace` on access for attr, val in options.__dict__.items(): if val is None: - resolved = self._resolve_attr(attr, - [namespace, self._defaults]) - options.__dict__[attr] = resolved + options.__dict__[attr] = self._resolve_attr(attr, [namespace]) elif self._option_map[attr][2] == 'append': # 'append' options are combined with those from the given # namespace, but *not* with the defaults (important) diff --git a/unittests/test_argparser.py b/unittests/test_argparser.py index 4b59b69d43..1535cdb412 100644 --- a/unittests/test_argparser.py +++ b/unittests/test_argparser.py @@ -88,6 +88,21 @@ def test_parsing(argparser, foo_options, bar_options): assert options.barfoo +def test_reparse_does_not_append_defaults(argparser, foo_options, + bar_options): + argparser.add_argument('--alist', action='append', default=['X']) + options = argparser.parse_args(['--foo', 'name']) + assert options.cmd_options.alist is None + assert options.cmd_options.bar is None + assert ['X'] == options.alist + assert 'BAR' == options.bar + + options = argparser.parse_args(['--alist', 'Y'], options) + assert ['Y'] == options.alist + assert 'name' == options.foo + assert 'BAR' == options.bar + + @pytest.fixture def extended_parser(): parser = ArgumentParser() diff --git a/unittests/test_cli.py b/unittests/test_cli.py index e8de6d2bfe..85724d88f6 100644 --- a/unittests/test_cli.py +++ b/unittests/test_cli.py @@ -618,6 +618,25 @@ def test_execution_modes_envvar_expansion(run_reframe, monkeypatch): assert 'Found 1 check(s)' in stdout +def test_execution_modes_prgenv_filter(run_reframe, monkeypatch): + # The default of `-p` must not leak into the command-line filter + monkeypatch.setenv('TEST_NAME_PATTERN', '^HelloTest$') + monkeypatch.setenv('VAR', 'x') + monkeypatch.setenv('VAL', '1') + returncode, stdout, stderr = run_reframe( + system='testsys:gpu', + environs=['PrgEnv-gnu'], + mode='env_vars', + action='list_concretized', + local=False + ) + assert returncode == 0 + assert 'Traceback' not in stdout + assert 'Traceback' not in stderr + assert 'testsys:gpu+PrgEnv-gnu' in stdout + assert 'testsys:gpu+builtin' not in stdout + + def test_invalid_mode_error(run_reframe): mode = 'invalid' returncode, stdout, stderr = run_reframe(