From 866904ab80605351c97c922db76c0586924403dd Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Sun, 14 Jul 2019 11:39:30 +0300 Subject: [PATCH] Revert "Let context-managers for raises and warns handle unknown keyword arguments" This reverts commit dfe54cd82f55f17f3c9e6e078325f306a046b93b. 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. --- src/_pytest/python_api.py | 9 ++++++--- src/_pytest/recwarn.py | 7 ++++++- testing/python/raises.py | 6 ++++++ testing/test_recwarn.py | 6 ++++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 7c63a3588..aae5ced33 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -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): diff --git a/src/_pytest/recwarn.py b/src/_pytest/recwarn.py index b124c69d5..7e772aa35 100644 --- a/src/_pytest/recwarn.py +++ b/src/_pytest/recwarn.py @@ -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): diff --git a/testing/python/raises.py b/testing/python/raises.py index 1f5594c8a..668be57fc 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -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) diff --git a/testing/test_recwarn.py b/testing/test_recwarn.py index 65fdd1682..208dc5b44 100644 --- a/testing/test_recwarn.py +++ b/testing/test_recwarn.py @@ -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)