summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorTim Hoffmann <2836374+timhoffm@users.noreply.github.com>2023-05-03 07:00:42 (GMT)
committerGitHub <noreply@github.com>2023-05-03 07:00:42 (GMT)
commitfdb3ef8c0f94c7e55870a585dc6499aca46f9f90 (patch)
tree8a2c2ce856fe00afc77a99049f3bfa331068069d /Lib
parent5b05b013ff13032ffc4db07108a507c08b3a604d (diff)
downloadcpython-fdb3ef8c0f94c7e55870a585dc6499aca46f9f90.zip
cpython-fdb3ef8c0f94c7e55870a585dc6499aca46f9f90.tar.gz
cpython-fdb3ef8c0f94c7e55870a585dc6499aca46f9f90.tar.bz2
gh-82012: Deprecate bitwise inversion (~) of bool (#103487)
The bitwise inversion operator on bool returns the bitwise inversion of the underlying int value; i.e. `~True == -2` such that `bool(~True) == True`. It's a common pitfall that users mistake `~` as negation operator and actually want `not`. Supporting `~` is an artifact of bool inheriting from int. Since there is no real use-case for the current behavior, let's deprecate `~` on bool and later raise an error. This removes a potential source errors for users. Full reasoning: https://github.com/python/cpython/issues/82012#issuecomment-1258705971 Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_bool.py18
1 files changed, 16 insertions, 2 deletions
diff --git a/Lib/test/test_bool.py b/Lib/test/test_bool.py
index 916e22a..34ecb45 100644
--- a/Lib/test/test_bool.py
+++ b/Lib/test/test_bool.py
@@ -58,8 +58,22 @@ class BoolTest(unittest.TestCase):
self.assertEqual(-True, -1)
self.assertEqual(abs(True), 1)
self.assertIsNot(abs(True), True)
- self.assertEqual(~False, -1)
- self.assertEqual(~True, -2)
+ with self.assertWarns(DeprecationWarning):
+ # We need to put the bool in a variable, because the constant
+ # ~False is evaluated at compile time due to constant folding;
+ # consequently the DeprecationWarning would be issued during
+ # module loading and not during test execution.
+ false = False
+ self.assertEqual(~false, -1)
+ with self.assertWarns(DeprecationWarning):
+ # also check that the warning is issued in case of constant
+ # folding at compile time
+ self.assertEqual(eval("~False"), -1)
+ with self.assertWarns(DeprecationWarning):
+ true = True
+ self.assertEqual(~true, -2)
+ with self.assertWarns(DeprecationWarning):
+ self.assertEqual(eval("~True"), -2)
self.assertEqual(False+2, 2)
self.assertEqual(True+2, 3)