Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use pinpointed handling of non-serializable data #35

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Next (tbd)

* `#16 <https://github.com/pytest-dev/pytest-reportlog/issues/15>`_: switch recommended file extension to `jsonl <https://jsonlines.org/>`__.
* `#17 <https://github.com/pytest-dev/pytest-reportlog/issues/17>`_: add compression support for ``.bz2``/``.gz``/``.xz`` log file extensions.
* `#12 <https://github.com/pytest-dev/pytest-reportlog/issues/12>`_: handle unserializable objects with a pinpointed marker


0.3.0 (2023-04-26)
Expand Down
21 changes: 5 additions & 16 deletions src/pytest_reportlog/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ def close(self):
self._file = None

def _write_json_data(self, data):
try:
json_data = json.dumps(data)
except TypeError:
data = cleanup_unserializable(data)
json_data = json.dumps(data)
json_data = json.dumps(data, default=unserializable_to_marked_str)
self._file.write(json_data + "\n")
self._file.flush()

Expand All @@ -92,7 +88,7 @@ def pytest_warning_recorded(self, warning_message, when, nodeid, location):
),
"filename": warning_message.filename,
"lineno": warning_message.lineno,
"message": warning_message.message,
"message": str(warning_message.message),
}
extra_data = {
"$report_type": "WarningMessage",
Expand All @@ -112,13 +108,6 @@ def pytest_terminal_summary(self, terminalreporter):
terminalreporter.write_sep("-", f"generated report log file: {self._log_path}")


def cleanup_unserializable(d: Dict[str, Any]) -> Dict[str, Any]:
"""Return new dict with entries that are not json serializable by their str()."""
result = {}
for k, v in d.items():
try:
json.dumps({k: v})
except TypeError:
v = str(v)
result[k] = v
return result
def unserializable_to_marked_str(obj: object) -> Dict[str, str]:
"""for a object that json can not serialize. return {"$no-json": str(obj)}"""
return {"$no-json": str(obj)}
11 changes: 6 additions & 5 deletions tests/test_reportlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pathlib import Path
from _pytest.reports import BaseReport

from pytest_reportlog.plugin import cleanup_unserializable, _open_filtered_writer
from pytest_reportlog.plugin import _open_filtered_writer, unserializable_to_marked_str

from typing_extensions import Protocol, Literal

Expand Down Expand Up @@ -133,16 +133,17 @@ def test_warning():
}


def test_cleanup_unserializable():
def test_unserializable_to_marked():
"""Unittest for the cleanup_unserializable function"""

good = {"x": 1, "y": ["a", "b"]}
new = cleanup_unserializable(good)
new = json.loads(json.dumps(good, default=unserializable_to_marked_str))
assert new == good

class C:
def __str__(self):
return "C instance"

bad = {"x": 1, "y": ["a", "b"], "c": C()}
new = cleanup_unserializable(bad)
assert new == {"x": 1, "c": "C instance", "y": ["a", "b"]}
new_bad = json.loads(json.dumps(bad, default=unserializable_to_marked_str))
assert new_bad == {**bad, "c": {"$no-json": "C instance"}}