]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-82012: Deprecate bitwise inversion (~) of bool (#103487)
authorTim Hoffmann <2836374+timhoffm@users.noreply.github.com>
Wed, 3 May 2023 07:00:42 +0000 (09:00 +0200)
committerGitHub <noreply@github.com>
Wed, 3 May 2023 07:00:42 +0000 (00:00 -0700)
commitfdb3ef8c0f94c7e55870a585dc6499aca46f9f90
tree8a2c2ce856fe00afc77a99049f3bfa331068069d
parent5b05b013ff13032ffc4db07108a507c08b3a604d
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>
Doc/library/functions.rst
Doc/library/stdtypes.rst
Doc/whatsnew/3.12.rst
Lib/test/test_bool.py
Misc/NEWS.d/next/Core and Builtins/2023-04-12-19-55-24.gh-issue-82012.FlcJAh.rst [new file with mode: 0644]
Objects/boolobject.c