Open ickc opened 2 years ago
Thanks for the suggestion. Let's keep the discussion in #95 for now, as I think the two requests share some points.
? I think they are completely different issue? This one is about how positional arg: list[...]
should be treated the same as *args: ...
. #95 is about allowing appending when the same keyword arg is repeated.
I read your comments in #95 and now I understand you're proposing using a similar solution to tackle 2 different issues.
Copied from #95:
This and #96 both seem reasonable to me, but adding a global option to
defopt.run
seems unsatisfying because sooner or later someone will request to be able to specify this only for some of the parameters, perhaps in some deeply nested subcommands hierarchy on top of that...Off the top of my head, a possibly(?) better solution could be to exploit typing.Annotated here, e.g.
Annotated[list[int], defopt.APPEND]
here andAnnotated[list[int], defopt.VARARGS]
for #96. (Note that for the case here, the underlying type annotation should belist[int]
, notint
, because that's what the type offoo
would ultimately be in the program.) A PR would be welcome :-) It should also clarify how these options interact withcli_options
, as introduced by #92 (which is generalizingstrict_kwonly
).
Sorry, giving up for now. I don't seem to understand the design of defopt.
For example,
#!/usr/bin/env python
from typing import Annotated
from inspect import Parameter
import defopt
def main(args: Annotated[list[str], Parameter.VAR_POSITIONAL]):
pass
if __name__ == "__main__":
defopt.run(main)
In _populate_parser
, I get the following
param.annotation == typing.List[str]
getattr(param.annotation, "__metadata__", []) == []
It doesn't seem to be able to get the Annotated types and its metadata.
Right now there is no support for Annotated at all, the suggestion is also to add such support.
MWE
resulted in
Notes
The feature request is to support
main(args: list[TypeX], *, ...)
to be equivalent in terms of defopt tomain(*args: TypeX, ...)
.In the function case,
main
, the user could have written it as the functionok
instead.In the dataclass case however, since Python's dataclass doesn't support variable positional arguments, one cannot defines
*args
. So the only sensible choice here is to defineargs: list
instead.