Skip to content

Cannot use format specifiers in argparse help #142950

Description

@savannahostrowski

#141940 caused a regression where users cannot use format specifiers.


See this as an example

my_parser.add_argument(
"--foo",
type=int,
default=1234,
help=f"""0x%(default)x""",
)

The above works in 3.14.2, but not in 3.15 alpha3. I apologize if my code is doing something incorrect.

The error I get is

Traceback (most recent call last):
File "[...]/3.15.0a3/lib/python3.15/argparse.py", line 1793, in _check_help
formatter._expand_help(action)
~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
File "[...]/3.15.0a3/lib/python3.15/argparse.py", line 696, in _expand_helpreturn help_string % params
~~~~~~~~~~~~^~~~~~~~TypeError: %x format: an integer is required, not str

Originally posted by @oyvindronningstad in #141940 (comment)

Linked PRs

Metadata

Metadata

Labels

3.15pre-release feature fixes, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

Status
Doc issues

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions