Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3348: raise error on unknown arguments to raises #3349

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions _pytest/python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,10 @@ def raises(expected_exception, *args, **kwargs):
message = kwargs.pop("message")
if "match" in kwargs:
match_expr = kwargs.pop("match")
if kwargs:
msg = 'Unexpected keyword arguments passed to pytest.raises: '
msg += ', '.join(kwargs.keys())
raise TypeError(msg)
return RaisesContext(expected_exception, message, match_expr)
elif isinstance(args[0], str):
code, = args
Expand Down
1 change: 1 addition & 0 deletions changelog/3348.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``pytest.raises`` now raises ``TypeError`` when receiving an unknown keyword argument.
5 changes: 5 additions & 0 deletions testing/python/raises.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ def test_noclass(self):
with pytest.raises(TypeError):
pytest.raises('wrong', lambda: None)

def test_invalid_arguments_to_raises(self):
with pytest.raises(TypeError, match='unknown'):
with pytest.raises(TypeError, unknown='bogus'):
raise ValueError()

def test_tuple(self):
with pytest.raises((KeyError, ValueError)):
raise KeyError('oops')
Expand Down
2 changes: 1 addition & 1 deletion testing/test_recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def f():
pass

msg = 'Did not produce DeprecationWarning or PendingDeprecationWarning'
with pytest.raises(AssertionError, matches=msg):
with pytest.raises(AssertionError, match=msg):
if mode == 'call':
pytest.deprecated_call(f)
else:
Expand Down