python / python/cpython

Undocumented exception: argparse add_argument(action='store_true', metavar='...') raises TypeError

未关闭
#156,577 4 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

@picnixz 已经在做这个了。

开始于 2026年9月1日。

docs stdlib type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Documentation

Not 100% sure whether this is a bug or a documentation error, but the behavior is reasonable, so filing this as a documentation issue for now.

>>> from argparse import ArgumentParser
>>> parser = ArgumentParser()
>>> parser.add_argument('-v', metavar='VERBOSE', dest='verbose', action='store_true')
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    parser.add_argument('-v', metavar='VERBOSE', dest='verbose', action='store_true')
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.14/argparse.py", line 1539, in add_argument
    action = action_class(**kwargs)
TypeError: _StoreTrueAction.__init__() got an unexpected keyword argument 'metavar'

There is no indication in the documentation that this combination will raise an exception.

(FWIW, this behavior is inconsistent with action='store_const', which silently swallows metavar.)

AI Usage

Claude Code, with Opus 4.8, was used to identify and analyze this issue.

Linked PRs
  • gh-156620

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。