pythonize icon indicating copy to clipboard operation
pythonize copied to clipboard

Support serialising to named mappings

Open juntyr opened this issue 1 year ago • 5 comments

This PR includes four changes:

  • Implement PythonizeListType for PyTuple, a small convenience that cannot be achieved outside the crate
  • Add support for prebuilding mappings during serialisation, which delays the collection of mapping key-value pairs until all are known
  • Add support for named mappings during serialisation, which adds a tiny default-implemented helper trait method to pass forward the struct / tuple / enum name to the mapping type
  • Allow deserializing an enum from any mapping instead of just dicts

I have found them to be very useful to serialise to and from class-like Python types (e.g. namedtuple which keep more type information around).

juntyr avatar May 18 '24 04:05 juntyr

@davidhewitt what are your thoughts on this?

juntyr avatar May 27 '24 13:05 juntyr

@davidhewitt I apologise for pinging again - I'm hoping that this PR could move forward soon-ish as I'm working on publishing a crate of mine (which would need a feature similar to this) for a publication.

juntyr avatar Jun 13 '24 07:06 juntyr

@juntyr Perhaps he unfollowed this repository or pull request. Maybe try pinging him on X, for example, as he has a few social links on his GitHub profile

MarshalX avatar Jun 13 '24 08:06 MarshalX

Hi @juntyr sorry for the delay! I have seen this PR but haven't had a chance to investigate yet. Please hold on and I'll do my best to get to this soon - I'm trying to wrap up PyO3 0.22 and then let's aim to ship this in pythonize 0.22 👍

davidhewitt avatar Jun 14 '24 11:06 davidhewitt

@davidhewitt Congrats on shipping pyo3 v0.22, I'm excited to start using it!

I'm looking forward to collaborating with you to push this PR forward :)

juntyr avatar Jun 26 '24 06:06 juntyr

The merge conflict should now be resolved again :)

juntyr avatar Aug 03 '24 19:08 juntyr

Once #66 is merged, I will very quickly rebase this PR again so that we can hopefully still get it into v0.22

juntyr avatar Aug 03 '24 19:08 juntyr

Codecov Report

Attention: Patch coverage is 75.25773% with 24 lines in your changes missing coverage. Please review.

Project coverage is 83.41%. Comparing base (6c2c3f2) to head (7c18b9f).

Files Patch % Lines
src/ser.rs 77.77% 4 Missing and 16 partials :warning:
src/de.rs 42.85% 0 Missing and 4 partials :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #62      +/-   ##
==========================================
- Coverage   83.83%   83.41%   -0.42%     
==========================================
  Files           3        3              
  Lines        1169     1224      +55     
  Branches     1169     1224      +55     
==========================================
+ Hits          980     1021      +41     
- Misses        140      144       +4     
- Partials       49       59      +10     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Aug 04 '24 08:08 codecov[bot]

GATs were only stabilised in 1.65 but pythonize uses 1.63 … so the builder can’t use a GAT. Perhaps I can inline the builder methods into the main trait itself (hopefully while keeping Map = PyDict)

juntyr avatar Aug 04 '24 09:08 juntyr

I also tried to add a small convenience wrapper type PythonizeUnnamedMappingWrapper so that

  1. it is more explicit that type NamedMap = PythonizeUnnamedMappingWrapper<PyDict> discards the name
  2. it is reusable for others so you don't need to copy-past two trait impls

juntyr avatar Aug 04 '24 10:08 juntyr

Thanks for your review @davidhewitt!

juntyr avatar Aug 07 '24 07:08 juntyr

I’m unsure if the coverage can be easily improved as all new misses seem to be partial and short-circuiting related

juntyr avatar Aug 07 '24 20:08 juntyr

Agreed, I'm very happy to call this as good as it gets! Thanks again 🚀

davidhewitt avatar Aug 07 '24 21:08 davidhewitt