~bool deprecation - Core Development - Discussions on Python.org
~bool deprecation Core Development barry (Barry Warsaw) August 28, 2024, 6:49pm 1 A few days ago, I noticed an email notification from GitHub about a change in deprecation period for ~bool , i.e. bitwise inversion of a bool type. The precipitating PR is GH-82012 , which is linked to the original issue GH-122982 . This surprised me because I couldn’t recall any more visible discussion about what seems to me to be a fairly impactful change for users.
Granted, this is only the deprecation, but the root topic of whether this should even be deprecated, wasn’t to my knowledge discussed anywhere else but GitHub, and it was pure happenstance that I noticed this thread in the deluge of GitHub notification emails. I’ll start a separate thread about the wider topic of change visibility [1] , but I wanted to raise this concern so it gets more discussion. I’m not presenting my opinion, either as an SC member or with my individual core dev hat on, as to whether this change is good or not.
But I think it’s at least controversial enough to warrant a wider discussion before any removal actually takes place [2] . So… should ~bool be deprecated? Discuss! please don’t post here, it’ll just likely split the topic anyway ↩︎ deprecations are much easier to reverse, but once its removed, that’s a path that’s much more difficult to walk back ↩︎ 4 Likes Change visibility About `defaultdict.__missing__` brettcannon (Brett Cannon) August 28, 2024, 7:22pm 2 If we want to continue to treat booleans as proper integers then no.
If we don’t quite care about maximizing that compatibility then I’m indifferent. 3 Likes storchaka (Serhiy Storchaka) August 28, 2024, 7:36pm 3 The original issue is bool(~True) == True · Issue #82012 · python/cpython · GitHub . Barry, something is wrong with your links. I agree that this change is slightly controversial, but I think that it is net positive. There is more confusion than benefit from having ~True == -2 .
The problem could gone if make True equal to -1 instead of 1 , but this is a bit late. 9 Likes AA-Turner (Adam Turner) August 28, 2024, 7:58pm 4 I’d agree that eventually removing ~bool makes sense - it is surprising in a bad way for users coming from other languages that might expect ~False is True , and also confusing for beginners. Extending by 2 years seems fine, though. A 5 Likes pf_moore (Paul Moore) August 28, 2024, 9:23pm 5 IMO, removing ~bool simply changes the nature of the confusion.
Why remove ~ , but leave + , - , * and / ? For that matter, leaving & and | simply because they happen to behave as if bool was a full-fleged type of its own, while removing ~ because it doesn’t, feels weird to me. I’d be a lot happier if this were part of a larger change to make bool a full-fledged boolean type. Either that, or I’d prefer to simply leave it alone. I know practicality beats purity and all that, but this feels more like a perlish “do what I mean” (or in this case, “don’t do what I don’t mean” ) 23 Likes Is there a road towards making bool a fully-fledged Boolean type? oscarbenjamin (Oscar Benjamin) August 28, 2024, 9:34pm 6 What about deprecating ~bool for a while but with the intention to make it work properly in the end rather than just removing it altogether?
The use of ~ for logical negation is widespread e.g. numpy: In [11]: a = np.array([1, 2, 3, 4]) In [12]: cond = a % 2 == 0 In [13]: cond Out[13]: array([False, True, False, True]) In [14]: ~cond Out[14]: array([ True, False, True, False]) In [15]: a[~cond] = -1 In [16]: a Out[16]: array([-1, 2, -1, 4]) Also sympy: In [17]: import sympy In [18]: x, y, z = sympy.symbols('x, y, z') In [19]: cond = x & (y | ~z) In [20]: cond Out[20]: x ∧ (y ∨ ¬z) In [21]: cond.subs(z, True) Out[21]: x ∧ y In [34]: x | ~sympy.S.true Out[34]: x In [35]: x | ~True --------------------------------------------------------------------------- TypeError Many more examples of libraries using these can be found.
The ones I know of all broadly correspond to the above two cases though: arrays of booleans or symbolic boolean expressions. Ultimately it would be better if bool was not a subclass of int. Having it be a subclass of int now does create a compatibility problem for any change because of e.g. if isinstance(obj, int): return ~obj but I doubt that a lot of code depends on ~bool given how useless it is: just use an int if you want an int!
Even ~int is rarely needed in Python and I can’t imagine doing that in some context where bools and ints are mixed so that I’m going to do ~obj without knowing where obj is an int or not. A future non-int bool type could still support arithmetic operations like True - True -> 0 for compatibility but could do the right thing for operators like ~ that might reasonably be expected to handle boolean logic properly. 9 Likes jeff5 (Jeff Allen) August 29, 2024, 7:27am 7 numpy does things I would not emulate.
While bool is a subclass of int , I think the arithmetic operators ought to do what they do. I’m with @pf_moore that the confusion is worse when one arithmetic operation is “corrected” and the others do … arithmetic. Let’s make True == -1 … . (Edit: I see this idea “if only we had a time machine” has been discussed. Now I realise why some languages make this implementation choice internally.) 1 Like bjorn-martinsson (Bjorn Martinsson) August 29, 2024, 12:09pm 8 I’ve found using ~ to be very useful in many different situations, and I’ve using it a lot, both in Python and C++.
I’ve identified 3 cases where my Python code breaks because of this change. Codes involving bitmasks, codes making use of ~ for “reverse indexing” of a list, and the codegolf trick of using -~x to increment x by 1. There is a relatively easy workaround, simply search replace ~ with ~+ . The + will convert any Boolean to an int. I’m against deprecating ~ on Booleans in general, but I am strongly against changing ~True to False .
A change like that is really dangerous since it could (and would) create silent bugs. It also would make new code incompatible with older versions of Python. Something I also want to note is that ~ in numpy is always bit-inverse. ~0 is always -1 , even in numpy. However, depending on the data type, -1 can have different representations. Modulo two, -1 is equal to 1. 6 Likes pitrou (Antoine Pitrou) August 29, 2024, 12:14pm 9 But that’s only because it’s not possible to override not , right?
In any case, ~bool behavior is confusing enough that it does seem desirable to deprecate it. “Codegolf” is not a good enough reason to keep supporting it 4 Likes pitrou (Antoine Pitrou) August 29, 2024, 12:18pm 10 That doesn’t seem to be the case here (NumPy 2.1.0): >>> int_arr = np.array([-2, -1, 0, 1, 2], dtype=np.int8) # Inverse then view as bool => bitwise complement >>> (~int_arr).view(np.bool_) array([ True, False, True, True, True]) # View as bool then inverse => logical not >>> ~(int_arr.view(np.bool_)) array([False, False, True, False, False]) oscarbenjamin (Oscar Benjamin) August 29, 2024, 12:26pm 11 I’m a bit confused by this: Are you using ~x to index a list like stuff[~x] but where x is a bool rather than an int?
Are you doing bitmasks like flags &= ~x but where x is a bool rather than an int? These seem like situations where I would definitely want x to be an int rather than a bool . bjorn-martinsson (Bjorn Martinsson) August 29, 2024, 12:40pm 12 Nothing in your example there contradicts my claim. np.bool is a single bit datatype, and -1 = 1 (mod 2). If you try using ~ on unsigned datatypes, like dtype=np.uint8 , dtype=np.uint16 , dtype=np.uint32 , … You’ll see how ~ is always bit inverse, ~0 is always -1 .
The only thing that changes is how many bits the data type supports. 1 Like bjorn-martinsson (Bjorn Martinsson) August 29, 2024, 12:50pm 13 Good question. For bitmasks, I do use things like flags &= ~(x == 0) . For example in my implementation of a special kind of segm
The rest of this page is on the live site.