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
16 changes: 15 additions & 1 deletion httpie/cli/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions tests/test_cli_utils.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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',
Expand Down Expand Up @@ -57,7 +60,7 @@ def test_lazy_choices_help():
help_formatter = mock.help_formatter
help_formatter.return_value = '<my help>'

parser = ArgumentParser()
parser = HelpNotCheckingArgumentParser()
parser.register('action', 'lazy_choices', LazyChoices)
parser.add_argument(
'--lazy-option',
Expand Down
Loading