Skip to content

Commit 9506493

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

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/_pytest/subtests.py

Lines changed: 10 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,15 @@ 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+
def convert(x: Any) -> str:
70+
try:
71+
return json.dumps(x)
72+
except TypeError:
73+
return saferepr(x)
74+
75+
result["kwargs"] = {k: convert(v) for (k, v) in result["kwargs"].items()}
6876
return result
6977

7078
@classmethod

0 commit comments

Comments
 (0)