Skip to content

fix(cli): print host data that isn't JSON serialisable instead of failing - #2031

Open
1sakshm wants to merge 2 commits into
pyinfra-dev:3.xfrom
1sakshm:fix/debug-inventory-unserializable-data
Open

1sakshm wants to merge 2 commits into
pyinfra-dev:3.xfrom
1sakshm:fix/debug-inventory-unserializable-data

Conversation

@1sakshm

@1sakshm 1sakshm commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Closes #2025

debug-inventory fails as soon as any host data value is something json_encode doesn't know how to encode. The issue's example is a group data file that defines a dataclass:

TypeError: Cannot serialize: <class 'type'> (<class 'Complex'>)
when serializing dict item 'Complex'

So a single value makes the whole inventory output unusable, even though the deploy itself works fine.

Change

json_encode in pyinfra_cli/util.py, the fallback encoder for debug-inventory and the other --json and debug output, now handles:

  • Classes: written as "Class: Complex", like the existing "Module: ..." and "Function: ...". This is what a class defined in a group data file ends up as.
  • Dataclass instances: written as a dict of their fields, e.g. {"id": 42, "addr": "192.168.0.42"}. This is shallow (not dataclasses.asdict), so nested values go back through json_encode and nothing is deep-copied.
  • Anything else: falls back to repr(obj) instead of raising.

The _host_to_dict docstring, 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(), in debug-inventory and in the other --json output that uses json_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_encode of a class, a dataclass (checking that it's shallow), and an unknown object.
  • tests/test_cli/test_cli.py: --json debug-inventory with a new tests/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.

…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
@wowi42 wowi42 added bug Label for all kind of bugs. CLI CLI mode specific issues. labels Oct 26, 2026
@1sakshm
1sakshm marked this pull request as ready for review October 26, 2026 11:59

@wowi42 wowi42 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I verified the fix end to end:

  • Reproduced #2025 on 3.x with the dataclass from the issue (TypeError: Cannot serialize: <class 'type'> (<class 'Complex'>)). On this branch the same debug-inventory run prints all host data: the class as Class: Complex, the dataclass instance as a field dict, the opaque object via repr(), and plain values untouched.
  • The branch order in json_encode is correct: isinstance(obj, type) catches dataclass classes before dataclasses.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 a datetime field 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_json now serializes as a field dict instead of via to_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 wowi42 left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The custom to_json() behavior needs to stay unchanged in v3. The new branches currently bypass it.

Issues

  • src/pyinfra_cli/util.py:113 and src/pyinfra_cli/util.py:117: the new class and dataclass branches run before the existing to_json() handling at line 142. Existing host data can therefore lose its custom JSON output. For example, a dataclass whose to_json() returns {"custom": 1} now returns its field dict instead. Keep to_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.md requires the tool name and the extent of assistance.

Done at PyCon FR.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Label for all kind of bugs. CLI CLI mode specific issues.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

debug-inventory fails if host data contains non-json-serializable objects

2 participants