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
gh-82012: Deprecate bitwise inversion (~) of bool #103487
Conversation
b94d85b
to
20dec73
Compare
|
Please document this in What's New at https://docs.python.org/3.12/whatsnew/3.12.html#deprecated And is this operator documented in the main reference? Let's also mention the deprecation there. Is the plan to turn this into an error in 3.14, or keep it open ended? |
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.
LGTM. Let's just merge this.
|
We should probably add a comment about this to https://github.com/python/cpython/blob/main/Doc/whatsnew/3.12.rst. |
Yes! |
8290ea7
to
bf02fd4
Compare
|
I've added the deprecation notice to whatsnew.
I suggest to turn this into an error (and have indicated so in the whatsnew). There is a real danger that some users write |
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
We should test this behavior separately (doing something like with assertWarns(DeprecationWarning): exec("~False")).
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.
Do you mean instead of or in addition to the current test? We originally had eval("~True") but I figured it's clearer to have the warning context only around the operation and not around a comparably complex eval() or exec(). I'm happy to change if there's a benefit of these though.
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.
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.
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.
The new behavior should also be mentioned in https://docs.python.org/3.12/library/stdtypes.html#boolean-values
It currently says "In numeric contexts (for example when used as the argument to an arithmetic operator), they behave like the integers 0 and 1, respectively.", which will no longer be true in all contexts.
3ac9b82
to
a6e3366
Compare
I suggest to not document this as it would become more confusing than helpful: In my view, there should be no reason to use bitwise operators on In particular, if we document
But maybe you have better ideas what to document. |
d599825
to
edd07f2
Compare
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: python#82012 (comment) Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
edd07f2
to
1f0351a
Compare
Whether we like it or not, these operators have worked this way for a long time in Python, and backwards compatibility alone means we can't just make them go away. So it's better to document clearly how they work and what is changing.
I think the whole reason that we're deprecating only the
Your wording sounds good to me! |
This also pulls the bool type to top-level of the type description page. Before it was only documented in the section "Other Built-in Types / Boolean Values".
|
@JelleZijlstra thanks for the feedback. I've rewritten and restructured the bool docs a bit so that it's hopefully more clear and still precise. Please check if this is ok. - It's in a seprate commit, so could be easily modified/reverted. In particular, I've created a top-level section "Boolean Type - bool", which does the importance of the type more justice. |
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.
Looks good, but I'd like to have another core dev look at the docs before we merge.
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.
I think I'd prefer to remove the Boolean Values section. We can preserve the .. _bltin-boolean-values: and index roles, which should hopefully preserve links.
You mean moving them to the new "Boolean Type" section? |
|
Yup! |
HTML links to the section cannot be preseved the URL was https://docs.python.org/3.12/library/stdtypes.html#boolean-values and the anchor I've rewritten internal references (because the label |
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
|
Is this waiting for anyone? |
|
Not anymore. Thanks all! |
The bitwise inversion operator on bool returns the bitwise inversion of the underlying int value; i.e.
~True == -2such thatbool(~True) == True.It's a common pitfall that users mistake
~as negation operator and actually wantnot. 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: #82012 (comment)