This thread collects real-world examples where users have been negatively affected by the ~bool deprecation introduced in 3.12. See ~bool deprecation - #94 by timhoffm for a detailed discussion. Please keep the fundamental discussion whether it’s a good idea or not there. This thread here specifically only collects evidence of intentional ~bool usage, which would not be possible anymore after the deprecation expires (currently planned for 3.16).
It shall serve as an empirical foundation to possibly reconsider whether the removal of ~bool has too many negative practical consequences.
How can you draw any conclusions from this? Any use of ~x where x is typed int is a potential use case since it is always allowed to assign a value of type bool to a variable or type int.
Yes, you cannot resolve this by looking at code or typing information. But with the deprecation, we have a runtime check in place. While it cannot give a complete picture I hope that reports of people that have been hit by the deprecation warning will give more insight in actual valid usage of ~bool and potential negative effects of its removal.
To elaborate on this (assuming that “book” is a typo for “bool”):
Python’s type system has currently no good[1] way of disallowing instances of bool whenever int instances are accepted, because bool is a subclass (and subtype) of int. This is bound to lead to type-unsafe situations (it violates Liskov’s substitution principle).
For example, type-checkers wouldn’t be able to detect that this will raise:
def f(x: int) -> None:
print(~x)
f(False) # type-checkers: "sure; bool <: int, so go ahead"
So a bool should be able to do everything that an int can do for it to be type-safe [2].
in some situations (in input positions in stubs, mostly) you can hack around it by using something like the optype.JustInt type (docs) ↩︎
for as long as bool is a subclass of int, that is ↩︎