Protocol inheritance for typing_extensions.SupportsX classes (#13010)

At runtime, depending on the version of python,
these inherit from typing_extenstions.Protocol
instead of typing.Protocol.
This commit is contained in:
Stephen Morton
2024-12-27 20:53:44 -08:00
committed by GitHub
parent 942350b6c7
commit abed9a8b55

View File

@@ -50,12 +50,6 @@ from typing import ( # noqa: Y022,Y037,Y038,Y039
Sequence as Sequence,
Set as Set,
Sized as Sized,
SupportsAbs as SupportsAbs,
SupportsBytes as SupportsBytes,
SupportsComplex as SupportsComplex,
SupportsFloat as SupportsFloat,
SupportsInt as SupportsInt,
SupportsRound as SupportsRound,
Text as Text,
TextIO as TextIO,
Tuple as Tuple,
@@ -192,6 +186,7 @@ __all__ = [
_T = typing.TypeVar("_T")
_F = typing.TypeVar("_F", bound=Callable[..., Any])
_TC = typing.TypeVar("_TC", bound=type[object])
_T_co = typing.TypeVar("_T_co", covariant=True) # Any type covariant containers.
class _Final: ... # This should be imported from typing but that breaks pytype
@@ -284,11 +279,6 @@ def get_origin(tp: Any) -> Any | None: ...
Annotated: _SpecialForm
_AnnotatedAlias: Any # undocumented
@runtime_checkable
class SupportsIndex(Protocol, metaclass=abc.ABCMeta):
@abc.abstractmethod
def __index__(self) -> int: ...
# New and changed things in 3.10
if sys.version_info >= (3, 10):
from typing import (
@@ -385,7 +375,17 @@ else:
if sys.version_info >= (3, 12):
from collections.abc import Buffer as Buffer
from types import get_original_bases as get_original_bases
from typing import TypeAliasType as TypeAliasType, override as override
from typing import (
SupportsAbs as SupportsAbs,
SupportsBytes as SupportsBytes,
SupportsComplex as SupportsComplex,
SupportsFloat as SupportsFloat,
SupportsIndex as SupportsIndex,
SupportsInt as SupportsInt,
SupportsRound as SupportsRound,
TypeAliasType as TypeAliasType,
override as override,
)
else:
def override(arg: _F, /) -> _F: ...
def get_original_bases(cls: type, /) -> tuple[Any, ...]: ...
@@ -420,6 +420,45 @@ else:
# https://github.com/python/typeshed/issues/10224 for why we're defining it this way
def __buffer__(self, flags: int, /) -> memoryview: ...
@runtime_checkable
class SupportsInt(Protocol, metaclass=abc.ABCMeta):
@abc.abstractmethod
def __int__(self) -> int: ...
@runtime_checkable
class SupportsFloat(Protocol, metaclass=abc.ABCMeta):
@abc.abstractmethod
def __float__(self) -> float: ...
@runtime_checkable
class SupportsComplex(Protocol, metaclass=abc.ABCMeta):
@abc.abstractmethod
def __complex__(self) -> complex: ...
@runtime_checkable
class SupportsBytes(Protocol, metaclass=abc.ABCMeta):
@abc.abstractmethod
def __bytes__(self) -> bytes: ...
@runtime_checkable
class SupportsIndex(Protocol, metaclass=abc.ABCMeta):
@abc.abstractmethod
def __index__(self) -> int: ...
@runtime_checkable
class SupportsAbs(Protocol[_T_co]):
@abc.abstractmethod
def __abs__(self) -> _T_co: ...
@runtime_checkable
class SupportsRound(Protocol[_T_co]):
@overload
@abc.abstractmethod
def __round__(self) -> int: ...
@overload
@abc.abstractmethod
def __round__(self, ndigits: int, /) -> _T_co: ...
if sys.version_info >= (3, 13):
from types import CapsuleType as CapsuleType
from typing import (