fix(cli): avoid triggering lazy getter during argparse validation - #1935
Open
zhaoxinyi02 wants to merge 1 commit into
Open
fix(cli): avoid triggering lazy getter during argparse validation#1935zhaoxinyi02 wants to merge 1 commit into
zhaoxinyi02 wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
test_lazy_choices_helpfails on Python 3.14:Root cause
Python 3.14 added
ArgumentParser._check_helptoadd_argument(python/cpython#124899). It visitsaction.helpduring argument registration.LazyChoices.helpis a property that lazily callsgetter()andhelp_formatter(), so the getter was triggered duringadd_argument— violating the lazy contract and breaking the test.Fix
Detect the validation phase by looking for
_check_helpon 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_argumentdoes not callgetter✅parse_args([])/parse_args(['--lazy-option', 'b'])do not callhelp_formatter✅parse_args(['--help'])callshelp_formatter(['a', 'b', 'c'], isolation_mode=False)✅Fixes #1641