Serialization hooks - #4965
Conversation
acabb78 to
8b683f6
Compare
|
I noticed that we have arbitrary keyword arguments that can be passed to Found another use case here: https://github.com/pytest-dev/pytest/pull/1874/files. |
|
@nicoddemus as long as reports support random mutation/extension, its necessary to support unknown attributes in some way, so we would need an "extension point" style attribute as a starting point to create a migration path details like the optional worker id are currently just "tacked on" |
Yes, I was thinking of having an explicit |
|
the critical point here is api consumers and backward compatibility |
|
Definitely, we can probably introduce a deprecation for |
|
i dont see report construction as the issue - but rather report consumption |
So it would probably be OK to change the report classes to attrs, while changing the |
|
pytest does not expose the report type so its absolutely valid to change its signature, however it does expose report objects, so its necessary to keep the object signature a combination of slots + redirecting setattr could do the trick,, there will be some "pain" |
|
Thanks for the thoughts @RonnyPfannschmidt. 👍 Regardless this is certainly a topic for a separate PR. |
2dc6aeb to
3d63d92
Compare
Codecov Report
@@ Coverage Diff @@
## features #4965 +/- ##
===========================================
+ Coverage 95.97% 96% +0.02%
===========================================
Files 113 114 +1
Lines 25359 25631 +272
Branches 2501 2536 +35
===========================================
+ Hits 24339 24607 +268
Misses 714 714
- Partials 306 310 +4
Continue to review full report at Codecov.
|
This methods were moved from xdist (ca03269). Our intention is to keep this code closer to the core, given that it might break easily due to refactorings. Having it in the core might also allow to improve the code by moving some responsibility to the "code" objects (ReprEntry, etc) which are often found in the reports. Finally pytest-xdist and pytest-subtests can use those functions instead of coding it themselves.
These hooks will be used by pytest-xdist and pytest-subtests to serialize and customize reports.
73b70de to
2d77018
Compare
| they fail). | ||
| """ | ||
|
|
||
| __test__ = False |
There was a problem hiding this comment.
heh neat, testify used the same approach: https://github.com/Yelp/Testify/blob/335f04a7de583026a1e0d04d006f4c40524fd89d/testify/test_discovery.py#L57-L59
I think this oddity could really be purged if tests were required to be defined in the module they appear in, and then importing names wouldn't add them to discovery -- always was a weird thing about unittest in my mind -- fwiw I ensured that in testify with this code
| if isinstance(report, (TestReport, CollectReport)): | ||
| data = report._to_json() | ||
| data["_report_type"] = report.__class__.__name__ | ||
| return data |
There was a problem hiding this comment.
Nitpick: these implementations don't actually serialize/unserialize the objects: they convert them to/from native Python types which can be serialised/deserialised as JSON. Serialisation, to my mind, means converting something to bytes (or at least str).
That's a fine API if that's what you want, but maybe the names should reflect it. Sorry for bikeshedding. ;-)
There was a problem hiding this comment.
Not at all, thanks for chipping in. I agree with you, they are not actually serializing anything.
I'm fine with changing those names now, easier to do this before merging. Do you have a suggestion?
There was a problem hiding this comment.
Maybe pytest_report_to_json and pytest_report_from_json? It's not strictly accurate either, because they're producing dicts suitable for JSON, not actual JSON, but I think that inaccuracy is widely accepted.
Otherwise:
pytest_report_to_dict- accurate but less specificpytest_report_to_jsonable- kind of ugly, IMO
There was a problem hiding this comment.
I would like to avoid restricting this to "json", because we might change the actual representation to something else.
I like pytest_report_to_dict and pytest_report_from_dict; I agree it is less accurate, but I believe it is enough to mention that it only supports built-in types in the docs.
Thanks a lot for the input! I will change to pytest_report_to_dict and pytest_report_from_dict later then. 👍
There was a problem hiding this comment.
at $previous_job we called this def __primitive__(self):
There was a problem hiding this comment.
JSON-compatible is a stricter standard than built-in types (e.g. no sets in JSON), but so long as the requirements are documented, I think the dict naming is a good option.
There was a problem hiding this comment.
What about …_to_python and …_from_python? But it does not clearly indicate the overall direction (as with …serialize and …unserialize).
There was a problem hiding this comment.
to/from_serializable just to keep the door open for msgpack and yaml as well
There was a problem hiding this comment.
I like @RonnyPfannschmidt's, we describe the intent rather what the hook returns in detail. 👍
There was a problem hiding this comment.
Done, pytest_report_from_serializable/pytest_report_to_serializable. 👍
If nobody objects I will merge this later then.

Add
pytest_report_serializeandpytest_report_unserializeexperimental hooks.Our intention is to keep this code closer to the core, given that it
might break easily due to refactorings.
Having it in the core might also allow to improve the code by moving
some responsibility to the "code" objects (ReprEntry, etc) which
are often found in the reports.
The actual job is done by
_to_jsonand_from_jsonofTestReportandCollectReport(this methods were moved from xdist (ca03269)).These hooks will be used by
pytest-xdist,pytest-subtests, and the replacement forresultlogto serialize and customize reports.