tophat / syrupy

:pancakes: The sweeter pytest snapshot plugin
https://tophat.github.io/syrupy
Apache License 2.0
501 stars 33 forks source link

How to use Syrupy to save custom objects without converting them to strings #786

Closed atharva-2001 closed 11 months ago

atharva-2001 commented 11 months ago

Dear Syrupy Team, Thank you for this great package. I wanted to extend the SingleFileSnapshotExtension to save Pandas and NumPy objects to HDF files and .dat files respectively. I used the functions that you mentioned could be overridden(except the serialize function) to save these files in these new formats. However this specific line in get_assert_diff is causing an error that I just can't get past.

Both Dataframes and Arrays have no bool values, which is why doing something like dataframe or "" raises a ValueError.

Any help on how I can get past this error? Am I doing the wrong thing? Here are a few references- image

Here is the extension that I wrote- https://github.com/atharva-2001/syrupy-pandas-numpy/blob/main/tests/test_pd.py

noahnu commented 11 months ago

That line can be updated to do an explicit "None" check. Are you interested in contributing / opening a PR?

atharva-2001 commented 11 months ago

Yes I would be very happy to! I would open a PR in sometime today!

atharva-2001 commented 11 months ago

@noahnu Would you mind looking at https://github.com/tophat/syrupy/pull/789? Is the change alright? Also could you please review my code a bit as well? Is it safe to extend the class in the way I did from a sustainability perspective?

We are planning to add this library for our tests in TARDIS- https://tardis-sn.github.io/, https://github.com/tardis-sn and might be doing this across several repos.

Could the classes I made stay as is for at least 3-4 years?

noahnu commented 11 months ago

Yes the API you're using is stable. I was thinking about renaming some of the methods to remove the underscore, however I have no immediate plans to do so, and if that happens, it'd be in a new major version with a migration guide.