Large codebases and libraries need time to refactor code that might be legacy or automatically generated.
For most developers, this is rarely the intended result. Usually, someone using ~ on a boolean actually wants the logical NOT ( not True ), which correctly returns False . Because of this common point of confusion, the Python steering committee decided to deprecate bitwise inversion on booleans to encourage clearer coding practices. What’s New in Issue #122982? 122982
If your project currently triggers a DeprecationWarning when using ~ on a boolean, the fix is straightforward. Replace the bitwise operator with the logical not keyword: Large codebases and libraries need time to refactor
Python Development Update: Extending the Deprecation Warning for ~bool (Issue #122982) Because of this common point of confusion, the
If you have ever accidentally used the bitwise inversion operator ( ~ ) on a Python boolean, you might have noticed it behaves in a way that is technically correct but logically confusing. As Python continues to refine its syntax for better clarity, Issue #122982 marks a small but important step in how the language handles these edge cases. The Problem: Why ~True Isn't False
is_active = True status = ~is_active # Returns -2, triggers warning Use code with caution. Copied to clipboard is_active = True status = not is_active # Returns False Use code with caution. Copied to clipboard Conclusion
This blog post addresses in the CPython repository, which focuses on extending the deprecation warning period for bitwise inversion on boolean types in Python.