Skip to content

Commit 47e9548

Browse files
Reuse and improve ergonomics of assert_raises utility
- Move assert_raises to testutils - Add optional message argument, with reasonable default - Reverse order of expr and exception type for readability - Lambda argument no longer takes parameter - Convert applicable snippets to use assert_raises
1 parent 159daf0 commit 47e9548

20 files changed

+129
-373
lines changed

tests/snippets/builtin_divmod.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1+
from testutils import assert_raises
2+
13
assert divmod(11, 3) == (3, 2)
24
assert divmod(8,11) == (0, 8)
35
assert divmod(0.873, 0.252) == (3.0, 0.11699999999999999)
46

5-
try:
6-
divmod(5, 0)
7-
except ZeroDivisionError:
8-
pass
9-
else:
10-
assert False, "Expected divmod by zero to throw ZeroDivisionError"
11-
12-
try:
13-
divmod(5.0, 0.0)
14-
except ZeroDivisionError:
15-
pass
16-
else:
17-
assert False, "Expected divmod by zero to throw ZeroDivisionError"
7+
assert_raises(ZeroDivisionError, lambda: divmod(5, 0), 'divmod by zero')
8+
assert_raises(ZeroDivisionError, lambda: divmod(5.0, 0.0), 'divmod by zero')

tests/snippets/builtin_format.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1+
from testutils import assert_raises
2+
13
assert format(5, "b") == "101"
24

3-
try:
4-
format(2, 3)
5-
except TypeError:
6-
pass
7-
else:
8-
assert False, "TypeError not raised when format is called with a number"
5+
assert_raises(TypeError, lambda: format(2, 3), 'format called with number')
96

107
assert format({}) == "{}"
118

12-
try:
13-
format({}, 'b')
14-
except TypeError:
15-
pass
16-
else:
17-
assert False, "TypeError not raised when format_spec not empty for dict"
9+
assert_raises(TypeError, lambda: format({}, 'b'), 'format_spec not empty for dict')

tests/snippets/builtin_hex.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1+
from testutils import assert_raises
2+
13
assert hex(16) == '0x10'
24
assert hex(-16) == '-0x10'
35

4-
try:
5-
hex({})
6-
except TypeError:
7-
pass
8-
else:
9-
assert False, "TypeError not raised when ord() is called with a dict"
6+
assert_raises(TypeError, lambda: hex({}), 'ord() called with dict')

tests/snippets/builtin_max.py

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from testutils import assert_raises
2+
13
# simple values
24
assert max(0, 0) == 0
35
assert max(1, 0) == 1
@@ -14,32 +16,17 @@
1416
}) == "b"
1517
assert max([1, 2], default=0) == 2
1618
assert max([], default=0) == 0
17-
try:
18-
max([])
19-
except ValueError:
20-
pass
21-
else:
22-
assert False, "ValueError was not raised"
19+
assert_raises(ValueError, lambda: max([]))
2320

2421
# key parameter
2522
assert max(1, 2, -3, key=abs) == -3
2623
assert max([1, 2, -3], key=abs) == -3
2724

2825
# no argument
29-
try:
30-
max()
31-
except TypeError:
32-
pass
33-
else:
34-
assert False, "TypeError was not raised"
26+
assert_raises(TypeError, lambda: max())
3527

3628
# one non-iterable argument
37-
try:
38-
max(1)
39-
except TypeError:
40-
pass
41-
else:
42-
assert False, "TypeError was not raised"
29+
assert_raises(TypeError, lambda: max(1))
4330

4431

4532
# custom class
@@ -64,9 +51,4 @@ class MyNotComparable():
6451
pass
6552

6653

67-
try:
68-
max(MyNotComparable(), MyNotComparable())
69-
except TypeError:
70-
pass
71-
else:
72-
assert False, "TypeError was not raised"
54+
assert_raises(TypeError, lambda: max(MyNotComparable(), MyNotComparable()))

tests/snippets/builtin_min.py

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from testutils import assert_raises
2+
13
# simple values
24
assert min(0, 0) == 0
35
assert min(1, 0) == 0
@@ -14,32 +16,18 @@
1416
}) == "a"
1517
assert min([1, 2], default=0) == 1
1618
assert min([], default=0) == 0
17-
try:
18-
min([])
19-
except ValueError:
20-
pass
21-
else:
22-
assert False, "ValueError was not raised"
19+
20+
assert_raises(ValueError, lambda: min([]))
2321

