Closed
Conversation
nh13
commented
May 27, 2025
| hasdefault = param.default is not param.empty | ||
| default = param.default if hasdefault else SUPPRESS | ||
|
|
||
| if not hasdefault and defaults is not None and name in defaults: |
Contributor
Author
There was a problem hiding this comment.
question: This makes the function argument take precedence. Perhaps it should be the other way around?
Owner
There was a problem hiding this comment.
It would seem that if you pass an explicit default, that one should be preferred?
nh13
commented
May 27, 2025
| Command line arguments to parse (default: ``sys.argv[1:]``). | ||
| :param defaults: | ||
| Mapping for argument defaults passed to | ||
| `~argparse.ArgumentParser.set_defaults`. Key must be the command name, |
Contributor
Author
There was a problem hiding this comment.
chore: fixme, this isn't true anymore
Owner
|
I don't know how exactly does your want your yaml-loading works (e.g. how do you specify the import defopt
from functools import partial
def foo(*, bar: int) -> None:
print(bar)
def foo2(*, bar: int) -> None:
print(bar)
if __name__ == '__main__':
defopt.run({"foo": partial(foo, bar=5), "sub": [partial(foo2, bar=6)]})Thoughts? |
Contributor
Author
|
proper support for functools.partial Would be great. |
Owner
|
From a quick test the following patch seems enough? Still needs tests and docs of course. diff --git i/src/defopt.py w/src/defopt.py
index 8855c05..df9e386 100644
--- i/src/defopt.py
+++ w/src/defopt.py
@@ -330,7 +330,8 @@ def _recurse_functions(funcs, subparsers):
# If this iterable is not a mapping, then convert it to one using the
# function name itself as the key, but replacing _ with -.
try:
- funcs = {func.__name__.replace('_', '-'): func for func in funcs}
+ funcs = {_unwrap_partial(func).__name__.replace('_', '-'): func
+ for func in funcs}
except AttributeError as exc:
# Do not allow a mapping inside of a list
raise ValueError(
@@ -468,12 +469,16 @@ def signature(func: Union[Callable, str]):
inspect_sig = _preprocess_inspect_signature(
func, inspect.signature(func))
doc_sig = _preprocess_doc_signature(
- func, signature(inspect.getdoc(func)))
+ func, signature(inspect.getdoc(_unwrap_partial(func))))
return _merge_signatures(inspect_sig, doc_sig)
+def _unwrap_partial(func):
+ return func.func if isinstance(func, functools.partial) else func
+
+
def _preprocess_inspect_signature(func, sig):
- hints = typing.get_type_hints(func)
+ hints = typing.get_type_hints(_unwrap_partial(func))
parameters = []
for name, param in sig.parameters.items():
if param.name.startswith('_'): |
Contributor
Author
|
Closing in favor of #130 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The following (incomplete) pull request adds a new argument to the
runcommand to specify a mapping of defaults for function arguments. It may be useful to have the defaults be set at runtime, for example if we want to read them from a YAML or other configuration file. The below is a toy example of how I'd use the argument.If you like the path this PR is going, the next steps would be: