Skip to content

feat(facts.util.packages): add PackageInfo model (phase 1 of #1725)#1726

Open
wowi42 wants to merge 3 commits intopyinfra-dev:3.xfrom
KalvadTech:feat/package-info-model
Open

feat(facts.util.packages): add PackageInfo model (phase 1 of #1725)#1726
wowi42 wants to merge 3 commits intopyinfra-dev:3.xfrom
KalvadTech:feat/package-info-model

Conversation

@wowi42
Copy link
Copy Markdown
Collaborator

@wowi42 wowi42 commented May 5, 2026

Summary

Phase 1 of the staged rollout tracked in #1725 (replacing the rotted #1594).

  • Add PackageInfo dataclass, PackageStatus enum, and build_package_map() in src/pyinfra/facts/util/packages.py.
  • Update operations/util/packaging.ensure_packages() to accept either the legacy dict[str, set[str]] or the new dict[str, PackageInfo]. When PackageInfo is provided, HELD packages always noop, UPGRADEABLE packages are upgraded when latest=True, and INSTALLED packages with no available upgrade noop with an "up to date" message.
  • Legacy callers are unchanged: with the old shape latest=True still blindly adds every versionless package to the upgrade list, and noop messages keep the existing format.
  • Make multi-version output deterministic: build_package_map() and _format_version() sort their input set before consuming it, so noop strings and the chosen installed_version are stable across runs. Set iteration order is hash-randomized for strings, so the previous behaviour was non-deterministic.
  • No package-manager facts or operations are migrated yet, so the existing dict shape is what every caller still returns.

Test plan

  • tests/test_facts_packages.py: unit tests for PackageInfo and build_package_map, including status precedence, frozen instances, empty version sets, and deterministic selection from a multi-version set.
  • tests/test_operations_utils.py::TestEnsurePackagesDualFormat: 14 cases covering both shapes; pinned-version match/mismatch against PackageInfo; uninstall paths (installed, held, missing); latest interaction with HELD, UPGRADEABLE, and pinned versions; sorted multi-version noop string.

Requirements

  • Pull request is based on the default branch (3.x at this time)
  • Pull request includes tests for any new/updated operations/facts
  • Pull request includes documentation for any new/updated operations/facts
  • Tests pass (see scripts/dev-test.sh)
  • Type checking & code style passes (see scripts/dev-lint.sh)

wowi42 added 3 commits May 5, 2026 21:40
Introduce PackageInfo, PackageStatus and build_package_map() so package
facts can return rich, structured data instead of dict[str, set[str]].
Update operations.util.packaging.ensure_packages to accept either the
existing dict[str, set[str]] or the new dict[str, PackageInfo], and
honour HELD / UPGRADEABLE / INSTALLED status when present.

No manager migrations yet, so existing facts/ops keep returning the
legacy shape and behave identically.

Refs pyinfra-dev#1725
Add cases that previously had no coverage:
- versioned _has_package against PackageInfo (matching and mismatching)
- present=False uninstall with PackageInfo (installed and held)
- UPGRADEABLE with a pinned version request goes through install,
  not upgrade

Refs pyinfra-dev#1725
…sion output

Sets are hash-randomized for strings, so picking a single version via
next(iter(versions)) and joining via ",".join(value) produced different
strings across runs. Sort first so:
- PackageInfo.installed_version chosen by build_package_map() is stable
- ensure_packages() noop messages are deterministic when a manager
  reports multiple installed versions for the same name

Lock both behaviours with regression tests.

Refs pyinfra-dev#1725
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant