Revert "Let context-managers for raises and warns handle unknown keyword arguments"

This reverts commit dfe54cd82f.

The idea in the commit was to simplify the code by removing the check
and instead letting it TypeError which has the same effect.

However this type error is caught by mypy, and rather than ignoring the
error we think it's better and clearer to go back to the previous
explicit check.
This commit is contained in:
Ran Benita 2019-07-14 11:39:30 +03:00
parent 35a57a0dfb
commit 866904ab80
4 changed files with 24 additions and 4 deletions

View File

@ -653,9 +653,12 @@ def raises(expected_exception, *args, match=None, **kwargs):
message = "DID NOT RAISE {}".format(expected_exception)
if not args:
return RaisesContext(
expected_exception, message=message, match_expr=match, **kwargs
)
if kwargs:
msg = "Unexpected keyword arguments passed to pytest.raises: "
msg += ", ".join(sorted(kwargs))
msg += "\nUse context-manager form instead?"
raise TypeError(msg)
return RaisesContext(expected_exception, message, match)
else:
func = args[0]
if not callable(func):

View File

@ -76,7 +76,12 @@ def warns(expected_warning, *args, match=None, **kwargs):
"""
__tracebackhide__ = True
if not args:
return WarningsChecker(expected_warning, match_expr=match, **kwargs)
if kwargs:
msg = "Unexpected keyword arguments passed to pytest.warns: "
msg += ", ".join(sorted(kwargs))
msg += "\nUse context-manager form instead?"
raise TypeError(msg)
return WarningsChecker(expected_warning, match_expr=match)
else:
func = args[0]
if not callable(func):

View File

@ -248,3 +248,9 @@ class TestRaises:
with pytest.raises(CrappyClass()):
pass
assert "via __class__" in excinfo.value.args[0]
def test_raises_context_manager_with_kwargs(self):
with pytest.raises(TypeError) as excinfo:
with pytest.raises(Exception, foo="bar"):
pass
assert "Unexpected keyword arguments" in str(excinfo.value)

View File

@ -374,3 +374,9 @@ class TestWarns:
assert f() == 10
assert pytest.warns(UserWarning, f) == 10
assert pytest.warns(UserWarning, f) == 10
def test_warns_context_manager_with_kwargs(self):
with pytest.raises(TypeError) as excinfo:
with pytest.warns(UserWarning, foo="bar"):
pass
assert "Unexpected keyword arguments" in str(excinfo.value)