question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Meta not inherited

See original GitHub issue

https://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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
lafrechcommented, Oct 26, 2021

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:

    missing=int(datetime.utcnow().timestamp())

because you get the datetime at schema declaration time, not instantiation time. You must use a lambda instead.

1reaction
deckar01commented, Oct 26, 2021

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found