46
46
'TypedDict' ,
47
47
48
48
# Structural checks, a.k.a. protocols.
49
+ 'SupportsAbs' ,
50
+ 'SupportsBytes' ,
51
+ 'SupportsComplex' ,
52
+ 'SupportsFloat' ,
49
53
'SupportsIndex' ,
54
+ 'SupportsInt' ,
55
+ 'SupportsRound' ,
50
56
51
57
# One-off things.
52
58
'Annotated' ,
@@ -739,8 +745,49 @@ def runtime_checkable(cls):
739
745
740
746
# Our version of runtime-checkable protocols is faster on Python 3.7-3.11
741
747
if sys .version_info >= (3 , 12 ):
748
+ SupportsInt = typing .SupportsInt
749
+ SupportsFloat = typing .SupportsFloat
750
+ SupportsComplex = typing .SupportsComplex
742
751
SupportsIndex = typing .SupportsIndex
752
+ SupportsAbs = typing .SupportsAbs
753
+ SupportsRound = typing .SupportsRound
743
754
else :
755
+ @runtime_checkable
756
+ class SupportsInt (Protocol ):
757
+ """An ABC with one abstract method __int__."""
758
+ __slots__ = ()
759
+
760
+ @abc .abstractmethod
761
+ def __int__ (self ) -> int :
762
+ pass
763
+
764
+ @runtime_checkable
765
+ class SupportsFloat (Protocol ):
766
+ """An ABC with one abstract method __float__."""
767
+ __slots__ = ()
768
+
769
+ @abc .abstractmethod
770
+ def __float__ (self ) -> float :
771
+ pass
772
+
773
+ @runtime_checkable
774
+ class SupportsComplex (Protocol ):
775
+ """An ABC with one abstract method __complex__."""
776
+ __slots__ = ()
777
+
778
+ @abc .abstractmethod
779
+ def __complex__ (self ) -> complex :
780
+ pass
781
+
782
+ @runtime_checkable
783
+ class SupportsBytes (Protocol ):
784
+ """An ABC with one abstract method __bytes__."""
785
+ __slots__ = ()
786
+
787
+ @abc .abstractmethod
788
+ def __bytes__ (self ) -> bytes :
789
+ pass
790
+
744
791
@runtime_checkable
745
792
class SupportsIndex (Protocol ):
746
793
__slots__ = ()
@@ -749,6 +796,28 @@ class SupportsIndex(Protocol):
749
796
def __index__ (self ) -> int :
750
797
pass
751
798
799
+ @runtime_checkable
800
+ class SupportsAbs (Protocol [T_co ]):
801
+ """
802
+ An ABC with one abstract method __abs__ that is covariant in its return type.
803
+ """
804
+ __slots__ = ()
805
+
806
+ @abc .abstractmethod
807
+ def __abs__ (self ) -> T_co :
808
+ pass
809
+
810
+ @runtime_checkable
811
+ class SupportsRound (Protocol [T_co ]):
812
+ """
813
+ An ABC with one abstract method __round__ that is covariant in its return type.
814
+ """
815
+ __slots__ = ()
816
+
817
+ @abc .abstractmethod
818
+ def __round__ (self , ndigits : int = 0 ) -> T_co :
819
+ pass
820
+
752
821
753
822
if sys .version_info >= (3 , 12 ):
754
823
# The standard library TypedDict in Python 3.8 does not store runtime information
0 commit comments