From 70b577e94b976945cd070d2c1eda778215973784 Mon Sep 17 00:00:00 2001 From: Josh Date: Tue, 22 Feb 2022 17:37:22 +1000 Subject: [PATCH] Fix typing in voice related files --- discord/guild.py | 6 +++--- discord/opus.py | 2 +- discord/player.py | 12 +++++++----- discord/voice_client.py | 27 +++++++++++++++------------ 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/discord/guild.py b/discord/guild.py index 1f1220f6b..0abe4d86f 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -104,7 +104,7 @@ if TYPE_CHECKING: from .voice_client import VoiceProtocol VocalGuildChannel = Union[VoiceChannel, StageChannel] - GuildChannel = Union[VoiceChannel, StageChannel, TextChannel, CategoryChannel, StoreChannel] + GuildChannel = Union[VocalGuildChannel, TextChannel, CategoryChannel, StoreChannel] ByCategoryItem = Tuple[Optional[CategoryChannel], List[GuildChannel]] @@ -3069,7 +3069,7 @@ class Guild(Hashable): ) async def change_voice_state( - self, *, channel: Optional[VocalGuildChannel], self_mute: bool = False, self_deaf: bool = False + self, *, channel: Optional[abc.Snowflake], self_mute: bool = False, self_deaf: bool = False ): """|coro| @@ -3079,7 +3079,7 @@ class Guild(Hashable): Parameters ----------- - channel: Optional[:class:`VoiceChannel`] + channel: Optional[:class:`abc.Snowflake`] Channel the client wants to join. Use ``None`` to disconnect. self_mute: :class:`bool` Indicates if the client should be self-muted. diff --git a/discord/opus.py b/discord/opus.py index c546b9040..f82e786cd 100644 --- a/discord/opus.py +++ b/discord/opus.py @@ -69,7 +69,7 @@ c_int_ptr = ctypes.POINTER(ctypes.c_int) c_int16_ptr = ctypes.POINTER(ctypes.c_int16) c_float_ptr = ctypes.POINTER(ctypes.c_float) -_lib = None +_lib: Any = None class EncoderStruct(ctypes.Structure): diff --git a/discord/player.py b/discord/player.py index dc5c9ba78..e8e2e8e54 100644 --- a/discord/player.py +++ b/discord/player.py @@ -161,7 +161,7 @@ class FFmpegAudio(AudioSource): kwargs.update(subprocess_kwargs) self._process: subprocess.Popen = self._spawn_process(args, **kwargs) - self._stdout: IO[bytes] = self._process.stdout # type: ignore + self._stdout: IO[bytes] = self._process.stdout # type: ignore - process stdout is explicitly set self._stdin: Optional[IO[bytes]] = None self._pipe_thread: Optional[threading.Thread] = None @@ -210,7 +210,8 @@ class FFmpegAudio(AudioSource): self._process.terminate() return try: - self._stdin.write(data) # type: ignore + if self._stdin is not None: + self._stdin.write(data) except Exception: _log.debug('Write error for %s, this is probably not a problem', self, exc_info=True) # at this point the source data is either exhausted or the process is fubar @@ -359,7 +360,7 @@ class FFmpegOpusAudio(FFmpegAudio): self, source: Union[str, io.BufferedIOBase], *, - bitrate: int = 128, + bitrate: Optional[int] = None, codec: Optional[str] = None, executable: str = 'ffmpeg', pipe=False, @@ -378,6 +379,7 @@ class FFmpegOpusAudio(FFmpegAudio): args.append('-' if pipe else source) codec = 'copy' if codec in ('opus', 'libopus') else 'libopus' + bitrate = bitrate if bitrate is not None else 128 # fmt: off args.extend(('-map_metadata', '-1', @@ -462,7 +464,7 @@ class FFmpegOpusAudio(FFmpegAudio): executable = kwargs.get('executable') codec, bitrate = await cls.probe(source, method=method, executable=executable) - return cls(source, bitrate=bitrate, codec=codec, **kwargs) # type: ignore + return cls(source, bitrate=bitrate, codec=codec, **kwargs) @classmethod async def probe( @@ -494,7 +496,7 @@ class FFmpegOpusAudio(FFmpegAudio): Returns --------- - Optional[Tuple[Optional[:class:`str`], Optional[:class:`int`]]] + Optional[Tuple[Optional[:class:`str`], :class:`int`]] A 2-tuple with the codec and bitrate of the input source. """ diff --git a/discord/voice_client.py b/discord/voice_client.py index fca3aa592..b29c4f1a9 100644 --- a/discord/voice_client.py +++ b/discord/voice_client.py @@ -44,7 +44,7 @@ import socket import logging import struct import threading -from typing import Any, Callable, List, Optional, TYPE_CHECKING, Tuple +from typing import Any, Callable, List, Optional, TYPE_CHECKING, Tuple, Union from . import opus, utils from .backoff import ExponentialBackoff @@ -59,6 +59,7 @@ if TYPE_CHECKING: from .state import ConnectionState from .user import ClientUser from .opus import Encoder + from .channel import StageChannel, VoiceChannel from . import abc from .types.voice import ( @@ -67,6 +68,8 @@ if TYPE_CHECKING: SupportedModes, ) + VocalGuildChannel = Union[VoiceChannel, StageChannel] + has_nacl: bool @@ -217,18 +220,19 @@ class VoiceClient(VoiceProtocol): The voice connection token. endpoint: :class:`str` The endpoint we are connecting to. - channel: :class:`abc.Connectable` + channel: Union[:class:`VoiceChannel`, :class:`StageChannel`] The voice channel connected to. loop: :class:`asyncio.AbstractEventLoop` The event loop that the voice client is running on. """ + channel: VocalGuildChannel endpoint_ip: str voice_port: int secret_key: List[int] ssrc: int - def __init__(self, client: Client, channel: abc.Connectable): + def __init__(self, client: Client, channel: VocalGuildChannel): if not has_nacl: raise RuntimeError("PyNaCl library needed in order to use voice") @@ -265,14 +269,14 @@ class VoiceClient(VoiceProtocol): ) @property - def guild(self) -> Optional[Guild]: - """Optional[:class:`Guild`]: The guild we're connected to, if applicable.""" - return getattr(self.channel, 'guild', None) + def guild(self) -> Guild: + """:class:`Guild`: The guild we're connected to.""" + return self.channel.guild @property def user(self) -> ClientUser: """:class:`ClientUser`: The user connected to voice (i.e. ourselves).""" - return self._state.user + return self._state.user # type: ignore - user can't be None after login def checked_add(self, attr, value, limit): val = getattr(self, attr) @@ -295,8 +299,7 @@ class VoiceClient(VoiceProtocol): # We're being disconnected so cleanup await self.disconnect() else: - guild = self.guild - self.channel = channel_id and guild and guild.get_channel(int(channel_id)) # type: ignore + self.channel = channel_id and self.guild.get_channel(int(channel_id)) # type: ignore - this won't be None else: self._voice_state_complete.set() @@ -305,7 +308,7 @@ class VoiceClient(VoiceProtocol): _log.info('Ignoring extraneous voice server update.') return - self.token = data.get('token') + self.token = data['token'] self.server_id = int(data['guild_id']) endpoint = data.get('endpoint') @@ -506,14 +509,14 @@ class VoiceClient(VoiceProtocol): if self.socket: self.socket.close() - async def move_to(self, channel: abc.Snowflake) -> None: + async def move_to(self, channel: Optional[abc.Snowflake]) -> None: """|coro| Moves you to a different voice channel. Parameters ----------- - channel: :class:`abc.Snowflake` + channel: Optional[:class:`abc.Snowflake`] The channel to move to. Must be a voice channel. """ await self.channel.guild.change_voice_state(channel=channel)