bpo-36972: Add SupportsIndex (GH-13448)

In order to support typing checks calling hex(), oct() and bin() on user-defined classes, a SupportIndex protocol is required. The ability to check these at runtime would be good to add for completeness sake. This is pretty much just a copy of SupportsInt with the names tweaked.
This commit is contained in:
Paul Dagnelie 2019-05-22 07:23:01 -07:00 committed by Ivan Levkivskyi
parent 33e71e01e9
commit 4c7a46eb3c
5 changed files with 21 additions and 0 deletions

View file

@ -568,6 +568,10 @@ class ProtocolTests(BaseTestCase):
self.assertIsSubclass(list, typing.Reversible)
self.assertNotIsSubclass(int, typing.Reversible)
def test_supports_index(self):
self.assertIsSubclass(int, typing.SupportsIndex)
self.assertNotIsSubclass(str, typing.SupportsIndex)
def test_protocol_instance_type_error(self):
with self.assertRaises(TypeError):
isinstance(0, typing.SupportsAbs)