Skip to content

Commit e03c1f5

Browse files
authored
Merge pull request #4540 from asottile/type_error_falsey_match_value
Raise `TypeError` for `with raises(..., match=<non-None falsey value>)`.
2 parents 6af674a + a254ad0 commit e03c1f5

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

changelog/4538.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Raise ``TypeError`` for ``with raises(..., match=<non-None falsey value>)``.

src/_pytest/python_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,6 @@ def __exit__(self, *tp):
716716
suppress_exception = issubclass(self.excinfo.type, self.expected_exception)
717717
if sys.version_info[0] == 2 and suppress_exception:
718718
sys.exc_clear()
719-
if self.match_expr and suppress_exception:
719+
if self.match_expr is not None and suppress_exception:
720720
self.excinfo.match(self.match_expr)
721721
return suppress_exception

testing/python/raises.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ def __call__(self):
3737
except pytest.raises.Exception:
3838
pass
3939

40+
def test_raises_falsey_type_error(self):
41+
with pytest.raises(TypeError):
42+
with pytest.raises(AssertionError, match=0):
43+
raise AssertionError("ohai")
44+
4045
def test_raises_repr_inflight(self):
4146
"""Ensure repr() on an exception info inside a pytest.raises with block works (#4386)"""
4247

0 commit comments

Comments
 (0)