feat: coerce fact fixtures to the fact's declared return type#4
Open
wowi42 wants to merge 2 commits into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When an operation calls
host.get_fact(FactClass), the harness now inspects the fact'sprocess()return annotation or itsFactBase[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_valueso dataclass fields typed as tuples are correctly populated from JSON arrays.Use case: pyinfra-dev/pyinfra#1823 migrates FreeBSD
pkgfacts to returnlist[PackageInfo]. With this change, operation tests can supply plain JSON arrays of objects and have them automatically becomePackageInfoinstances, removing the need for operation-side normalization helpers.