Meta not inherited
See original GitHub issuehttps://github.com/marshmallow-code/marshmallow/issues/162
This issue which was solved a while ago seems to have came back. Meta on a baseschema, particularly ordered, is not carrying through or being inherited onto the next schema.
from datetime import datetime
from marshmallow import Schema, fields, validate
class BaseSchema(Schema):
created_at = fields.Integer(validate=validate.Range(min=0), missing=int(datetime.utcnow().timestamp()))
updated_at = fields.Integer(validate=validate.Range(min=0), missing=None)
class Meta:
ordered = True
class AlbumSchema(BaseSchema):
title = fields.Str()
album = dict(title="Hunky Dory")
result = AlbumSchema().load(album)
result = AlbumSchema().dump(result)
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
meta class doesn't get inherited - Stack Overflow
Since you return type(name, bases, attrs) you get an object of type type instead of Meta . You can check this by printing...
Read more >Python – Can't use an inheriting Django model's Meta class to ...
I would like to use properties from an inheriting model's Meta class to configure a field defined in an abstract model higher up...
Read more >Metaclasses - mypy 0.991 documentation
Note that metaclasses pose some requirements on the inheritance structure, so it's better not to combine metaclasses and class hierarchies:.
Read more >16. Metaclasses | OOP | python-course.eu
Metaclasses are not supported by every object oriented ... like any other Python class, but they are classes that inherit from "type".
Read more >Only first meta inner class inherited with multiple abstract base ...
This is a matter of when using abstract base classes, the meta is not inherited for multiple abstract base classes. Only the first...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
ordered
does have an effect because the order of the fields depends on the set class being used. See #1744, where I propose to use an ordered set by default to get ordered schemas for (almost ?) free.So we do need
ordered
to be inherited. But I agree meta attribute should be inherited, though. In fact, I use the same pattern in my apps: define an ordered base schema class.As a sidenote, this is wrong:
because you get the datetime at schema declaration time, not instantiation time. You must use a lambda instead.
I am unable to reproduce this with python 3.9 on marshmallow 3.14.0. I added
assert(AlbumSchema.Meta == BaseSchema.Meta)
to that example and it passes.I assume you are on python 3 based on the timestamp usage. What version of marshmallow are you using?