marshmallow-annotations icon indicating copy to clipboard operation
marshmallow-annotations copied to clipboard

Handle children of types registered in default registry

Open justanr opened this issue 7 years ago • 4 comments

ala singledispatch

If I have ClassA registered with a schema, then I should be able to use ClassB(ClassA) in a declaration and this lib will see that while ClassB itself isn't registered it's parent is and use that schema.

Edge Case:

class A:
    ...

class B:
    ...

# register both as schema

class C(A, B):
    pass

what do now? My knee jerk is to use A's schema since it's first in the MRO order.

Additionally, if no schema is found for the child class, should the chosen schema be pushed into the registry as the schema for that type?

justanr avatar May 07 '18 03:05 justanr

It will be very helpful for using marshmallow-enum. Because now there are no way to register EnumField for all enums

unknownlighter avatar Feb 06 '19 09:02 unknownlighter

You wouldn't want to register one field for so enums since dealing with the serialization process involves looking at that particular enum and its values.

We could look at adding an enum registration helper. I'd like to avoid bringing in ma_enum if possible but enough people have requested it that it might be worth it.

justanr avatar Feb 06 '19 23:02 justanr

dealing with the serialization process involves looking at that particular enum and its values

For dealing with it BaseConverter should pass typehint to field_constructor (according FieldFactory should accept typehint argument). Together with parent class registration it allow to register EnumField for all enums. Also it allow base class registration for many other cases, where FieldFactory can produce proper field for concrete base class implementation. Cons of this proposal is possible backward incompatibility issue

unknownlighter avatar Feb 07 '19 06:02 unknownlighter

Hmm. I'd be willing to look at a PoC for that.

As for this proposal in general, my current line of thinking is to use functools.singledispatch since it already exists and deals with quite a few corner cases but I'm not 100% sold on that yet

justanr avatar Feb 07 '19 14:02 justanr