Skip to content

fix(cli): avoid triggering lazy getter during argparse validation - #1935

Open
zhaoxinyi02 wants to merge 1 commit into
httpie:masterfrom
zhaoxinyi02:fix/1641-lazy-choices-python314
Open

fix(cli): avoid triggering lazy getter during argparse validation#1935
zhaoxinyi02 wants to merge 1 commit into
httpie:masterfrom
zhaoxinyi02:fix/1641-lazy-choices-python314

Conversation

@zhaoxinyi02

Copy link
Copy Markdown

Problem

test_lazy_choices_help fails on Python 3.14:

AssertionError: Expected 'getter' to not have been called. Called 1 times.
Calls: [call()].

Root cause

Python 3.14 added ArgumentParser._check_help to add_argument (python/cpython#124899). It visits action.help during argument registration. LazyChoices.help is a property that lazily calls getter() and help_formatter(), so the getter was triggered during add_argument — violating the lazy contract and breaking the test.

Fix

Detect the validation phase by looking for _check_help on the call stack and skip the lazy computation there. The help string is still lazily computed when argparse renders --help.

Verified on Python 3.14.6:

  • add_argument does not call getter
  • parse_args([]) / parse_args(['--lazy-option', 'b']) do not call help_formatter
  • parse_args(['--help']) calls help_formatter(['a', 'b', 'c'], isolation_mode=False)

Fixes #1641

Python 3.14 added _check_help to ArgumentParser.add_argument
(see python/cpython#124899). It visits action.help during argument
registration, which made LazyChoices.help trigger the lazy
getter()/help_formatter() immediately — violating the lazy
contract and breaking test_lazy_choices_help on Python 3.14.

Detect the validation phase by looking for _check_help on the call
stack and skip the lazy computation there; the help string is still
lazily computed when argparse renders --help.

Fixes httpie#1641

Signed-off-by: 赵鑫亿 <98445030+zhaoxinyi02@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings August 12, 2026 18:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test_lazy_choices_help fails on Python 3.14

2 participants