From 34afded06dbcc4a2615043c3deaea64511d4287d Mon Sep 17 00:00:00 2001 From: Jeffrey Rackauckas Date: Tue, 27 Mar 2018 19:57:15 -0700 Subject: [PATCH 1/4] Update pytest.raises to raise a TypeError when an invalid keyword argument is passed. --- _pytest/python_api.py | 4 ++++ changelog/3348.bugfix.rst | 1 + testing/python/raises.py | 5 +++++ 3 files changed, 10 insertions(+) create mode 100644 changelog/3348.bugfix.rst diff --git a/_pytest/python_api.py b/_pytest/python_api.py index 69ae6ed0e..5e4776ce3 100644 --- a/_pytest/python_api.py +++ b/_pytest/python_api.py @@ -597,6 +597,10 @@ def raises(expected_exception, *args, **kwargs): message = kwargs.pop("message") if "match" in kwargs: match_expr = kwargs.pop("match") + if len(kwargs.keys()) > 0: + 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 diff --git a/changelog/3348.bugfix.rst b/changelog/3348.bugfix.rst new file mode 100644 index 000000000..7b56ebf98 --- /dev/null +++ b/changelog/3348.bugfix.rst @@ -0,0 +1 @@ +Updated `pytest.raises` to raise a TypeError when an invalid keyword argument is passed. \ No newline at end of file diff --git a/testing/python/raises.py b/testing/python/raises.py index 183259f6b..156319816 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -61,6 +61,11 @@ class TestRaises(object): 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') From a5d9fbe2b0d14a84325e87629a785eda72f7d9f4 Mon Sep 17 00:00:00 2001 From: Jeffrey Rackauckas Date: Tue, 27 Mar 2018 20:24:15 -0700 Subject: [PATCH 2/4] Change pytest.raises to use match instead of matches --- testing/test_recwarn.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_recwarn.py b/testing/test_recwarn.py index f1cf542e9..1d99a7656 100644 --- a/testing/test_recwarn.py +++ b/testing/test_recwarn.py @@ -113,7 +113,7 @@ class TestDeprecatedCall(object): 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: From bfe773bfc8a37981faec2c27ea3ba65aa04f5c5d Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 28 Mar 2018 07:30:14 -0300 Subject: [PATCH 3/4] Use shorter 'if kwargs' check as requested during review --- _pytest/python_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_pytest/python_api.py b/_pytest/python_api.py index 5e4776ce3..0b30b7ac8 100644 --- a/_pytest/python_api.py +++ b/_pytest/python_api.py @@ -597,7 +597,7 @@ def raises(expected_exception, *args, **kwargs): message = kwargs.pop("message") if "match" in kwargs: match_expr = kwargs.pop("match") - if len(kwargs.keys()) > 0: + if kwargs: msg = 'Unexpected keyword arguments passed to pytest.raises: ' msg += ', '.join(kwargs.keys()) raise TypeError(msg) From 765658130235e586542df81213f666f547377353 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 28 Mar 2018 07:31:26 -0300 Subject: [PATCH 4/4] Update changelog formatting --- changelog/3348.bugfix.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/3348.bugfix.rst b/changelog/3348.bugfix.rst index 7b56ebf98..7cf13ab2c 100644 --- a/changelog/3348.bugfix.rst +++ b/changelog/3348.bugfix.rst @@ -1 +1 @@ -Updated `pytest.raises` to raise a TypeError when an invalid keyword argument is passed. \ No newline at end of file +``pytest.raises`` now raises ``TypeError`` when receiving an unknown keyword argument.