Skip to content

Commit

Permalink
Use assertRaisesRegex instead of assertRaisesRegexp
Browse files Browse the repository at this point in the history
  • Loading branch information
kislyuk committed Dec 10, 2023
1 parent d2a4f99 commit 126795d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions ensure/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ def raises_regex(self, expected_exception, expected_regexp):
Ensures preceding predicates (specifically, :meth:`called_with()`) result in *expected_exception* being raised,
and the string representation of *expected_exception* must match regular expression *expected_regexp*.
"""
return unittest_case.assertRaisesRegexp(
return unittest_case.assertRaisesRegex(
expected_exception, expected_regexp, self._orig_subject, *self._args, **self._kwargs
)

Expand Down Expand Up @@ -937,4 +937,4 @@ def f(x: int, y: float) -> float:
check = Check()

ensure_raises = unittest_case.assertRaises
ensure_raises_regex = unittest_case.assertRaisesRegexp
ensure_raises_regex = unittest_case.assertRaisesRegex
22 changes: 11 additions & 11 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,21 @@ def h(x: str, y: int):
self.assertEqual(f(1, 2.3), 3.3)
self.assertEqual(f(1, y=2.3), 3.3)
self.assertEqual(f(y=1.2, x=3), 4.2)
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
EnsureError,
"Argument y of type <class 'int'> to <function f at .+> does not match annotation type <class 'float'>",
):
self.assertEqual(f(1, 2), 3.3)
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
EnsureError,
"Argument y of type <class 'int'> to <function f at .+> does not match annotation type <class 'float'>",
):
self.assertEqual(f(y=2, x=1), 3.3)
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
EnsureError, "Return value of <function f at .+> does not match annotation type <class 'float'>"
):
self.assertEqual(f(1, -2.3), 4)
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
EnsureError, "Return value of <function f at .+> does not match annotation type <class 'float'>"
):
self.assertEqual(f(x=1, y=-2.3), 4)
Expand Down Expand Up @@ -291,7 +291,7 @@ def f(x: int, y: float=None) -> float:
def g(x: str, y: str=5, z='untyped with default') -> str:
return x+y+str(z)
"""
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
EnsureError,
"Default argument y of type <class 'int'> to <function g at .+> does not match annotation type <class 'str'>",
):
Expand Down Expand Up @@ -321,7 +321,7 @@ def f(x: int, y: float, *args, z: int=5) -> float:
self.assertEqual(2.0, f(3, 4.0))
self.assertEqual(62.0, f(3, 4.0, 10, 20, 30))
self.assertEqual(66.0, f(3, 4.0, 10, 20, 30, z=1))
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
EnsureError,
"Argument z of type <class 'str'> to <function f at .+> does not match annotation type <class 'int'>",
):
Expand Down Expand Up @@ -350,7 +350,7 @@ def f(x: int, y: float, *args, z: int=5) -> str:
self.assertEqual("2.0abc", f(3, 4.0, "abc"))
self.assertEqual("2.0abc2.0def", f(3, 4.0, "abc", "def"))
self.assertEqual("3.0abc3.0def", f(3, 4.0, "abc", "def", z=4))
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
EnsureError,
"Argument z of type <class 'str'> to <function f at .+> does not match annotation type <class 'int'>",
):
Expand All @@ -372,7 +372,7 @@ def f(x: int, y: float, *args, z: int='not an int') -> str:
return r
"""
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
EnsureError,
"Default argument z of type <class 'str'> to <function f at .+> does not match annotation type <class 'int'>",
):
Expand All @@ -399,22 +399,22 @@ def g(self, x: int, y: float):
c = C()
self.assertEqual("3.3", c.f(1, 2.3))
self.assertRegex(repr(c.f), "<bound method C.f of <.+.C object at 0x[0-9a-fA-F]+>>")
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
EnsureError,
"Argument x of type <class 'float'> to <function (C.f|f) at .+> does not match annotation type <class 'int'>",
):
g = C().f(3.2, 1)

self.assertEqual("3.3", c.g(1, 2.3))
self.assertRegex(repr(c.g), "<bound method C.g of <.+.C object at 0x[0-9a-fA-F]+>>")
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
EnsureError,
"Argument x of type <class 'float'> to <function (C.g|g) at .+> does not match annotation type <class 'int'>",
):
g = C().g(3.2, 1)

def test_error_formatting(self):
with self.assertRaisesRegexp(Exception, "Major fail detected"):
with self.assertRaisesRegex(Exception, "Major fail detected"):
check(False).is_true().or_raise(KeyError, "{} {error} detected", "Major", error="fail")


Expand Down

0 comments on commit 126795d

Please sign in to comment.