lovasoa / marshmallow_dataclass

Automatic generation of marshmallow schemas from dataclasses.

Home Page:https://lovasoa.github.io/marshmallow_dataclass/html/marshmallow_dataclass.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Basic support for partial=True

hhalaby opened this issue · comments

This relates to #169 but instead of suggesting to return a different object type depending on partial, I suggest to simply return a dataclass with missing fields much like marshmallow does for its dicts. Otherwise the partial argument cannot be used.

If someone is specifically setting partial=True, it means they are aware of the consequences.

But (if I understand correctly) a "dataclass with missing fields" is also a different object type than the original dataclass.

E.g.

@dataclass
class C:
    x: int
    y: int

@dataclass
class PartialC:
    x: int

c = C(1,2)
pc = PartialC(1)

isinstance(c, PartialC) # => False
isinstance(pc, C) # => False

I.e. dataclasses don't support "missing fields". A dataclass without a particular set of fields is a whole new type.
If load (with partial=True) were to return a whole bunch of different (dynamically created) types based on which input fields were missing, I think the behavior would be quite confusing in practice.

@dairiki Thank you for your explanation. I think I had misunderstood at first the implications of what I suggested. More so for my specific use case of wanting to create unit tests for specific parts of a dataclass. No need to load a partial dataclass, all I have to do is load a complete one.