Skip to content

fix(api): apply @deploy signature defaults to global arguments - #2009

Open
wowi42 wants to merge 2 commits into
3.xfrom
fix/deploy-signature-defaults
Open

wowi42 wants to merge 2 commits into
3.xfrom
fix/deploy-signature-defaults

Conversation

@wowi42

@wowi42 wowi42 commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

Signature defaults for global arguments on @deploy functions (eg def my_deploy(_sudo=True)) are now applied when resolving global arguments, instead of being silently ignored.

Fixes #923

Root cause

_wrap_deploy passed only the explicitly passed kwargs to pop_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 with bind_partial + apply_defaults() and injects defaults for global argument params the caller did not bind, before pop_global_arguments runs.

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) on def do()) keeps working.

Test plan

  • New test_deploy_signature_global_argument_defaults in tests/test_api/test_api_deploys.py: bare call applies the _sudo=True signature default, a nested deploy inherits it, a signature default takes precedence over the enclosing deploy context, and an explicit _sudo=False overrides the default. Verified the test fails without the fix.
  • Full unit suite: 2127 passed, 15 deselected (end_to_end).
  • ruff check, ruff format --check, mypy and scripts/lint_arguments_sync.py all pass.

Checklist

  • Tests pass (scripts/dev-test.sh)
  • Lint/types pass (scripts/dev-lint.sh)
  • New operations/facts include tests and documentation (no new operations/facts)
  • Atomic scope: one change in src/pyinfra/api/deploy.py plus its test

@wowi42 wowi42 added bug Label for all kind of bugs. API API mode specific issues. labels Oct 13, 2026

@maisim maisim left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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" on TypeError/ValueError would keep the old tolerance.
  • The Global Arguments example in docs/api/deploys.md reads install_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.

Comment thread src/pyinfra/api/deploy.py


def _wrap_deploy(func: Callable[P, Any]) -> PyinfraOperation[P]:
func_signature = signature(func)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
            ...

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API API mode specific issues. bug Label for all kind of bugs.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Apply @deploy wrapped func default arguments to global arguments

2 participants