Skip to content

Commit

Permalink
Merge pull request #822 from marshmallow-code/load_default
Browse files Browse the repository at this point in the history
Fix warnings in tests
  • Loading branch information
lafrech committed Apr 6, 2023
2 parents 85d17bd + 6f82cb4 commit be86d0d
Show file tree
Hide file tree
Showing 25 changed files with 58 additions and 50 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ Your code will need to be updated to use ``Fields`` rather than ``Args``.
args = {
"name": fields.Str(required=True),
"password": fields.Str(validate=lambda p: len(p) >= 6),
"display_per_page": fields.Int(missing=10),
"display_per_page": fields.Int(load_default=10),
"nickname": fields.List(fields.Str()),
"content_type": fields.Str(load_from="Content-Type"),
"location": fields.Nested({"city": fields.Str(), "state": fields.Str()}),
Expand Down
10 changes: 5 additions & 5 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ When you need more flexibility in defining input schemas, you can pass a marshma
id = fields.Int(dump_only=True) # read-only (won't be parsed by webargs)
username = fields.Str(required=True)
password = fields.Str(load_only=True) # write-only
first_name = fields.Str(missing="")
last_name = fields.Str(missing="")
first_name = fields.Str(load_default="")
last_name = fields.Str(load_default="")
date_registered = fields.DateTime(dump_only=True)
Expand All @@ -121,7 +121,7 @@ When you need more flexibility in defining input schemas, you can pass a marshma
# You can add additional parameters
@use_kwargs({"posts_per_page": fields.Int(missing=10)}, location="query")
@use_kwargs({"posts_per_page": fields.Int(load_default=10)}, location="query")
@use_args(UserSchema())
def profile_posts(args, posts_per_page):
username = args["username"]
Expand Down Expand Up @@ -305,8 +305,8 @@ Consider the following use cases:
id = fields.Int(dump_only=True)
username = fields.Str(required=True)
password = fields.Str(load_only=True)
first_name = fields.Str(missing="")
last_name = fields.Str(missing="")
first_name = fields.Str(load_default="")
last_name = fields.Str(load_default="")
date_registered = fields.DateTime(dump_only=True)
Expand Down
6 changes: 3 additions & 3 deletions docs/framework_support.rst
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ The `parse <webargs.aiohttpparser.AIOHTTPParser.parse>` method of `AIOHTTPParser
from webargs import fields
from webargs.aiohttpparser import parser
handler_args = {"name": fields.Str(missing="World")}
handler_args = {"name": fields.Str(load_default="World")}
async def handler(request):
Expand Down Expand Up @@ -389,7 +389,7 @@ The :meth:`use_args <webargs.aiohttpparser.AIOHTTPParser.use_args>` and :meth:`u
from webargs import fields
from webargs.aiohttpparser import use_kwargs
hello_args = {"name": fields.Str(missing="World")}
hello_args = {"name": fields.Str(load_default="World")}
# The following are equivalent
Expand Down Expand Up @@ -439,7 +439,7 @@ The preferred way to apply decorators to Bottle routes is using the
from bottle import route
user_args = {"name": fields.Str(missing="Friend")}
user_args = {"name": fields.Str(load_default="Friend")}
@route("/users/<_id:int>", method="GET", apply=use_args(user_args))
Expand Down
2 changes: 1 addition & 1 deletion docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Arguments are specified as a dictionary of name -> :class:`Field <marshmallow.fi
# OR use marshmallow's built-in validators
"password": fields.Str(validate=validate.Length(min=6)),
# Default value when argument is missing
"display_per_page": fields.Int(missing=10),
"display_per_page": fields.Int(load_default=10),
# Repeated parameter, e.g. "/?nickname=Fred&nickname=Freddie"
"nickname": fields.List(fields.Str()),
# Delimited list, e.g. "/?languages=python,javascript"
Expand Down
6 changes: 4 additions & 2 deletions examples/aiohttp_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from webargs import fields, validate
from webargs.aiohttpparser import use_args, use_kwargs

hello_args = {"name": fields.Str(missing="Friend")}
hello_args = {"name": fields.Str(load_default="Friend")}


@use_args(hello_args)
Expand All @@ -41,7 +41,9 @@ async def add(request, x, y):
dateadd_args = {
"value": fields.Date(required=False),
"addend": fields.Int(required=True, validate=validate.Range(min=1)),
"unit": fields.Str(missing="days", validate=validate.OneOf(["minutes", "days"])),
"unit": fields.Str(
load_default="days", validate=validate.OneOf(["minutes", "days"])
),
}


Expand Down
2 changes: 1 addition & 1 deletion examples/annotations_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class User(Model):


@route("/", methods=["GET"])
def index(name: fields.Str(missing="Friend")): # noqa: F821
def index(name: fields.Str(load_default="Friend")): # noqa: F821
return {"message": f"Hello, {name}!"}


Expand Down
6 changes: 4 additions & 2 deletions examples/bottle_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from webargs.bottleparser import use_args, use_kwargs


hello_args = {"name": fields.Str(missing="Friend")}
hello_args = {"name": fields.Str(load_default="Friend")}


@route("/", method="GET", apply=use_args(hello_args))
Expand All @@ -40,7 +40,9 @@ def add(x, y):
dateadd_args = {
"value": fields.Date(required=False),
"addend": fields.Int(required=True, validate=validate.Range(min=1)),
"unit": fields.Str(missing="days", validate=validate.OneOf(["minutes", "days"])),
"unit": fields.Str(
load_default="days", validate=validate.OneOf(["minutes", "days"])
),
}


Expand Down
4 changes: 2 additions & 2 deletions examples/falcon_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def hook(req, resp, params):
class HelloResource:
"""A welcome page."""

hello_args = {"name": fields.Str(missing="Friend", location="query")}
hello_args = {"name": fields.Str(load_default="Friend", location="query")}

@use_args(hello_args)
def on_get(self, req, resp, args):
Expand All @@ -71,7 +71,7 @@ class DateAddResource:
"value": fields.Date(required=False),
"addend": fields.Int(required=True, validate=validate.Range(min=1)),
"unit": fields.Str(
missing="days", validate=validate.OneOf(["minutes", "days"])
load_default="days", validate=validate.OneOf(["minutes", "days"])
),
}

Expand Down
6 changes: 4 additions & 2 deletions examples/flask_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

app = Flask(__name__)

hello_args = {"name": fields.Str(missing="Friend")}
hello_args = {"name": fields.Str(load_default="Friend")}


@app.route("/", methods=["GET"])
Expand Down Expand Up @@ -51,7 +51,9 @@ async def subtract(x, y):
dateadd_args = {
"value": fields.Date(required=False),
"addend": fields.Int(required=True, validate=validate.Range(min=1)),
"unit": fields.Str(missing="days", validate=validate.OneOf(["minutes", "days"])),
"unit": fields.Str(
load_default="days", validate=validate.OneOf(["minutes", "days"])
),
}


Expand Down
4 changes: 2 additions & 2 deletions examples/flaskrestful_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class IndexResource(Resource):
"""A welcome page."""

hello_args = {"name": fields.Str(missing="Friend")}
hello_args = {"name": fields.Str(load_default="Friend")}

@use_args(hello_args)
def get(self, args):
Expand All @@ -50,7 +50,7 @@ class DateAddResource(Resource):
"value": fields.Date(required=False),
"addend": fields.Int(required=True, validate=validate.Range(min=1)),
"unit": fields.Str(
missing="days", validate=validate.OneOf(["minutes", "days"])
load_default="days", validate=validate.OneOf(["minutes", "days"])
),
}

Expand Down
6 changes: 4 additions & 2 deletions examples/pyramid_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from webargs.pyramidparser import use_args, use_kwargs


hello_args = {"name": fields.Str(missing="Friend")}
hello_args = {"name": fields.Str(load_default="Friend")}


@view_config(route_name="hello", request_method="GET", renderer="json")
Expand All @@ -46,7 +46,9 @@ def add(request, x, y):
dateadd_args = {
"value": fields.Date(required=False),
"addend": fields.Int(required=True, validate=validate.Range(min=1)),
"unit": fields.Str(missing="days", validate=validate.OneOf(["minutes", "days"])),
"unit": fields.Str(
load_default="days", validate=validate.OneOf(["minutes", "days"])
),
}


Expand Down
2 changes: 1 addition & 1 deletion examples/schema_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def user_detail(reqargs, user_id):

# You can add additional arguments with use_kwargs
@app.route("/users/", methods=["GET", "POST"])
@use_kwargs({"limit": fields.Int(missing=10, location="query")})
@use_kwargs({"limit": fields.Int(load_default=10, location="query")})
@use_schema(UserSchema, list_view=True)
def user_list(reqargs, limit):
users = db["users"].values()
Expand Down
4 changes: 2 additions & 2 deletions examples/tornado_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def write_error(self, status_code, **kwargs):
class HelloHandler(BaseRequestHandler):
"""A welcome page."""

hello_args = {"name": fields.Str(missing="Friend")}
hello_args = {"name": fields.Str(load_default="Friend")}

@use_args(hello_args)
def get(self, args):
Expand All @@ -62,7 +62,7 @@ class DateAddHandler(BaseRequestHandler):
"value": fields.Date(required=False),
"addend": fields.Int(required=True, validate=validate.Range(min=1)),
"unit": fields.Str(
missing="days", validate=validate.OneOf(["minutes", "days"])
load_default="days", validate=validate.OneOf(["minutes", "days"])
),
}

Expand Down
2 changes: 1 addition & 1 deletion src/webargs/bottleparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from webargs.bottleparser import use_args
hello_args = {
'name': fields.Str(missing='World')
'name': fields.Str(load_default='World')
}
@route('/', method='GET', apply=use_args(hello_args))
def index(args):
Expand Down
2 changes: 1 addition & 1 deletion src/webargs/djangoparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from webargs.djangoparser import use_args
hello_args = {
'name': fields.Str(missing='World')
'name': fields.Str(load_default='World')
}
class MyView(View):
Expand Down
2 changes: 1 addition & 1 deletion src/webargs/pyramidparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from webargs.pyramidparser import use_args
hello_args = {
'name': fields.Str(missing='World')
'name': fields.Str(load_default='World')
}
@use_args(hello_args)
Expand Down
2 changes: 1 addition & 1 deletion src/webargs/tornadoparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class HelloHandler(tornado.web.RequestHandler):
@use_args({'name': fields.Str(missing='World')})
@use_args({'name': fields.Str(load_default='World')})
def get(self, args):
response = {'message': 'Hello {}'.format(args['name'])}
self.write(response)
Expand Down
4 changes: 2 additions & 2 deletions tests/apps/aiohttp_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from webargs.aiohttpparser import parser, use_args, use_kwargs
from webargs.core import json

hello_args = {"name": fields.Str(missing="World", validate=lambda n: len(n) >= 3)}
hello_args = {"name": fields.Str(load_default="World", validate=lambda n: len(n) >= 3)}
hello_multiple = {"name": fields.List(fields.Str())}


class HelloSchema(ma.Schema):
name = fields.Str(missing="World", validate=lambda n: len(n) >= 3)
name = fields.Str(load_default="World", validate=lambda n: len(n) >= 3)


hello_many_schema = HelloSchema(many=True)
Expand Down
4 changes: 2 additions & 2 deletions tests/apps/bottle_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from webargs.core import json


hello_args = {"name": fields.Str(missing="World", validate=lambda n: len(n) >= 3)}
hello_args = {"name": fields.Str(load_default="World", validate=lambda n: len(n) >= 3)}
hello_multiple = {"name": fields.List(fields.Str())}


class HelloSchema(ma.Schema):
name = fields.Str(missing="World", validate=lambda n: len(n) >= 3)
name = fields.Str(load_default="World", validate=lambda n: len(n) >= 3)


hello_many_schema = HelloSchema(many=True)
Expand Down
4 changes: 2 additions & 2 deletions tests/apps/django_app/echo/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
from webargs.core import json


hello_args = {"name": fields.Str(missing="World", validate=lambda n: len(n) >= 3)}
hello_args = {"name": fields.Str(load_default="World", validate=lambda n: len(n) >= 3)}
hello_multiple = {"name": fields.List(fields.Str())}


class HelloSchema(ma.Schema):
name = fields.Str(missing="World", validate=lambda n: len(n) >= 3)
name = fields.Str(load_default="World", validate=lambda n: len(n) >= 3)


hello_many_schema = HelloSchema(many=True)
Expand Down
6 changes: 3 additions & 3 deletions tests/apps/falcon_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
from webargs.core import json
from webargs.falconparser import parser, use_args, use_kwargs

hello_args = {"name": fields.Str(missing="World", validate=lambda n: len(n) >= 3)}
hello_args = {"name": fields.Str(load_default="World", validate=lambda n: len(n) >= 3)}
hello_multiple = {"name": fields.List(fields.Str())}

FALCON_MAJOR_VERSION = int(falcon.__version__.split(".")[0])
FALCON_SUPPORTS_ASYNC = FALCON_MAJOR_VERSION >= 3


class HelloSchema(ma.Schema):
name = fields.Str(missing="World", validate=lambda n: len(n) >= 3)
name = fields.Str(load_default="World", validate=lambda n: len(n) >= 3)


hello_many_schema = HelloSchema(many=True)
Expand Down Expand Up @@ -137,7 +137,7 @@ def always_fail(value):
class EchoHeaders:
def on_get(self, req, resp):
class HeaderSchema(ma.Schema):
NAME = fields.Str(missing="World")
NAME = fields.Str(load_default="World")

resp.body = json.dumps(parser.parse(HeaderSchema(), req, location="headers"))

Expand Down
4 changes: 2 additions & 2 deletions tests/apps/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ class TestAppConfig:
TESTING = True


hello_args = {"name": fields.Str(missing="World", validate=lambda n: len(n) >= 3)}
hello_args = {"name": fields.Str(load_default="World", validate=lambda n: len(n) >= 3)}
hello_multiple = {"name": fields.List(fields.Str())}


class HelloSchema(ma.Schema):
name = fields.Str(missing="World", validate=lambda n: len(n) >= 3)
name = fields.Str(load_default="World", validate=lambda n: len(n) >= 3)


hello_many_schema = HelloSchema(many=True)
Expand Down
4 changes: 2 additions & 2 deletions tests/apps/pyramid_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from webargs.pyramidparser import parser, use_args, use_kwargs
from webargs.core import json

hello_args = {"name": fields.Str(missing="World", validate=lambda n: len(n) >= 3)}
hello_args = {"name": fields.Str(load_default="World", validate=lambda n: len(n) >= 3)}
hello_multiple = {"name": fields.List(fields.Str())}


class HelloSchema(ma.Schema):
name = fields.Str(missing="World", validate=lambda n: len(n) >= 3)
name = fields.Str(load_default="World", validate=lambda n: len(n) >= 3)


hello_many_schema = HelloSchema(many=True)
Expand Down

0 comments on commit be86d0d

Please sign in to comment.