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.

6.0.0 requires changes to some pre_load hooks

See original GitHub issue

Just upgraded to 6.0.0. Using FlaskParser, the input to Schema.load is now a MultiDictProxy wrapping a werkzeug ImmutableMultiDict instead of a regular dictionary. This breaks a schema I have that uses a pre_load hook to converts empty strings to None. (I’m processing an HTML form with a bunch of optional number inputs similar to some of the solutions in marshmallow-code/marshmallow#713)

Not the biggest deal to work around but I thought I would mention it because I didn’t see it in the change log.

Also it would be convenient if MultiDictProxy had a __repr__ to make debugging more convenient.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
sirosencommented, Mar 16, 2020

I think the issue with pre_load hooks pre 6.0 is that they weren’t used when gathering parameters from various locations as described in #173. The pre_load hook is still called but just called with the wrong data.

Yes, you’re spot on. I had gotten myself mixed up; sorry about that. In 5.x, parsing assembled a new dict of data, which is what got passed to the schema.load method (and thereby, to pre_load and post_load).

That also explains why your hook worked in 5.x, but not in 6.0. In 5.x, you get a dict with your parameters. But the MultiDictProxy you get in 6.0 wraps the raw flask/werkzeug datatype (an ImmutableMultiDict).

I just looked, and werkzeug supports converting an ImmutableMultiDict to a (mutable) MultiDict. So I think for right now you could start your pre_load hook with a line like this

    @pre_load
    def convert_empty_string_to_none(self, data):
        # unfortunate names... makes the underlying datatype mutable
        data.data = data.data.copy()

I’m thinking about whether or not / how webargs could support this more “out of the box”. For example, should we just call copy() on all flask multidicts on their way into the proxy object? (We’d have to check out each framework on this front too.) I don’t want to rush out a solution, but this seems like a usage we need to support – rewriting the contents of the dict in a pre_load hook.

1reaction
sirosencommented, Mar 24, 2020

If you’re comfortable with us asking users (like yourself) to do the conversion, I’m happy with that. The performance penalty is probably negligible in the majority of cases, but I’m sure that for some combination of location/framework it would be an issue. It would be great to avoid it.

Maybe I should just close #491 . I can try to add something reasonable to the docs soon, once the upgrade doc is finished. The location you suggested looks good to me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Router v6 Preview
React Router contains many modules and hooks that you may not even need in your application, so depending on which pieces you import...
Read more >
node.js - Webpack issue when update 4.x to 5.x - Stack Overflow
I found the error. WebPack Hot Client is not update for the last version of webpack (5.x). If you use Webpack Hot Client,...
Read more >
Class: Puma::DSL — Puma-6.0.0
All the hooks run inside the Cluster::Worker#run method. ... option is used to allow your app and its gems to be properly reloaded...
Read more >
ACF PRO 6.0 RC 1
ACF PRO 6.0.0 RC 1 is now available for download ! ... ACF Blocks which contain no fields now preload correctly Fix –...
Read more >
Kong Gateway Changelog | Kong Docs
Fixed an issue where some items were missing from the suggestion list when ... From 3.0 onward, Kong Docker images will only contain...
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