Merge pull request #4540 from asottile/type_error_falsey_match_value

Raise `TypeError` for `with raises(..., match=<non-None falsey value>)`.
This commit is contained in:
Bruno Oliveira 2018-12-13 06:01:44 -02:00 committed by GitHub
commit e03c1f538f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 1 deletions

View File

@ -0,0 +1 @@
Raise ``TypeError`` for ``with raises(..., match=<non-None falsey value>)``.

View File

@ -716,6 +716,6 @@ class RaisesContext(object):
suppress_exception = issubclass(self.excinfo.type, self.expected_exception) suppress_exception = issubclass(self.excinfo.type, self.expected_exception)
if sys.version_info[0] == 2 and suppress_exception: if sys.version_info[0] == 2 and suppress_exception:
sys.exc_clear() 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) self.excinfo.match(self.match_expr)
return suppress_exception return suppress_exception

View File

@ -37,6 +37,11 @@ class TestRaises(object):
except pytest.raises.Exception: except pytest.raises.Exception:
pass 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): def test_raises_repr_inflight(self):
"""Ensure repr() on an exception info inside a pytest.raises with block works (#4386)""" """Ensure repr() on an exception info inside a pytest.raises with block works (#4386)"""