Skip to content

Commit

Permalink
Fix test for UserWarning for older python versions; add note in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sloria committed Sep 27, 2015
1 parent bf67cc8 commit c58449a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
8 changes: 7 additions & 1 deletion docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,17 @@ When you need more flexibility in defining input schemas, you can pass a marshma
last_name = fields.Str(missing='')
date_registered = fields.DateTime(dump_only=True)
class Meta:
strict = True
@use_args(UserSchema)
@use_args(UserSchema())
def profile_view(args):
# ...
.. note::
You should always set ``strict=True`` (either as a ``class Meta`` option or in the Schema's constructor) when passing a schema to webargs. This will ensure that the parser's error handler is invoked when expected.

Next Steps
----------

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ universal = 1

[flake8]
ignore = E127,E128,E265,E302,N803,N804,N806,E731,E402
max-line-length = 95
max-line-length = 100
exclude = .git,.ropeproject,.tox,docs,.git,setup.py,compat.py,build
10 changes: 6 additions & 4 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ class UserSchema(Schema):
def test_passing_schema_to_parse(self, parser, web_request):
web_request.json = {'id': 12, 'email': 'foo@bar.com', 'password': 'bar'}

result = parser.parse(self.UserSchema(), web_request)
result = parser.parse(self.UserSchema(strict=True), web_request)

assert result == {'email': 'foo@bar.com', 'password': 'bar'}

Expand All @@ -466,9 +466,11 @@ def viewfunc(email, password):
return {'email': email, 'password': password}
assert viewfunc() == {'email': 'foo@bar.com', 'password': 'bar'}

def test_warning_raised_if_schema_is_not_in_strict_mode(self, web_request, parser):
with pytest.warns(UserWarning):
parser.parse(self.UserSchema(strict=False), web_request)
def test_warning_raised_if_schema_is_not_in_strict_mode(
self, web_request, parser, recwarn):
parser.parse(self.UserSchema(strict=False), web_request)
warning = recwarn.pop(UserWarning)
assert 'strict=True' in str(warning.message)

def test_error_handler_is_called_when_regardless_of_schema_strict_setting(self,
web_request, parser):
Expand Down
2 changes: 1 addition & 1 deletion webargs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def load(self, data, argmap):
schema = argmap2schema(argmap)()
if not schema.strict:
warnings.warn("It is highly recommended that you set strict=True on your schema "
"so that the parser's error handler will get invoked.", UserWarning)
"so that the parser's error handler will be invoked when expected.", UserWarning)

return schema.load(data)

Expand Down

0 comments on commit c58449a

Please sign in to comment.