gh-122982: Prohibit bitwise inversion on bool type#122983
gh-122982: Prohibit bitwise inversion on bool type#122983Eclips4 wants to merge 12 commits intopython:mainfrom
Conversation
Misc/NEWS.d/next/Core and Builtins/2024-08-13-21-40-20.gh-issue-122982.JSLp1k.rst
Outdated
Show resolved
Hide resolved
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Misc/NEWS.d/next/Core and Builtins/2024-08-13-21-40-20.gh-issue-122982.JSLp1k.rst
Outdated
Show resolved
Hide resolved
picnixz
left a comment
There was a problem hiding this comment.
Apart from Nikita's comment, this looks good to me now (TIL I cannot do class A(bool): ...).
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
|
I have made the requested changes; please review again |
|
|
||
| * :mod:`builtins`: | ||
|
|
||
| * ``~bool``, bitwise inversion on bool. |
There was a problem hiding this comment.
If this gets merged, you should open a new PR to move this note to the 3.14 pending removals & backport that.
serhiy-storchaka
left a comment
There was a problem hiding this comment.
Just wondering: what if simply set PyBool_Type.tp_as_number->nb_invert = NULL in _PyTypes_InitTypes()?
Yeah, this works. Do you think it's worth doing or should we continue with the current approach? |
|
I do not know. How does On one hand, using the same code to generate error messages guarantees that there is no difference in error messages. On other hand, we may want to have a different error message, for example add a suggestion about |
Python 3.14.0a0 (heads/remove_bool_invert-dirty:4760de8ecbb, Aug 16 2024, 09:24:28) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> True.__invert__()
-2So, this is a wrong approach.
That's sounds good. I'll add a suggestion and tests for it. |
sobolevn
left a comment
There was a problem hiding this comment.
Thank you! I don't click merge, because @encukou's comment here: #122983 (review) seems like it is better to wait for resolution.
AA-Turner
left a comment
There was a problem hiding this comment.
Wordsmithing: We seem to use "Maybe you meant" for SyntaxErrors, with "Did you mean" used more generally. Non-blocking suggestion though.
A
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
hauntsaninja
left a comment
There was a problem hiding this comment.
I mentioned this on the issue, but I think in this case we should extend the deprecation period beyond the minimum #122982 (comment)
| 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.
I think the text in the warning was helpful, especially since the previous behaviour has been around forever.
Maybe something like:
bad operand type for unary ~: 'bool'. Did you mean 'not' instead of '~'? Use 'not x' for boolean negation and '~int(x)` in the rare case you want bitwise inversion of the underlying int"
|
Closing per #122982 (comment) |
booltype #122982📚 Documentation preview 📚: https://cpython-previews--122983.org.readthedocs.build/