justanr / marshmallow-annotations

Allows declaring marshmallow schema through type annotations
MIT License
48 stars 12 forks source link

Handle children of types registered in default registry #10

Open justanr opened 6 years ago

justanr commented 6 years ago

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?

unknownlighter commented 5 years ago

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

justanr commented 5 years ago

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.

unknownlighter commented 5 years ago

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

justanr commented 5 years ago

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