From 059d97f6de229d05bc705bc57bb26520967544b5 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 29 Nov 2026 20:18:31 +0100 Subject: [PATCH] feat(updates): apply_updates takes an optional log callback (NetOrk/netork#1112) UpdateMixin.apply_updates declares a keyword-only log, as harden_ssh_server does: it receives each line of the package manager's output as the host prints it. A driver that cannot stream does not call it and returns the output at the end, as before; the declaration stays compatible with every driver that implements it without the keyword. --- CHANGELOG.md | 7 +++++++ napalm_device_types/updates.py | 12 +++++++++++- tests/test_updates.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0251134..3db5975 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ recorded here; their history is the git log. ## [Unreleased] +### Added +- `UpdateMixin.apply_updates(packages, *, log=None)`: an optional keyword-only `log` that + receives each line of the package manager's output as the host prints it + (NetOrk/netork#1112), as `harden_ssh_server(..., log=...)` does. A driver that cannot + stream does not call it and returns the output at the end, as before; `output` holds the + whole output either way. Drivers without it keep conforming. + ## [4.53.0] - 2026-11-28 ### Added diff --git a/napalm_device_types/updates.py b/napalm_device_types/updates.py index d730b2d..ea33bf5 100644 --- a/napalm_device_types/updates.py +++ b/napalm_device_types/updates.py @@ -12,6 +12,7 @@ this class in can never shadow a working implementation from a sibling base. from __future__ import annotations +from collections.abc import Callable from typing import TYPE_CHECKING, Any, Literal from typing_extensions import TypedDict @@ -182,7 +183,12 @@ class UpdateMixin: """ ... - def apply_updates(self, packages: list[str]) -> ApplyUpdatesResultDict: + def apply_updates( + self, + packages: list[str], + *, + log: Callable[[str], None] | None = None, + ) -> ApplyUpdatesResultDict: """ Upgrades the given packages to the newest available version. @@ -193,6 +199,10 @@ class UpdateMixin: :param packages: List of package names to upgrade. Each name must match ``^[a-zA-Z0-9_\\-\\+\\.]+$``; a :exc:`ValueError` is raised for any name that does not conform. + :param log: Receives each line of the package manager's output as the + host prints it, so a caller can follow a long run. A driver that + cannot stream does not call it and returns the output at the end, + as without it; ``output`` holds the whole output either way. :returns: A dict with: * success (bool) – ``True`` if the package manager exited without error diff --git a/tests/test_updates.py b/tests/test_updates.py index c891041..6f068b3 100644 --- a/tests/test_updates.py +++ b/tests/test_updates.py @@ -2,6 +2,9 @@ from __future__ import annotations +import ast +import inspect + import napalm_device_types from napalm_device_types.updates import PackageIndexMissing @@ -18,3 +21,29 @@ def test_a_missing_index_is_its_own_failure(): def test_it_is_exported_from_the_package(): assert napalm_device_types.PackageIndexMissing is PackageIndexMissing assert "PackageIndexMissing" in napalm_device_types.__all__ + + +def _declared_apply_updates(): + """``UpdateMixin.apply_updates`` as declared (under ``TYPE_CHECKING``, so read from source).""" + from napalm_device_types import updates + + for node in ast.walk(ast.parse(inspect.getsource(updates))): + if isinstance(node, ast.FunctionDef) and node.name == "apply_updates": + return node + raise AssertionError("apply_updates is not declared") + + +def test_apply_updates_takes_a_log_callback_by_keyword_only(): + """A caller can follow the host's output as it comes (NetOrk/netork#1112); the + positional signature stays as it was, so existing drivers keep conforming.""" + node = _declared_apply_updates() + + assert [a.arg for a in node.args.args] == ["self", "packages"] + assert [a.arg for a in node.args.kwonlyargs] == ["log"] + (default,) = node.args.kw_defaults + assert isinstance(default, ast.Constant) and default.value is None + assert ast.unparse(node.args.kwonlyargs[0].annotation) == "Callable[[str], None] | None" + + +def test_apply_updates_still_hides_at_runtime(): + assert not hasattr(napalm_device_types.UpdateMixin, "apply_updates")