Real world impediments through ~bool deprecation

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].


  1. in some situations (in input positions in stubs, mostly) you can hack around it by using something like the optype.JustInt type (docs) ↩︎

  2. for as long as bool is a subclass of int, that is ↩︎

8 Likes