Repository navigation
Conversation
maisim
left a comment
There was a problem hiding this comment.
Looks right, and it fixes the case in #923 exactly - forwarding _sudo=_sudo by hand is no longer needed.
Two things I checked on the way: the injection filter uses all_argument_meta, which I compared against all_global_arguments() and found to be the same 33 keys, so it cannot inject something that is not a global argument; and the test covers the four cases that matter (bare call, nested inheritance, default over the enclosing deploy context, explicit argument wins).
One consequence to be aware of: a None default becomes a value that beats host data, the enclosing deploy context and --sudo. @deploy def d(_sudo=None) called under pyinfra --sudo, or on a host carrying _sudo: true, would run its operations without escalation where it escalated before. That is the stated priority applied consistently, and no signature like that exists in the tree as far as I can see, so it is a documentation point rather than a defect - _sudo=None reads as "unset" to most people, and a line in docs/api/deploys.md would settle it.
Two small things:
signature(func)now runs at decoration time, so a callable it cannot introspect would raise at import instead of at call. Nothing does that today; falling back to "no defaults" onTypeError/ValueErrorwould keep the old tolerance.- The Global Arguments example in
docs/api/deploys.mdreadsinstall_mariadb(sudo=True)while the argument is_sudo- wrong before and after this change, and this is the natural moment to fix it.
Heads up on sequencing: #1957 and #1482 rewrite src/pyinfra/api/deploy.py and its test, so a rebase will be needed whichever lands first.
|
|
||
|
|
||
| def _wrap_deploy(func: Callable[P, Any]) -> PyinfraOperation[P]: | ||
| func_signature = signature(func) |
There was a problem hiding this comment.
For the point 2, maybe we should directly do something like
def _wrap_deploy(func: Callable[P, Any]) -> PyinfraOperation[P]:
try:
func_signature = signature(func)
except (TypeError, ValueError):
# Not every callable can be introspected (builtin types, for instance):
# those simply have no signature defaults to apply.
func_signature = None
global_arg_default_names: set[str] = set()
if func_signature is not None:
parameters = func_signature.parameters
accepts_var_keyword = any(
parameter.kind is Parameter.VAR_KEYWORD for parameter in parameters.values()
)
global_arg_default_names = {
name
for name, parameter in parameters.items()
if name in all_argument_meta and parameter.default is not Parameter.empty
}
@wraps(func)
def decorated_func(*args: P.args, **kwargs: P.kwargs) -> Any:
if global_arg_default_names and func_signature is not None:
...
Signature defaults for global arguments on
@deployfunctions (egdef my_deploy(_sudo=True)) are now applied when resolving global arguments, instead of being silently ignored.Fixes #923
Root cause
_wrap_deploypassed only the explicitly passed kwargs topop_global_arguments, so a global argument declared as a signature default never entered resolution. The wrapper now inspects the function signature at decoration time and, at call time, binds the call withbind_partial+apply_defaults()and injects defaults for global argument params the caller did not bind, beforepop_global_argumentsruns.Priority is now: explicit call arguments > signature defaults > enclosing deploy context > host data > config.
Note: the bind only covers arguments declared in the signature, so passing global arguments to a deploy that does not declare them (eg
do(_sudo=True)ondef do()) keeps working.Test plan
test_deploy_signature_global_argument_defaultsintests/test_api/test_api_deploys.py: bare call applies the_sudo=Truesignature default, a nested deploy inherits it, a signature default takes precedence over the enclosing deploy context, and an explicit_sudo=Falseoverrides the default. Verified the test fails without the fix.ruff check,ruff format --check,mypyandscripts/lint_arguments_sync.pyall pass.Checklist
scripts/dev-test.sh)scripts/dev-lint.sh)src/pyinfra/api/deploy.pyplus its test