@@ -6,14 +6,10 @@ This section documents migration paths to new releases.
6
6
Upgrading to 6.0
7
7
++++++++++++++++
8
8
9
- In webargs 6, the way that data is loaded from various locations and passed to
10
- your schemas changed significantly. This may require you to change several
11
- different parts of your webargs usage.
12
-
13
- Multiple locations are no longer supported in a single call
9
+ Multiple Locations Are No Longer Supported In A Single Call
14
10
-----------------------------------------------------------
15
11
16
- And only JSON data is parsed by default .
12
+ The default location is JSON/body .
17
13
18
14
Under webargs 5.x, you may have written code which did not specify a location.
19
15
@@ -29,23 +25,24 @@ parameter, you must be explicit and rewrite like so:
29
25
30
26
# webargs 5.x
31
27
@parser.use_args ({" q" : ma.fields.String()})
32
- def foo (q ):
33
- return some_function(user_query = q )
28
+ def foo (args ):
29
+ return some_function(user_query = args.get( " q " ) )
34
30
35
31
36
32
# webargs 6.x
37
33
@parser.use_args ({" q" : ma.fields.String()}, location = " query" )
38
- def foo (q ):
39
- return some_function(user_query = q )
34
+ def foo (args ):
35
+ return some_function(user_query = args.get( " q " ) )
40
36
41
37
This also means that another usage from 5.x is not supported. If you had code
42
38
with multiple locations in a single `use_args `, `use_kwargs `, or `parse ` call,
43
- you must write it in multiple separate `use_args ` invocations, like so:
39
+ you must write it in multiple separate `use_args ` or `use_kwargs ` invocations,
40
+ like so:
44
41
45
42
.. code-block :: python
46
43
47
44
# webargs 5.x
48
- @parser.use_args (
45
+ @parser.use_kwargs (
49
46
{
50
47
" q1" : ma.fields.Int(location = " query" ),
51
48
" q2" : ma.fields.Int(location = " query" ),
@@ -58,13 +55,13 @@ you must write it in multiple separate `use_args` invocations, like so:
58
55
59
56
60
57
# webargs 6.x
61
- @parser.use_args ({" q1" : ma.fields.Int(), " q2" : ma.fields.Int()}, location = " query" )
62
- @parser.use_args ({" h1" : ma.fields.Int()}, location = " headers" )
58
+ @parser.use_kwargs ({" q1" : ma.fields.Int(), " q2" : ma.fields.Int()}, location = " query" )
59
+ @parser.use_kwargs ({" h1" : ma.fields.Int()}, location = " headers" )
63
60
def foo (q1 , q2 , h1 ):
64
61
...
65
62
66
63
67
- Fields no longer support location=...
64
+ Fields No Longer Support location=...
68
65
-------------------------------------
69
66
70
67
Because a single `parser.use_args `, `parser.use_kwargs `, or `parser.parse ` call
@@ -75,16 +72,16 @@ to specify its location. Rewrite code like so:
75
72
76
73
# webargs 5.x
77
74
@parser.use_args ({" q" : ma.fields.String(location = " query" )})
78
- def foo (q ):
79
- return some_function(user_query = q )
75
+ def foo (args ):
76
+ return some_function(user_query = args.get( " q " ) )
80
77
81
78
82
79
# webargs 6.x
83
80
@parser.use_args ({" q" : ma.fields.String()}, location = " query" )
84
- def foo (q ):
85
- return some_function(user_query = q )
81
+ def foo (args ):
82
+ return some_function(user_query = args.get( " q " ) )
86
83
87
- location_handler has been replaced with location_loader
84
+ location_handler Has Been Replaced With location_loader
88
85
-------------------------------------------------------
89
86
90
87
This is not just a name change. The expected signature of a `location_loader `
@@ -108,17 +105,17 @@ Rewrite code like this:
108
105
def load_data (request , schema ):
109
106
return request.data
110
107
111
- Data is not filtered before being passed to your schema, and it may be proxified
108
+ Data Is Not Filtered Before Being Passed To Your Schema, And It May Be Proxified
112
109
--------------------------------------------------------------------------------
113
110
114
- In webargs 5.x, the schema you gave was used to pull data out of the request
115
- object. That data was compiled into a dictionary which was then passed to your
116
- schema.
111
+ In webargs 5.x, the deserialization schema was used to pull data out of the
112
+ request object. That data was compiled into a dictionary which was then passed
113
+ to the schema.
117
114
118
115
One of the major changes in webargs 6.x allows the use of `unknown ` parameter
119
116
on schemas. This lets a schema decide what to do with fields not specified in
120
117
the schema. In order to achieve this, webargs now passes the full data from
121
- the location you selected to your schema.
118
+ the specified location to the schema.
122
119
123
120
However, many types of request data are so-called "multidicts" -- dictionary-like
124
121
types which can return one or multiple values as you prefer. To handle
@@ -230,7 +227,7 @@ the data in-place. You may need to apply rewrites like so:
230
227
...
231
228
232
229
233
- DelimitedList now only takes a string input
230
+ DelimitedList Now Only Takes A String Input
234
231
-------------------------------------------
235
232
236
233
Combining `List ` and string parsing functionality in a single type had some
@@ -274,7 +271,7 @@ you need to allow both usages in your API, you can do a rewrite like so:
274
271
...
275
272
276
273
277
- ValidationError messages are namespaced under the location
274
+ ValidationError Messages Are Namespaced Under The Location
278
275
----------------------------------------------------------
279
276
280
277
If you were parsing ValidationError messages, you will notice a change in the
@@ -312,7 +309,7 @@ to traverse messages by one additional level. For example:
312
309
log.debug(" bad value for '{} ' [{} ]: {} " .format(field, location, messages))
313
310
314
311
315
- Some functions take keyword-only arguments now
312
+ Some Functions Take Keyword-Only Arguments Now
316
313
----------------------------------------------
317
314
318
315
The signature of several methods has changed to have keyword-only arguments.
@@ -422,26 +419,58 @@ and finally, the `dict2schema` function:
422
419
...
423
420
424
421
425
- PyramidParser now appends arguments (used to prepend )
422
+ PyramidParser Now Appends Arguments (Used To Prepend )
426
423
-----------------------------------------------------
427
424
428
425
`PyramidParser.use_args ` was not conformant with the other parsers in webargs.
429
426
While all other parsers added new arguments to the end of the argument list of
430
427
a decorated view function, the Pyramid implementation added them to the front
431
- of the argument list. This has been corrected, but as a result pyramid views
432
- with `use_args ` will need to be rewritten like so:
428
+ of the argument list.
429
+
430
+ This has been corrected, but as a result pyramid views with `use_args ` may need
431
+ to be rewritten. The `request ` object is always passed first in both versions,
432
+ so the issue is only apparent with view functions taking other positional
433
+ arguments.
434
+
435
+ For example, imagine code with a decorator for passing user information,
436
+ `pass_userinfo `, like so:
437
+
438
+ .. code-block :: python
439
+
440
+ # a decorator which gets information about the authenticated user
441
+ def pass_userinfo (f ):
442
+ def decorator (request , * args , ** kwargs ):
443
+ return f(request, get_userinfo(), * args, ** kwargs)
444
+
445
+ return decorator
446
+
447
+ You will see a behavioral change if `pass_userinfo ` is called on a function
448
+ decorated with `use_args `. The difference between the two versions will be like
449
+ so:
433
450
434
451
.. code-block :: python
435
452
436
453
from webargs.pyramidparser import use_args
437
454
438
455
# webargs 5.x
456
+ # pass_userinfo is called first, webargs sees positional arguments of
457
+ # (userinfo,)
458
+ # and changes it to
459
+ # (request, args, userinfo)
460
+ @pass_userinfo
439
461
@use_args ({" q" : ma.fields.String()}, locations = (" query" ,))
440
- def viewfunc (q , request ):
462
+ def viewfunc (request , args , userinfo ):
463
+ q = args.get(" q" )
441
464
...
442
465
443
466
444
467
# webargs 6.x
468
+ # pass_userinfo is called first, webargs sees positional arguments of
469
+ # (userinfo,)
470
+ # and changes it to
471
+ # (request, userinfo, args)
472
+ @pass_userinfo
445
473
@use_args ({" q" : ma.fields.String()}, location = " query" )
446
- def viewfunc (request , q ):
474
+ def viewfunc (request , userinfo , args ):
475
+ q = args.get(" q" )
447
476
...
0 commit comments