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.
An ABC with one abstract method ``__bytes__``.
+.. class:: SupportsIndex
+
+ An ABC with one abstract method ``__index__``.
+
+ .. versionadded:: 3.8
+
.. class:: SupportsAbs
An ABC with one abstract method ``__abs__`` that is covariant
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)
'SupportsBytes',
'SupportsComplex',
'SupportsFloat',
+ 'SupportsIndex',
'SupportsInt',
'SupportsRound',
pass
+class SupportsIndex(_Protocol):
+ __slots__ = ()
+
+ @abstractmethod
+ def __index__(self) -> int:
+ pass
+
+
class SupportsAbs(_Protocol[T_co]):
__slots__ = ()
Raúl Cumplido
Antonio Cuni
Brian Curtin
+Paul Dagnelie
Lisandro Dalcin
Darren Dale
Andrew Dalke
--- /dev/null
+Add SupportsIndex protocol to the typing module to allow type checking to detect classes that can be passed to `hex()`, `oct()` and `bin()`.