Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions httpie/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ def help(self, value: Any) -> None:
self._help = value

def __contains__(self, item: Any) -> bool:
# Python 3.14 may validate option defaults against `choices`
# during parser setup. Avoid loading dynamic choices for
# validating this action's own default value.
if self._obj is None and item == self.default:
return True
return item in self.load()

def __iter__(self) -> Iterator[T]:
Expand Down
30 changes: 30 additions & 0 deletions tests/test_cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,33 @@ def test_lazy_choices_help():
with pytest.raises(SystemExit):
parser.parse_args(['--help'])
help_formatter.assert_called_once_with(['a', 'b', 'c'], isolation_mode=False)


def test_lazy_choices_default_validation_does_not_load_choices():
mock = Mock()
getter = mock.getter
getter.return_value = ['a', 'b', 'c']

parser = ArgumentParser()
parser.register('action', 'lazy_choices', LazyChoices)
parser.add_argument(
'--lazy-option',
default='a',
metavar='SYMBOL',
action='lazy_choices',
getter=getter,
cache=False, # for test purposes
)

action = next(
action for action in parser._actions
if action.dest == 'lazy_option'
)

# Simulate default validation (as in Python 3.14 parser setup)
# and ensure it does not trigger eager loading.
parser._check_value(action, 'a')
getter.assert_not_called()

parser._check_value(action, 'b')
getter.assert_called_once()
Loading