The new typing
module contains several objects with names like "SupportsInt" (-Float, -Bytes, etc.). The name, and the descriptions on the documentation page for the module, might be read to suggest that you can test whether an object is of a type that "supports __int__()
". But if you try to use isinstance()
, it gives a response that makes it clear that that isn't something you are meant to do:
>>> isinstance(5, typing.SupportsInt)
(Traceback omitted)
TypeError: Protocols cannot be used with isinstance().
On the other hand, you can use issubclass()
:
>>> issubclass((5).__class__, typing.SupportsInt)
True
>>> issubclass(type(5), typing.SupportsInt)
True
What is a "protocol" in this context? Why does it disallow the use of isinstance()
in this way?