Skip to content

Commit

Permalink
Remove duplicate typing
Browse files Browse the repository at this point in the history
  • Loading branch information
justmobilize committed Jan 2, 2024
1 parent 16d658d commit 66fb48c
Showing 1 changed file with 7 additions and 87 deletions.
94 changes: 7 additions & 87 deletions adafruit_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,94 +46,14 @@

from adafruit_connection_manager import get_connection_manager


if not sys.implementation.name == "circuitpython":
from ssl import SSLContext
from types import ModuleType, TracebackType
from typing import Any, Dict, Optional, Tuple, Type, Union

try:
from typing import Protocol
except ImportError:
from typing_extensions import Protocol

# Based on https://github.com/python/typeshed/blob/master/stdlib/_socket.pyi
class CommonSocketType(Protocol):
"""Describes the common structure every socket type must have."""

def send(self, data: bytes, flags: int = ...) -> None:
"""Send data to the socket. The meaning of the optional flags kwarg is
implementation-specific."""

def settimeout(self, value: Optional[float]) -> None:
"""Set a timeout on blocking socket operations."""

def close(self) -> None:
"""Close the socket."""

class CommonCircuitPythonSocketType(CommonSocketType, Protocol):
"""Describes the common structure every CircuitPython socket type must have."""

def connect(
self,
address: Tuple[str, int],
conntype: Optional[int] = ...,
) -> None:
"""Connect to a remote socket at the provided (host, port) address. The conntype
kwarg optionally may indicate SSL or not, depending on the underlying interface.
"""

class SupportsRecvWithFlags(Protocol):
"""Describes a type that posseses a socket recv() method supporting the flags kwarg."""

def recv(self, bufsize: int = ..., flags: int = ...) -> bytes:
"""Receive data from the socket. The return value is a bytes object representing
the data received. The maximum amount of data to be received at once is specified
by bufsize. The meaning of the optional flags kwarg is implementation-specific.
"""

class SupportsRecvInto(Protocol):
"""Describes a type that possesses a socket recv_into() method."""

def recv_into(
self, buffer: bytearray, nbytes: int = ..., flags: int = ...
) -> int:
"""Receive up to nbytes bytes from the socket, storing the data into the provided
buffer. If nbytes is not specified (or 0), receive up to the size available in the
given buffer. The meaning of the optional flags kwarg is implementation-specific.
Returns the number of bytes received."""

class CircuitPythonSocketType(
CommonCircuitPythonSocketType,
SupportsRecvInto,
SupportsRecvWithFlags,
Protocol,
): # pylint: disable=too-many-ancestors
"""Describes the structure every modern CircuitPython socket type must have."""

class StandardPythonSocketType(
CommonSocketType, SupportsRecvInto, SupportsRecvWithFlags, Protocol
):
"""Describes the structure every standard Python socket type must have."""

def connect(self, address: Union[Tuple[Any, ...], str, bytes]) -> None:
"""Connect to a remote socket at the provided address."""

SocketType = Union[
CircuitPythonSocketType,
StandardPythonSocketType,
]

SocketpoolModuleType = ModuleType

class InterfaceType(Protocol):
"""Describes the structure every interface type must have."""

@property
def TLS_MODE(self) -> int: # pylint: disable=invalid-name
"""Constant representing that a socket's connection mode is TLS."""

SSLContextType = Union[SSLContext, "_FakeSSLContext"]
from types import TracebackType
from typing import Any, Dict, Optional, Type
from adafruit_connection_manager import (
SocketType,
SocketpoolModuleType,
SSLContextType,
)


class _RawResponse:
Expand Down

0 comments on commit 66fb48c

Please sign in to comment.