-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-122982: Prohibit bitwise inversion on bool type #122983
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
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
75348d6
Prohibit bitwise inversion on bool
Eclips4 8dc66a7
Fix blurb
Eclips4 77b4080
Apply suggestions from code review
Eclips4 a8fdf66
Add more test cases
Eclips4 7a3e73a
func -> class
Eclips4 9bf6fdf
Reword news
Eclips4 33b1397
Address Nikita's comment
Eclips4 4760de8
Address Petr's comment
Eclips4 a949b8a
Add a suggestion
Eclips4 0e26ff8
Add few test cases
Eclips4 d0d8ffa
Use raw strings
Eclips4 d980ef2
Apply suggestions from code review
Eclips4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Core and Builtins/2024-08-13-21-40-20.gh-issue-122982.JSLp1k.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Bitwise inversion (``~``) is prohibited for :data:`True` and :data:`False`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,17 +70,11 @@ bool_vectorcall(PyObject *type, PyObject * const*args, | |
static PyObject * | ||
bool_invert(PyObject *v) | ||
{ | ||
if (PyErr_WarnEx(PyExc_DeprecationWarning, | ||
"Bitwise inversion '~' on bool is deprecated. This " | ||
"returns the bitwise inversion of the underlying int " | ||
"object and is usually not what you expect from negating " | ||
"a bool. Use the 'not' operator for boolean negation or " | ||
"~int(x) if you really want the bitwise inversion of the " | ||
"underlying int.", | ||
1) < 0) { | ||
return NULL; | ||
} | ||
return PyLong_Type.tp_as_number->nb_invert(v); | ||
PyErr_SetString( | ||
PyExc_TypeError, | ||
"bad operand type for unary ~: 'bool'. Did you mean 'not' instead of '~'?" | ||
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. I think the text in the warning was helpful, especially since the previous behaviour has been around forever. Maybe something like:
|
||
); | ||
return NULL; | ||
} | ||
|
||
static PyObject * | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 this gets merged, you should open a new PR to move this note to the 3.14 pending removals & backport that.