-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-82012: Deprecate bitwise inversion (~) of bool #103487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1f0351a
3632ba5
2cbfe2d
e5ec711
2db5864
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,8 +58,22 @@ def test_math(self): | |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should test this behavior separately (doing something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean instead of or in addition to the current test? We originally had There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have both. The existing tests check that the deprecation warning is emitted correctly at runtime. The new tests would ensure that if the operation occurs at compile time, we still emit the DeprecationWarning. |
||
# 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) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
The bitwise inversion operator (``~``) on bool is deprecated. | ||
It returns the bitwise inversion of the underlying ``int`` representation such that | ||
``bool(~True) == True``, which can be confusing. Use ``not`` for logical negation | ||
of bools. In the rare case that you really need the bitwise inversion of the underlying ``int``, | ||
convert to int explicitly ``~int(x)``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello. The removal of this
ref
tag broke intersphinx downstream. I have not tracked down yet which middleware is calling this tag when API docstring has a "bool" mentioned. I suspect it is numpydoc. Is it not possible to reuse this tag for your new section above?xref astropy/astropy#15428
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you open a PR, I can backport it to the 3.12 branch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Please see #110371 . Thank you for your consideration!