Differences in bound=object vs bound=Any

Type systems often look at types as though they exist in a hierarchy from top to bottom. Types can be silently converted or assigned up the tree but not down.

In Python object is what is known as a top type: you can assign anything to a variable of type object but the only operations you can perform on that variable are the operations valid on all objects.

The Any type is both a top type (so you can assign anything to Any) and a bottom type (so you can assign Any to anything). In effect it turns off the type system whenever you use Any.

If you know any typescript there the equivalents are object and unknown are top types (but unknown has no operations at all defined on it), never is a bottom type (so no other types can be assigned to a never) and any is both top and bottom so again effectively breaks the type system.

As of Python 3.11 we also have a proper bottom type in typing.Never. I’m not aware if we have an equivalent unknown type but for most purposes using object will do (and object is almost always preferable in places people use typing.Any).

So to answer the question, bound=Any is removing all the safety harnesses you want if you’re doing typing at all in Python, bound=object keeps the safety but does mean you will have to explicitly check the actual type if you want to do anything much with the object.

1 Like