Skip to content

Commit 53f50a5

Browse files
committed
Best-effort to convert to JSON
1 parent 7585f45 commit 53f50a5

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/_pytest/subtests.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from contextlib import ExitStack
1212
from contextlib import nullcontext
1313
import dataclasses
14+
import json
1415
import time
1516
from types import TracebackType
1617
from typing import Any
@@ -63,8 +64,19 @@ class SubtestContext:
6364

6465
def _to_json(self) -> dict[str, Any]:
6566
result = dataclasses.asdict(self)
66-
# Brute-force the returned kwargs dict to be JSON serializable (pytest-dev/pytest-xdist#1273).
67-
result["kwargs"] = {k: saferepr(v) for (k, v) in result["kwargs"].items()}
67+
68+
# Best-effort to convert the kwargs values to JSON (pytest-dev/pytest-xdist#1273).
69+
# If they can be converted, we return as it is, otherwise we return its saferepr because it seems
70+
# this is the best we can do at this point.
71+
def convert(x: Any) -> Any:
72+
try:
73+
json.dumps(x)
74+
except TypeError:
75+
return saferepr(x)
76+
else:
77+
return x
78+
79+
result["kwargs"] = {k: convert(v) for (k, v) in result["kwargs"].items()}
6880
return result
6981

7082
@classmethod

testing/test_subtests.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from enum import Enum
4+
import json
45
import sys
56
from typing import Literal
67

@@ -978,10 +979,12 @@ class MyEnum(Enum):
978979
)
979980
data = pytest_report_to_serializable(report)
980981
assert data is not None
982+
# Ensure the report is actually serializable to JSON.
983+
_ = json.dumps(data)
981984
new_report = pytest_report_from_serializable(data)
982985
assert new_report is not None
983986
assert new_report.context == SubtestContext(
984-
msg="custom message", kwargs=dict(i=saferepr(10), a=saferepr(MyEnum.A))
987+
msg="custom message", kwargs=dict(i=10, a=saferepr(MyEnum.A))
985988
)
986989

987990

0 commit comments

Comments
 (0)