Skip to content

gh-149532: Remove bool.__invert__ magic method#149533

Open
sobolevn wants to merge 1 commit intopython:mainfrom
sobolevn:issue-149532
Open

gh-149532: Remove bool.__invert__ magic method#149533
sobolevn wants to merge 1 commit intopython:mainfrom
sobolevn:issue-149532

Conversation

@sobolevn
Copy link
Copy Markdown
Member

@sobolevn sobolevn commented May 8, 2026

Copy link
Copy Markdown
Contributor

@sergey-miryanov sergey-miryanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment thread Doc/whatsnew/3.16.rst
* Bitwise inversion on boolean types, ``~True`` or ``~False``
has been deprecated since Python 3.12,
as it produces surprising and unintuitive results (``-2`` and ``-1``).
Use ``not x`` instead for the logical negation of a Boolean.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Use ``not x`` instead for the logical negation of a Boolean.
Use ``not x`` instead for the logical negation of a boolean.

Comment thread Lib/test/test_bool.py
Comment on lines +160 to +175
msg = re.escape("bad operand type for unary ~: 'bool'")

# Check constants in case of a folding:
with self.assertRaisesRegex(TypeError, msg):
~False
with self.assertRaisesRegex(TypeError, msg):
~True

# Check variable:
for bool_val in [True, False]:
with self.subTest(bool_val), self.assertRaisesRegex(TypeError, msg):
~bool_val

# Check `eval`:
with self.assertRaisesRegex(TypeError, msg):
eval("~False")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if those shouldn't be subtests

@sobolevn
Copy link
Copy Markdown
Member Author

sobolevn commented May 9, 2026

See https://discuss.python.org/t/bool-deprecation/62232/128 for the context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants