Skip to content

feat: coerce fact fixtures to the fact's declared return type#4

Open
wowi42 wants to merge 2 commits into
pyinfra-dev:mainfrom
wowi42:main
Open

feat: coerce fact fixtures to the fact's declared return type#4
wowi42 wants to merge 2 commits into
pyinfra-dev:mainfrom
wowi42:main

Conversation

@wowi42

@wowi42 wowi42 commented Jul 14, 2026

Copy link
Copy Markdown

When an operation calls host.get_fact(FactClass), the harness now inspects the fact's process() return annotation or its FactBase[T] generic argument and coerces plain JSON fixture data into the declared type.

This lets facts that return dataclasses (e.g. PackageInfo) use plain dict fixtures instead of requiring custom parsing in each operation.

Also adds tuple[T, ...] support to _coerce_value so dataclass fields typed as tuples are correctly populated from JSON arrays.


Use case: pyinfra-dev/pyinfra#1823 migrates FreeBSD pkg facts to return list[PackageInfo]. With this change, operation tests can supply plain JSON arrays of objects and have them automatically become PackageInfo instances, removing the need for operation-side normalization helpers.

wowi42 added 2 commits July 14, 2026 12:07
When an operation calls host.get_fact(FactClass), the harness now inspects
the fact's process() return annotation or its FactBase[T] generic argument
and coerces plain JSON fixture data into the declared type. This lets facts
that return dataclasses (e.g. PackageInfo) use plain dict fixtures instead
of requiring custom parsing in each operation.

Also adds tuple[T, ...] support to _coerce_value so dataclass fields typed
as tuples are correctly populated from JSON arrays.
PkgPackages does not declare a return type in pyinfra 3.x, so the
harness had no type to coerce the fixture into. Use a local
PackageListFact annotated as -> list[PackageInfo] so the test still
validates coercion of real pyinfra PackageInfo/PackageStatus objects.
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