You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
argparse performance regression in 3.14+ due to colorization overhead #142267
For a "realistic" CLI with 10 arguments, parser creation is ~3x slower on main compared to 3.13.
The root cause is that _get_formatter() is called twice per add_argument() - once for metavar validation and once in _check_help() for help string validation (called at the end of add_argument ). Each _get_formatter() call creates a new HelpFormatter, which calls _set_color(), which calls can_colorize(), which checks 5 environment variables.
I think a viable fix is to cache the HelpFormatter on ArgumentParser for validation operations. The validation only performs read-only operations (_format_args, _expand_help) that don't modify formatter state. This preserves the existing _get_formatter() behavior while eliminating redundant _set_color() calls during argument setup.
An alternative approach could be to lazily _set_color but that could also break the theme (and then I can't backport a fix)? I'll open the PR for discussion.
Activity
savannahostrowski commentedon Dec 4, 2025
An alternative approach could be to lazily
_set_colorbut that could also break the theme (and then I can't backport a fix)? I'll open the PR for discussion.GH-142267: Cache formatter to avoid repeated `_set_color` calls (#142268
pythonGH-142267: Cache formatter to avoid repeated `_set_color` calls (…
[3.14] GH-142267: Cache formatter to avoid repeated `_set_color` calls (
pythonGH-142267: Cache formatter to avoid repeated `_set_color` calls (…