I’m not a fan of it being a global state, but I somewhat think copyreg makes more sense for a location for this than most places, including on the class itself (different applications need to serialize types differently, we know this). (Constructing and passing around a serializer/deserializer with the appropriate options seems the best option, but that’s already possible and people don’t always do it when they should)
I wonder if the API in copyreg can be imrpoved so that consumers of the api can get a point in time snapshot that can’t change under them, without having to create than snapshot themselves.
Sure, but the point from Serhiy is that while potential breakage will be taken into consideration, we also don’t have to feel bad if there is some due to people not following this rule.
It would be very hard to implement in a way that’s forwards-compatible. Python is a language of consenting adults, so people should realize what risks they are taking on when doing something.
A wrapper works, but each one walks the tree a second time in Python, and that walk dominates the cost. Hooks inside the C encoder avoid it. Msgspec follows suit but defined their own container to speed it up whereas we opted to support native python.
Question on “Neutral ground”: is the intent that third-party native encoders read copyreg.json_dispatch_table directly?
I see your point; probably my point was just noise then. I did want to raise however that the walking is the hard/slow part.
But regardless, I definitely agree we should have an agnostic __serialize__ and this was what I optimized for in treekle. In fact, this is why its “tree”+“pickle” not “json pickle”–I felt going to plain python tree is better because then last mile serialization can be handled elsewhere.
There is a caveat though. Converting object to tree and tree to JSON can be made 2x faster if you go directly to JSON so there is a tension between agnosticism and performance.