|
4 | 4 | from test.support import os_helper |
5 | 5 |
|
6 | 6 | import os |
| 7 | +import re |
| 8 | + |
7 | 9 |
|
8 | 10 | class BoolTest(unittest.TestCase): |
9 | 11 |
|
@@ -58,22 +60,6 @@ def test_math(self): |
58 | 60 | self.assertEqual(-True, -1) |
59 | 61 | self.assertEqual(abs(True), 1) |
60 | 62 | self.assertIsNot(abs(True), True) |
61 | | - with self.assertWarns(DeprecationWarning): |
62 | | - # We need to put the bool in a variable, because the constant |
63 | | - # ~False is evaluated at compile time due to constant folding; |
64 | | - # consequently the DeprecationWarning would be issued during |
65 | | - # module loading and not during test execution. |
66 | | - false = False |
67 | | - self.assertEqual(~false, -1) |
68 | | - with self.assertWarns(DeprecationWarning): |
69 | | - # also check that the warning is issued in case of constant |
70 | | - # folding at compile time |
71 | | - self.assertEqual(eval("~False"), -1) |
72 | | - with self.assertWarns(DeprecationWarning): |
73 | | - true = True |
74 | | - self.assertEqual(~true, -2) |
75 | | - with self.assertWarns(DeprecationWarning): |
76 | | - self.assertEqual(eval("~True"), -2) |
77 | 63 |
|
78 | 64 | self.assertEqual(False+2, 2) |
79 | 65 | self.assertEqual(True+2, 3) |
@@ -169,6 +155,25 @@ def test_math(self): |
169 | 155 | self.assertIs(not True, False) |
170 | 156 | self.assertIs(not False, True) |
171 | 157 |
|
| 158 | + def test_invert(self): |
| 159 | + # See gh-149532 |
| 160 | + msg = re.escape("bad operand type for unary ~: 'bool'") |
| 161 | + |
| 162 | + # Check constants in case of a folding: |
| 163 | + with self.assertRaisesRegex(TypeError, msg): |
| 164 | + ~False |
| 165 | + with self.assertRaisesRegex(TypeError, msg): |
| 166 | + ~True |
| 167 | + |
| 168 | + # Check variable: |
| 169 | + for bool_val in [True, False]: |
| 170 | + with self.subTest(bool_val), self.assertRaisesRegex(TypeError, msg): |
| 171 | + ~bool_val |
| 172 | + |
| 173 | + # Check `eval`: |
| 174 | + with self.assertRaisesRegex(TypeError, msg): |
| 175 | + eval("~False") |
| 176 | + |
172 | 177 | def test_convert(self): |
173 | 178 | self.assertRaises(TypeError, bool, 42, 42) |
174 | 179 | self.assertIs(bool(10), True) |
|
0 commit comments