diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bf0fce..637cc4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,15 @@ All notable changes to this driver. The format follows ## [Unreleased] +### Changed +- Requires napalm-device-types 4.59.0, which makes a package's `architecture` an + optional key of `PackageDict` itself. `WindowsPackageDict` no longer redeclares it + as required, so strict type checking accepts the driver against that contract (#14). + +### Fixed +- `get_packages()` leaves `architecture` out of a program whose Uninstall key names + none, as the contract asks, instead of reporting it as `""`. + ## [0.3.0] - 2026-12-03 ### Changed diff --git a/napalm_windows/packages.py b/napalm_windows/packages.py index d363b9b..b252eaf 100644 --- a/napalm_windows/packages.py +++ b/napalm_windows/packages.py @@ -67,10 +67,15 @@ ConvertTo-Json -Compress -Depth 3 -InputObject @( class WindowsPackageDict(PackageDict): - """A program from the Uninstall keys, with whose it is and which build.""" + """A program from the Uninstall keys, with whose it is. + + Which build it is -- ``x64``, ``x86`` or ``arm64`` -- is ``PackageDict``'s own + optional ``architecture`` (napalm-device-types 4.59.0), left out where the key names + none. Not redeclared here: a subclass may not make the contract's optional key + required. + """ publisher: str - architecture: str # x64, x86 or arm64 def _size(value: Any) -> int: @@ -88,7 +93,7 @@ def _listed(entry: dict[str, Any]) -> bool: def _package(entry: dict[str, Any]) -> WindowsPackageDict: - return { + package: WindowsPackageDict = { "name": str(entry["name"]).strip(), "version": str(entry.get("version") or "").strip(), "installed": True, @@ -96,14 +101,17 @@ def _package(entry: dict[str, Any]) -> WindowsPackageDict: "size": _size(entry.get("size")), "source": "msi" if str(entry.get("msi") or "") == "1" else "exe", "publisher": str(entry.get("publisher") or "").strip(), - "architecture": str(entry.get("arch") or ""), } + architecture = str(entry.get("arch") or "").strip() + if architecture: + package["architecture"] = architecture + return package def parse_packages(raw: Any) -> list[PackageDict]: """``_PS_PACKAGES``'s JSON as packages, by name; see the module.""" packages = [_package(e) for e in _as_list(raw) if isinstance(e, dict) and _listed(e)] - packages.sort(key=lambda p: (p["name"].lower(), p["architecture"])) + packages.sort(key=lambda p: (p["name"].lower(), p.get("architecture", ""))) return list(packages) diff --git a/pyproject.toml b/pyproject.toml index b8b603d..df40833 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,7 +36,7 @@ classifiers = [ ] dependencies = [ "napalm>=5.1", # older releases import pkg_resources, which a 3.12 venv lacks - "napalm-device-types>=4.56.0", # HostStorageMixin, HostRepairMixin, host events, UserDict.sid + "napalm-device-types>=4.59.0", # PackageDict.architecture "pypsrp>=0.8", ] diff --git a/tests/test_packages.py b/tests/test_packages.py index ba7cef2..ef41d63 100644 --- a/tests/test_packages.py +++ b/tests/test_packages.py @@ -121,3 +121,22 @@ def test_an_empty_answer_has_no_packages(answer): d = WindowsDriver("win01", "admin", "secret") d._transport = FakeTransport(answer) assert d.get_packages() == [] + + +def test_architecture_is_the_contract_s_optional_key(): + """``PackageDict.architecture`` (napalm-device-types 4.59.0) is inherited, not + redeclared: a subclass may not make the contract's optional key required.""" + assert "architecture" in mod.WindowsPackageDict.__optional_keys__ + + +@pytest.mark.parametrize("arch", [None, ""]) +def test_a_program_without_an_architecture_leaves_it_out(arch): + reading = _reading() + git = next(e for e in reading if e.get("name") == "Git") + if arch is None: + del git["arch"] + else: + git["arch"] = arch + packages = _packages(reading) + assert "architecture" not in next(p for p in packages if p["name"] == "Git") + assert [p["name"] for p in packages] == sorted((p["name"] for p in packages), key=str.lower)