Repository navigation
Conversation
…ling `debug-inventory` (and the other JSON output) raised "Cannot serialize" as soon as host data held a value json_encode didn't know, such as a class or dataclass defined in a group data file, so one value made the whole output unusable. Encode classes as "Class: <name>" like modules and functions, dataclass instances as a dict of their fields, and fall back to repr() for any other object. Closes pyinfra-dev#2025
1sakshm
marked this pull request as ready for review
October 26, 2026 11:59
wowi42
approved these changes
Oct 28, 2026
wowi42
left a comment
Collaborator
There was a problem hiding this comment.
LGTM. I verified the fix end to end:
- Reproduced #2025 on
3.xwith the dataclass from the issue (TypeError: Cannot serialize: <class 'type'> (<class 'Complex'>)). On this branch the samedebug-inventoryrun prints all host data: the class asClass: Complex, the dataclass instance as a field dict, the opaque object viarepr(), and plain values untouched. - The branch order in
json_encodeis correct:isinstance(obj, type)catches dataclass classes beforedataclasses.is_dataclass, which is true for classes too. - The shallow field dict is the right choice:
json.dumps(default=json_encode)re-encodes nested values, so adatetimefield still becomes ISO format. - New tests cover all three branches plus the CLI path. Full
tests/test_cli/, ruff and format all pass locally.
Two minor, non-blocking notes:
- A dataclass that also defines
to_jsonnow serializes as a field dict instead of viato_json(), since the dataclass branch comes first. Marginal, but it is a precedence change. - The
repr()fallback can emit memory addresses for default-repr objects. Fine for debug output, and I agree with showing something over failing the whole command here.
wowi42
requested changes
Nov 1, 2026
Collaborator
There was a problem hiding this comment.
The custom to_json() behavior needs to stay unchanged in v3. The new branches currently bypass it.
Issues
src/pyinfra_cli/util.py:113andsrc/pyinfra_cli/util.py:117: the new class and dataclass branches run before the existingto_json()handling at line 142. Existing host data can therefore lose its custom JSON output. For example, a dataclass whoseto_json()returns{"custom": 1}now returns its field dict instead. Keepto_json()ahead of both new branches and add regression tests for custom output.
Notes
- The PR has no AI-use disclosure. If AI tools were used,
AI_POLICY.mdrequires the tool name and the extent of assistance.
Done at PyCon FR.
This branch has not been deployed
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.
Closes #2025
debug-inventoryfails as soon as any host data value is somethingjson_encodedoesn't know how to encode. The issue's example is a group data file that defines a dataclass:So a single value makes the whole inventory output unusable, even though the deploy itself works fine.
Change
json_encodeinpyinfra_cli/util.py, the fallback encoder fordebug-inventoryand the other--jsonand debug output, now handles:"Class: Complex", like the existing"Module: ..."and"Function: ...". This is what a class defined in a group data file ends up as.{"id": 42, "addr": "192.168.0.42"}. This is shallow (notdataclasses.asdict), so nested values go back throughjson_encodeand nothing is deep-copied.repr(obj)instead of raising.The
_host_to_dictdocstring, which said such values would raise, is updated to match.Fallback to
repr()The fallback is the part that changes behaviour. Any value that used to raise is now shown as its
repr(), indebug-inventoryand in the other--jsonoutput that usesjson_encode(facts, operations). I went with this because these are debug and inspection outputs, where showing something is more useful than failing the whole command, and the issue suggests the same.If you'd rather keep the error for unknown types, I can drop the fallback and keep just the class and dataclass handling, which fixes the reported case on its own.
Tests
tests/test_cli/test_cli_util.py:json_encodeof a class, a dataclass (checking that it's shallow), and an unknown object.tests/test_cli/test_cli.py:--json debug-inventorywith a newtests/test_cli/group_data_objects/all.py. That file holds the issue's dataclass, an opaque object and a plain string, and the test checks that all of them come through.All 4 new tests fail on
3.x. The full suite, ruff, ruff format, mypy, the arguments sync check and typos all pass.