python / python/cpython

argparse: document exceptions, subclass from SystemExit and ArgparseError

オープン
#93,873 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

docs
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

There are many problems with exit status people encounter using argparse (and some of them are open, e.g. https://github.com/python/cpython/issues/85427).

It is reasonable and consistent with other code that it raises SystemExit (in case of unrecognized arguments).
However, this is not flexible and requires pretty much work from users if they want to catch this error, and not exit (there are many alternatives (which in itself contradicts the Zen of Python), the default proposes to subclass the ArgumentParser).

Why can't argparse just subclass SystemExit? This is usually a good guideline for a module to create its own exception classes.

Also, all three argparse exceptions could derive from one argparse exception class (to make their interception easier) (in the title I suggested ArgparseError, but a better name might be found). However, this is not so crucial as this SystemExit: I run a big function in one place of which there is parse_args; I don't want to attribute all SystemExit-s to argparse when I catch exceptions from that function (for now I had to create my own subclass of SystemExit raised from the line with parse_args).

And (should it be a separate feature request?) could there be a subsection dedicated to exceptions in the argparse module documentation? I had to read Python help to understand the initialisation of ArgumentError.
If argparse could guarantee the types of exceptions that it can raise, that would be ideal.

Originally posted by @ericvsmith in https://github.com/python/cpython/issues/83514#issuecomment-1093850932

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Issue で言及されている argparse のエントリーポイント、ArgumentParser、parse_args、ArgumentError から始め、続いて argparse ドキュメントの終了メソッドのセクションと Python のヘルプ出力を確認してください。変更やドキュメントの更新を提案する前に、意図された例外階層、送出される例外型に関する保証、専用の例外サブセクションの範囲を明らかにしてください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
cli, documentation
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。