2422
# key parameter
2523
assert min(1, 2, -3, key=abs) == 1
2624
assert min([1, 2, -3], key=abs) == 1
2725

2826
# no argument
29-
try:
30-
min()
31-
except TypeError:
32-
pass
33-
else:
34-
assert False, "TypeError was not raised"
27+
assert_raises(TypeError, lambda: min())
3528

3629
# one non-iterable argument
37-
try:
38-
min(1)
39-
except TypeError:
40-
pass
41-
else:
42-
assert False, "TypeError was not raised"
30+
assert_raises(TypeError, lambda: min(1))
4331

4432

4533
# custom class
@@ -64,9 +52,4 @@ class MyNotComparable():
6452
pass
6553

6654

67-
try:
68-
min(MyNotComparable(), MyNotComparable())
69-
except TypeError:
70-
pass
71-
else:
72-
assert False, "TypeError was not raised"
55+
assert_raises(TypeError, lambda: min(MyNotComparable(), MyNotComparable()))

tests/snippets/builtin_open.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1+
from testutils import assert_raises
2+
13
fd = open('README.md')
24
assert 'RustPython' in fd.read()
35

4-
try:
5-
open('DoesNotExist')
6-
assert False
7-
except FileNotFoundError:
8-
pass
6+
assert_raises(FileNotFoundError, lambda: open('DoesNotExist'))

tests/snippets/builtin_ord.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
1+
from testutils import assert_raises
2+
13
assert ord("a") == 97
24
assert ord("é") == 233
35
assert ord("🤡") == 129313
4-
try:
5-
ord()
6-
except TypeError:
7-
pass
8-
else:
9-
assert False, "TypeError not raised when ord() is called with no argument"
10-
11-
try:
12-
ord("")
13-
except TypeError:
14-
pass
15-
else:
16-
assert False, "TypeError not raised when ord() is called with an empty string"
176

18-
try:
19-
ord("ab")
20-
except TypeError:
21-
pass
22-
else:
23-
assert False, "TypeError not raised when ord() is called with more than one character"
7+
assert_raises(TypeError, lambda: ord(), "ord() is called with no argument")
8+
assert_raises(TypeError, lambda: ord(""), "ord() is called with an empty string")
9+
assert_raises(TypeError, lambda: ord("ab"), "ord() is called with more than one character")

tests/snippets/builtin_range.py

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,4 @@
1-
def assert_raises(expr, exc_type):
2-
"""
3-
Helper function to assert `expr` raises an exception of type `exc_type`
4-
Args:
5-
expr: Callable
6-
exec_type: Exception
7-
Returns:
8-
None
9-
Raises:
10-
Assertion error on failure
11-
"""
12-
try:
13-
expr(None)
14-
except exc_type:
15-
assert True
16-
else:
17-
assert False
1+
from testutils import assert_raises
182

193
assert range(2**63+1)[2**63] == 9223372036854775808
204

@@ -29,15 +13,10 @@ def assert_raises(expr, exc_type):
2913
assert range(4, 10, 2).index(6) == 1
3014
assert range(10, 4, -2).index(8) == 1
3115

32-
# index raises value error on out of bounds
33-
assert_raises(lambda _: range(10).index(-1), ValueError)
34-
assert_raises(lambda _: range(10).index(10), ValueError)
35-
36-
# index raises value error if out of step
37-
assert_raises(lambda _: range(4, 10, 2).index(5), ValueError)
38-
39-
# index raises value error if needle is not an int
40-
assert_raises(lambda _: range(10).index('foo'), ValueError)
16+
assert_raises(ValueError, lambda: range(10).index(-1), 'out of bounds')
17+
assert_raises(ValueError, lambda: range(10).index(10), 'out of bounds')
18+
assert_raises(ValueError, lambda: range(4, 10, 2).index(5), 'out of step')
19+
assert_raises(ValueError, lambda: range(10).index('foo'), 'not an int')
4120

4221
# count tests
4322
assert range(10).count(2) == 1

tests/snippets/builtin_slice.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from testutils import assert_raises
12

23
a = []
34
assert a[:] == []
@@ -18,12 +19,7 @@
1819
assert b[0:0] == []
1920
assert b[1:0] == []
2021

21-
try:
22-
_ = b[::0]
23-
except ValueError:
24-
pass
25-
else:
26-
assert False, "Zero step slice should raise ValueError"
22+
assert_raises(ValueError, lambda: b[::0], 'zero step slice')
2723

2824
assert b[::-1] == [2, 1]
2925
assert b[1::-1] == [2, 1]

