Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Discussion: optionally restoring argparse nargs/multiplicity support

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

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

評価

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

調査の方向性

まず argparse ローダーの内部実装、特に _KVArgParser、_DefaultOptionDict、_KVAction と、#322 および #360 で参照されている変更を読んでください。optional nargs のサポートが位置引数および trait の多重性とどのように連携すべきかを判断してください。その動作とテストについて合意されて初めて、issue の準備が整います。

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

説明

Hi, I realize this may be re-opening a can of worms, but I was looking for a way to re-support --key v1 v2 v3 for Container traits in argparse-based CLI handling, originally added in #322 by @ankostis. That support was one of the main reasons I felt comfortable refactoring a codebase to use traitlets. While trying to upgrade to traitlets==5.1, I found out that traitlets was now silently discarding v2 v3 due to https://github.com/ipython/traitlets/pull/582#issuecomment-671922717 (none of my applications use positional arguments).

I was able to patch this for my applications in a rather convoluted way by having them inherit a mixin to change the argparse loader, using #360:

class FixNArgsMixin(HasTraits):
    def _create_loader(self, ..):
        class _DefaultOptionDict(loader._DefaultOptionDict):
            def _add_kv_action(self, key):
                self[key] = loader._KVAction(
                     # ..
                     nargs="+",  # not ideal, but not sure what else can be done, unless we restrict ourselves to Application.classes, resolve these traits and look up trait.multiplicity 
                )
           class KVArgParser(argparse.ArgumentParser):
                 # same as loader.KVArgParser, but with _DefaultOptionDict
           class KVArgParseConfigLoader(loader.KVArgParseConfigLoader):
                 parser_class = KVArgParser
           return KVArgParserConfigLoader(..)
           
class MyApp(FixNArgsMixin, Application):
    foo = List(config=True).tag(multiplicity="+")
MyApp().initialize(["--MyApp.foo", "a", "b"])        

This is pretty verbose and frail since it re-implements a significant amount of the traitlets.config.loader internals. One idea I was considering is to support an allow_nargs=True argument to _KVArgParser() which will set nargs="+" in _DefaultOptionDict, and that way the above method could be shortened to return KVArgParserConfigLoader(.., allow_nargs=True).

Why I use nargs="+":

  1. Compatibility with existing script APIs
  2. Principle of least surprise, its natural to python users that lists can be built from CLI via nargs="+", and matches the argparse behavior of consuming nargs="+" instead of positional arguments
  3. Significantly easier to type/autocomplete out --App.my_long_trait_name 1 2 3 4 vs --App.my_long_trait_name 1 --App.my_long_trait_name 2 --App.my_long_trait_name 3 --App.my_long_trait_name 4

Note: I understand that the multiplicity feature was buyer-beware since it was not officially released (and that traitlets is to an extent "semi-private"), and also the significant complexity in trying to handle nargs together with positional arguments, hence why I don't wish to request any changes in the current default behavior.

cc @minrk @Carreau, apologies in advance for wall of text

主要言語
Python
スター
653
フォーク
217
平均マージ
2日 21時間
マージ済み PR(30日)
2

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

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

はじめの一歩

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

ipython/traitlets のほかの issue

ipython/traitlets の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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