Skip to content

Conversation

@nicoddemus
Copy link
Member

Convert all the values of SubtestContext.kwargs to strings using saferepr.

This complies with the requirement that the returned dict from pytest_report_to_serializable is serializable to JSON, at the cost of losing type information for objects that are natively supported by JSON.

Fixes pytest-dev/pytest-xdist#1273

Convert all the values of `SubtestContext.kwargs` to strings using `saferepr`.

This complies with the requirement that the returned dict from `pytest_report_to_serializable` is serializable to JSON, at the cost of losing type information for objects that are natively supported by JSON.

Fixes pytest-dev/pytest-xdist#1273
@nicoddemus nicoddemus force-pushed the xdist-subtests-serialization branch from a3cf834 to 7585f45 Compare November 13, 2025 22:35
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Nov 13, 2025
@nicoddemus nicoddemus added the backport 9.0.x apply to PRs at any point; backports the changes to the 9.0.x branch label Nov 13, 2025
Copy link
Member

@bluetech bluetech left a comment

Choose a reason for hiding this comment

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

A couple of comments for your consideration.

return dataclasses.asdict(self)
result = dataclasses.asdict(self)
# Brute-force the returned kwargs dict to be JSON serializable (pytest-dev/pytest-xdist#1273).
result["kwargs"] = {k: saferepr(v) for (k, v) in result["kwargs"].items()}
Copy link
Member

Choose a reason for hiding this comment

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

IMO we should strive to have the property that from_json(to_json(report)) == report. In this case it means that we should have self.kwargs itself should be serializable, not just in to_json.

return dataclasses.asdict(self)
result = dataclasses.asdict(self)
# Brute-force the returned kwargs dict to be JSON serializable (pytest-dev/pytest-xdist#1273).
result["kwargs"] = {k: saferepr(v) for (k, v) in result["kwargs"].items()}
Copy link
Member

Choose a reason for hiding this comment

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

saferepr itself might be non-optimal, e.g. it would make strings "foo" render as 'foo' while we probably want plain foo. The _idval_from_value function does some reasonable conversions I think.

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

Labels

backport 9.0.x apply to PRs at any point; backports the changes to the 9.0.x branch bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

execnet.gateway_base.DumpError: can't serialize <enum 'SortBy'> with pytest==9.0.0 and while using pytest-xdist

2 participants