diff --git a/httpie/cli/argparser.py b/httpie/cli/argparser.py index 9bf09b3b73..98b50d8e1c 100644 --- a/httpie/cli/argparser.py +++ b/httpie/cli/argparser.py @@ -76,9 +76,23 @@ def add_usage(self, usage, actions, groups, prefix=None): ) +class HelpNotCheckingArgumentParser(argparse.ArgumentParser): + """ + This is ArgumentParser which does not check the help when adding arguments. + Effectively, it reverts the behavior added to Python 3.14 + in https://github.com/python/cpython/pull/124899 + + It needs to be used to make LazyChoices actually lazy. + + Using this fixes https://github.com/httpie/cli/issues/1641 + """ + def _check_help(self, *args, **kwargs): + pass + + # TODO: refactor and design type-annotated data structures # for raw args + parsed args and keep things immutable. -class BaseHTTPieArgumentParser(argparse.ArgumentParser): +class BaseHTTPieArgumentParser(HelpNotCheckingArgumentParser): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.env = None diff --git a/tests/test_cli_utils.py b/tests/test_cli_utils.py index 8041727b57..69c5e7222e 100644 --- a/tests/test_cli_utils.py +++ b/tests/test_cli_utils.py @@ -1,6 +1,6 @@ import pytest -from argparse import ArgumentParser from unittest.mock import Mock +from httpie.cli.argparser import HelpNotCheckingArgumentParser from httpie.cli.utils import LazyChoices @@ -9,7 +9,10 @@ def test_lazy_choices(): getter = mock.getter getter.return_value = ['a', 'b', 'c'] - parser = ArgumentParser() + # On Python 3.14+ LazyChoices only works properly + # with our custom ArgumentParser. + # In httpie code base, all our argument parsers inherit from this class. + parser = HelpNotCheckingArgumentParser() parser.register('action', 'lazy_choices', LazyChoices) parser.add_argument( '--option', @@ -57,7 +60,7 @@ def test_lazy_choices_help(): help_formatter = mock.help_formatter help_formatter.return_value = '' - parser = ArgumentParser() + parser = HelpNotCheckingArgumentParser() parser.register('action', 'lazy_choices', LazyChoices) parser.add_argument( '--lazy-option',