cattrs
cattrs copied to clipboard
Extending Coverter instances?
Is it possible to extend a Converter instance, i.e. make a copy and define new or override old hooks? This would be an extremely useful feature, but I couldn't find anything about it.
I don't think so, no one has asked for it yet. Wouldn't be too difficult to add. Can you tell me more about your use case?
I have two different ways I would like to convert some attrs classes to JSON, but with a lot of commonalities. It would be nice if I could create a common base Converter and extend it to create the two final instances. Basically the same pattern as an abstract base class. Now that I'm thinking about it, I guess it is possible to use a factory pattern similar to the preconfigured converters, but a "subclassing" pattern seems a lot more elegant and and can be used with any base converter no matter how it was set up.
Yeah, I suppose adding a .copy() method to the Converter class would be OK. If you put together a PR for this, I'll be happy to give feedback. Should be fairly simple.