Discussion: optionally restoring argparse nargs/multiplicity support
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 30/100
Direzione di ricerca
Inizia leggendo le parti interne del loader di argparse, in particolare _KVArgParser, _DefaultOptionDict e _KVAction, insieme alle modifiche referenziate in #322 e #360. Determina come il supporto per nargs opzionale dovrebbe interagire con gli argomenti posizionali e la molteplicità dei trait; l’issue sarà pronta solo dopo che questo comportamento e i relativi test saranno concordati.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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="+":
- Compatibility with existing script APIs
- Principle of least surprise, its natural to python users that lists can be built from CLI via
nargs="+", and matches theargparsebehavior of consumingnargs="+"instead of positional arguments - Significantly easier to type/autocomplete out
--App.my_long_trait_name 1 2 3 4vs--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
- Lingua principale
- Python
- Stelle
- 653
- Fork
- 217
- Merge medio
- 2g 21h
- PR unite (30g)
- 2
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di ipython/traitlets
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 65/100
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 65/100
-
good first issue
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 85/100
-
documentation
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 65/100
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
Tutte le issue di ipython/traitlets
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 90/100
learningequality/ricecooker#747 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
BSData/horus-heresy-3rd-edition#3171 ·
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
run-llama/llama_index#23199 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
KhronosGroup/glTF-Blender-IO#2769 ·