Discussion: optionally restoring argparse nargs/multiplicity support
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 30/100
Hướng nghiên cứu
Bắt đầu bằng cách đọc các phần nội bộ của trình loader argparse, đặc biệt là _KVArgParser, _DefaultOptionDict và _KVAction, cùng với các thay đổi được tham chiếu từ #322 và #360. Xác định cách hỗ trợ nargs tùy chọn nên tương tác với các đối số vị trí và tính đa trị của trait; issue chỉ sẵn sàng sau khi hành vi đó và các bài kiểm thử của nó được thống nhất.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- Python
- Star
- 653
- Fork
- 217
- Merge trung bình
- 2 ngày 21 giờ
- Pull request đã merge (30 ngày)
- 2
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của ipython/traitlets
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 65/100
-
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 65/100
-
good first issue
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
-
Document `TraitType.read_only` Đang mởdocumentation
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 65/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
Tất cả issue của ipython/traitlets
Issue tương tự
-
essnmx good first issue
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 95/100
-
[Feature] 奇物选择添加优先级 Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
syfoud/Simulated_Scepter#174 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Giskard-AI/giskard-oss#2840 · 1 bình luận ·
-
A claim comment carrying the issue number is silently declined while the workflow reports success Đang mởarea: repo bug perceived difficulty: 2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
yeti-platform/yeti#1380 ·