From a254ad0436a922cb180015cdaec1bfe1cef8eb07 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 12 Dec 2018 14:58:48 -0800 Subject: [PATCH] Raise `TypeError` for `with raises(..., match=)`. --- changelog/4538.bugfix.rst | 1 + src/_pytest/python_api.py | 2 +- testing/python/raises.py | 5 +++++ 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 changelog/4538.bugfix.rst diff --git a/changelog/4538.bugfix.rst b/changelog/4538.bugfix.rst new file mode 100644 index 000000000..0ecfb5be0 --- /dev/null +++ b/changelog/4538.bugfix.rst @@ -0,0 +1 @@ +Raise ``TypeError`` for ``with raises(..., match=)``. diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 7de8e154c..37eafa7f0 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -716,6 +716,6 @@ class RaisesContext(object): suppress_exception = issubclass(self.excinfo.type, self.expected_exception) if sys.version_info[0] == 2 and suppress_exception: sys.exc_clear() - if self.match_expr and suppress_exception: + if self.match_expr is not None and suppress_exception: self.excinfo.match(self.match_expr) return suppress_exception diff --git a/testing/python/raises.py b/testing/python/raises.py index 912d34673..2ee18b173 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -37,6 +37,11 @@ class TestRaises(object): except pytest.raises.Exception: pass + def test_raises_falsey_type_error(self): + with pytest.raises(TypeError): + with pytest.raises(AssertionError, match=0): + raise AssertionError("ohai") + def test_raises_repr_inflight(self): """Ensure repr() on an exception info inside a pytest.raises with block works (#4386)"""