tests/snippets/division_by_zero.py

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,11 @@
1-
try:
2-
5 / 0
3-
except ZeroDivisionError:
4-
pass
5-
else:
6-
assert False, 'Expected ZeroDivisionError'
1+
from testutils import assert_raises
72

8-
try:
9-
5 / -0.0
10-
except ZeroDivisionError:
11-
pass
12-
else:
13-
assert False, 'Expected ZeroDivisionError'
3+
assert_raises(ZeroDivisionError, lambda: 5 / 0)
4+
assert_raises(ZeroDivisionError, lambda: 5 / -0.0)
5+
assert_raises(ZeroDivisionError, lambda: 5 / (2-2))
6+
assert_raises(ZeroDivisionError, lambda: 5 % 0)
7+
assert_raises(ZeroDivisionError, lambda: 5 // 0)
8+
assert_raises(ZeroDivisionError, lambda: 5.3 // (-0.0))
9+
assert_raises(ZeroDivisionError, lambda: divmod(5, 0))
1410

15-
try:
16-
5 / (2-2)
17-
except ZeroDivisionError:
18-
pass
19-
else:
20-
assert False, 'Expected ZeroDivisionError'
21-
22-
try:
23-
5 % 0
24-
except ZeroDivisionError:
25-
pass
26-
else:
27-
assert False, 'Expected ZeroDivisionError'
28-
29-
try:
30-
5 // 0
31-
except ZeroDivisionError:
32-
pass
33-
else:
34-
assert False, 'Expected ZeroDivisionError'
35-
36-
try:
37-
5.3 // (-0.0)
38-
except ZeroDivisionError:
39-
pass
40-
else:
41-
assert False, 'Expected ZeroDivisionError'
42-
43-
try:
44-
divmod(5, 0)
45-
except ZeroDivisionError:
46-
pass
47-
else:
48-
assert False, 'Expected ZeroDivisionError'
49-
50-
try:
51-
raise ZeroDivisionError('Is an ArithmeticError subclass?')
52-
except ArithmeticError:
53-
pass
54-
else:
55-
assert False, 'Expected ZeroDivisionError'
11+
assert issubclass(ZeroDivisionError, ArithmeticError)
Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from testutils import assert_raises
2+
13
# 2.456984346552728
24
res = 10**500 / (4 * 10**499 + 7 * 10**497 + 3 * 10**494)
35
assert 2.456984 <= res <= 2.456985
@@ -8,20 +10,10 @@
810

911
assert 10**500 / (2*10**(500-308)) == 5e307
1012
assert 10**500 / (10**(500-308)) == 1e308
11-
try:
12-
10**500 / (10**(500-309))
13-
except OverflowError:
14-
pass
15-
else:
16-
assert False, 'Expected overflow on too big result'
13+
assert_raises(OverflowError, lambda: 10**500 / (10**(500-309)), 'too big result')
1714

1815
# a bit more than f64::MAX = 1.7976931348623157e+308_f64
1916
assert (2 * 10**308) / 2 == 1e308
2017

2118
# when dividing too big int by a float, the operation should fail
22-
try:
23-
(2 * 10**308) / 2.0
24-
except OverflowError:
25-
pass
26-
else:
27-
assert False, 'Expected overflow on division of a big int by a float'
19+
assert_raises(OverflowError, lambda: (2 * 10**308) / 2.0, 'division of big int by float')

tests/snippets/floats.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,6 @@
11
import math
22

3-
def assert_raises(expr, exc_type):
4-
"""
5-
Helper function to assert `expr` raises an exception of type `exc_type`
6-
Args:
7-
expr: Callable
8-
exec_type: Exception
9-
Returns:
10-
None
11-
Raises:
12-
Assertion error on failure
13-
"""
14-
try:
15-
expr(None)
16-
except exc_type:
17-
assert True
18-
else:
19-
assert False
3+
from testutils import assert_raises
204

215
1 + 1.1
226

@@ -79,8 +63,8 @@ def assert_raises(expr, exc_type):
7963
assert float(b'3.14') == 3.14
8064
assert float(b'2.99e-23') == 2.99e-23
8165

82-
assert_raises(lambda _: float('foo'), ValueError)
83-
assert_raises(lambda _: float(2**10000), OverflowError)
66+
assert_raises(ValueError, lambda: float('foo'))
67+
assert_raises(OverflowError, lambda: float(2**10000))
8468

8569
# check that magic methods are implemented for ints and floats
8670

0 commit comments

Comments
 (0)