fix(packages): architecture is PackageDict's optional key, left out when unnamed (#14) #15

Merged
christianmanivong merged 1 commits from fix/package-architecture-optional into main 2026-12-05 06:41:22 +00:00
4 changed files with 42 additions and 6 deletions

No files matched your search

+9
View File
@@ -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
+13 -5
View File
@@ -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)
+1 -1
View File
@@ -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",
]
+19
View File
@@ -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)