You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With schema inheritance or mixins, post_load decorators execution is dependant of their function name.
In the following example:
frommarshmallowimportSchema, fields, post_loadclassSchema1(Schema):
toto=fields.String()
@post_loaddefschema1_post_load(self, data, **kwargs):
print('Schema1 post load: data = %s'%data)
returndataclassSchema2(Schema):
toto=fields.String()
@post_loaddefschema2_post_load(self, data, **kwargs):
print('Schema2 post load: data = %s'%data)
returndataclassMixinSchema(Schema2, Schema1):
titi=fields.String()
@post_loaddefmixinshchema_post_load(self, data, **kwargs):
print('MixinSchema post load: data = %s'%data)
returndataif__name__=="__main__":
payload= {
'toto': 'gfdgefrh',
'titi': 'fdghfgjhjh'
}
result=MixinSchema().load(payload)
This example produces the following output:
MixinSchema post load: data = {'titi': 'fdghfgjhjh', 'toto': 'gfdgefrh'}
Schema1 post load: data = {'titi': 'fdghfgjhjh', 'toto': 'gfdgefrh'}
Schema2 post load: data = {'titi': 'fdghfgjhjh', 'toto': 'gfdgefrh'}
As per the decorators documentation i understand that the execution order is not guaranteed, so the output is fine by me because all the post_loads are executed.
Then, when i give the MixinSchema post_load function the same name as Schema2:
frommarshmallowimportSchema, fields, post_loadclassSchema1(Schema):
toto=fields.String()
@post_loaddefschema1_post_load(self, data, **kwargs):
print('Schema1 post load: data = %s'%data)
returndataclassSchema2(Schema):
toto=fields.String()
@post_loaddefschema2_post_load(self, data, **kwargs):
print('Schema2 post load: data = %s'%data)
returndataclassMixinSchema(Schema2, Schema1):
titi=fields.String()
@post_loaddefschema2_post_load(self, data, **kwargs):
print('MixinSchema post load: data = %s'%data)
returndataif__name__=="__main__":
payload= {
'toto': 'gfdgefrh',
'titi': 'fdghfgjhjh'
}
result=MixinSchema().load(payload)
Then i get the following output:
Schema1 post load: data = {'titi': 'fdghfgjhjh', 'toto': 'gfdgefrh'}
MixinSchema post load: data = {'titi': 'fdghfgjhjh', 'toto': 'gfdgefrh'}
The Schema2 post_load is not executed. From my point of view, post_load execution should not be dependant of function naming.
The text was updated successfully, but these errors were encountered:
cimourdain
changed the title
post_load
post_load decorated functions with same name are not executed
Oct 31, 2019
This is standard Python inheritance. You're overridding the method.
Note that if you didn't decorate the override method in the mixin, it wouldn't even be called.
You could call the overridden method from the mixin using super().
Overall, I think I'm still in favour of replacing the decoration mechanism by hooks. By hooks, I mean the Base schema would have post_load etc. no-op methods that would be called in the process and the user would override those.
The downside is that you'd need to merge methods if you have several post_load processings, but when you do, if you rely on the execution order, you need to merge them anyway, so...
I agree with you, I too would be in favour of using hook. With schemas with large inheritance/mixin trees it is sometimes hard to find that you override the post_load decorated method (as it is often called def post_load() in all schemas).
With schema inheritance or mixins, post_load decorators execution is dependant of their function name.
In the following example:
This example produces the following output:
As per the decorators documentation i understand that the execution order is not guaranteed, so the output is fine by me because all the post_loads are executed.
Then, when i give the
MixinSchema
post_load function the same name asSchema2
:Then i get the following output:
The Schema2 post_load is not executed. From my point of view, post_load execution should not be dependant of function naming.
The text was updated successfully, but these errors were encountered: