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.

Why is post_load called after schema.validate()?

See original GitHub issue

Hello,

I noticed that @post_load is called when calling schema_object.validate().

Then I went to the library code and saw this (everything seemed OK, because I was getting a feeling postprocessing will not take a place):

image

It was obvious I will need some debugging, so I went through the calling stacktrace and got surprised when I saw the marshalling module calls the deserialize method which calls schema_object.load(). I don’t think it is expected to have post_load called when you call schema_object.validate(), because in my case, we are performing some intensive operations, e.g. b64 decoding some string data and it takes some time. In this way, we have post_load called twice - the first time when a controller validates the input and the other when we actually load the data into a dict.

Of course, this is not a bug, but an open question.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
deckar01commented, Sep 18, 2019

I was able to repro on 3.2.0. Here is a minimal repro case:

from marshmallow import Schema, fields, post_load

class Foo(Schema):
    bar = fields.Str()
    
    @post_load
    def test_post_load(self, data, **kwargs):
        assert False

class Test(Schema):
    foo = fields.Nested(Foo)

Test().validate({'foo': {'bar': 'test'}})
# AssertionError

This is an artifact of validation calling _do_load under the hood. Propagating postprocess through the field load calls would work, but this might be a good opportunity to consider separating validation from loading. There has been recurring interest in validating objects that have already been deserialized.

1reaction
deckar01commented, Jun 4, 2019

I don’t think it is expected to have post_load called when you call schema_object.validate()

from marshmallow import Schema, fields, post_load

class Test(Schema):
    foo = fields.Str()
    
    @post_load
    def test_post_load(self, data):
        assert False

Test().validate({'foo': 'test'})
# {}

It doesn’t seem to be called in marshmallow 2 or 3.

Of course, this is not a bug, but an open question.

If post_load was being called during validate it would be a bug. I can’t reproduce that behavior.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Extending Schemas — marshmallow 3.19.0 documentation
Data pre-processing and post-processing methods can be registered using the pre_load , post_load , pre_dump , and post_dump decorators. from marshmallow import ...
Read more >
Is there any post_load in pydantic? - python - Stack Overflow
It is not obvious but pydantic's validator returns value of the field. So there are two ways to handle post_load conversions: validator and ......
Read more >
Object validation and conversion with Marshmallow in Python
The bookMark = BookMarkSchema() schema is responsible for deserializing one single dataset, (the POST , READ and UPDATE routes) when ...
Read more >
Events - Doctrine Object Relational Mapper (ORM)
It is called inside EntityManager::flush() after the changes to all the ... When using Doctrine\ORM\AbstractQuery::toIterable() , postLoad events will be ...
Read more >
Doctrine postLoad Listener > API Platform Part 3 - SymfonyCasts
Because our property is called isMvp , we need a getIsMvp() method. ... downside to this solution is that the postLoad listener is...
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