cattrs icon indicating copy to clipboard operation
cattrs copied to clipboard

Extending Coverter instances?

Open jlumpe opened this issue 3 years ago • 3 comments

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.

jlumpe avatar Jan 06 '22 23:01 jlumpe

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?

Tinche avatar Jan 07 '22 18:01 Tinche

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.

jlumpe avatar Jan 07 '22 22:01 jlumpe

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.

Tinche avatar Jan 09 '22 22:01 Tinche