Fix typing issues and improve typing completeness across the library

Co-authored-by: Danny <Rapptz@users.noreply.github.com>
Co-authored-by: Josh <josh.ja.butt@gmail.com>
This commit is contained in:
Stocker
2022-03-13 23:52:10 -04:00
committed by GitHub
parent 603681940f
commit 5aa696ccfa
66 changed files with 1071 additions and 802 deletions

View File

@ -54,6 +54,8 @@ __all__ = (
)
if TYPE_CHECKING:
from typing_extensions import Self
from .client import Client
from .state import ConnectionState
from .voice_client import VoiceClient
@ -62,10 +64,10 @@ if TYPE_CHECKING:
class ReconnectWebSocket(Exception):
"""Signals to safely reconnect the websocket."""
def __init__(self, shard_id, *, resume=True):
self.shard_id = shard_id
self.resume = resume
self.op = 'RESUME' if resume else 'IDENTIFY'
def __init__(self, shard_id: Optional[int], *, resume: bool = True) -> None:
self.shard_id: Optional[int] = shard_id
self.resume: bool = resume
self.op: str = 'RESUME' if resume else 'IDENTIFY'
class WebSocketClosure(Exception):
@ -225,7 +227,7 @@ class VoiceKeepAliveHandler(KeepAliveHandler):
ack_time = time.perf_counter()
self._last_ack = ack_time
self._last_recv = ack_time
self.latency = ack_time - self._last_send
self.latency: float = ack_time - self._last_send
self.recent_ack_latencies.append(self.latency)
@ -339,7 +341,7 @@ class DiscordWebSocket:
@classmethod
async def from_client(
cls: Type[DWS],
cls,
client: Client,
*,
initial: bool = False,
@ -348,7 +350,7 @@ class DiscordWebSocket:
session: Optional[str] = None,
sequence: Optional[int] = None,
resume: bool = False,
) -> DWS:
) -> Self:
"""Creates a main websocket for Discord from a :class:`Client`.
This is for internal use only.
@ -821,11 +823,11 @@ class DiscordVoiceWebSocket:
*,
hook: Optional[Callable[..., Coroutine[Any, Any, Any]]] = None,
) -> None:
self.ws = socket
self.loop = loop
self._keep_alive = None
self._close_code = None
self.secret_key = None
self.ws: aiohttp.ClientWebSocketResponse = socket
self.loop: asyncio.AbstractEventLoop = loop
self._keep_alive: Optional[VoiceKeepAliveHandler] = None
self._close_code: Optional[int] = None
self.secret_key: Optional[str] = None
if hook:
self._hook = hook # type: ignore - type-checker doesn't like overriding methods
@ -864,7 +866,9 @@ class DiscordVoiceWebSocket:
await self.send_as_json(payload)
@classmethod
async def from_client(cls: Type[DVWS], client: VoiceClient, *, resume=False, hook=None) -> DVWS:
async def from_client(
cls, client: VoiceClient, *, resume: bool = False, hook: Optional[Callable[..., Coroutine[Any, Any, Any]]] = None
) -> Self:
"""Creates a voice websocket for the :class:`VoiceClient`."""
gateway = 'wss://' + client.endpoint + '/?v=4'
http = client._state.http