From a3157868696279fe73e34d85bae72afa009607e8 Mon Sep 17 00:00:00 2001 From: Josh Date: Tue, 22 Feb 2022 23:51:11 +1000 Subject: [PATCH] Handle type-errors in upcoming pyright release --- discord/embeds.py | 4 ++-- discord/ext/commands/flags.py | 4 ++-- discord/player.py | 11 ++++++----- discord/shard.py | 2 +- discord/voice_client.py | 2 +- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/discord/embeds.py b/discord/embeds.py index 59914f0ea..7cb1c5a8c 100644 --- a/discord/embeds.py +++ b/discord/embeds.py @@ -185,7 +185,7 @@ class Embed: type: EmbedType = 'rich', url: MaybeEmpty[Any] = EmptyEmbed, description: MaybeEmpty[Any] = EmptyEmbed, - timestamp: datetime.datetime = None, + timestamp: MaybeEmpty[datetime.datetime] = EmptyEmbed, ): self.colour = colour if colour is not EmptyEmbed else color @@ -203,7 +203,7 @@ class Embed: if self.url is not EmptyEmbed: self.url = str(self.url) - if timestamp: + if timestamp is not EmptyEmbed: self.timestamp = timestamp @classmethod diff --git a/discord/ext/commands/flags.py b/discord/ext/commands/flags.py index b356af342..b3494d0df 100644 --- a/discord/ext/commands/flags.py +++ b/discord/ext/commands/flags.py @@ -416,9 +416,9 @@ async def convert_flag(ctx, argument: str, flag: Flag, annotation: Any = None) - # typing.List[x] annotation = annotation.__args__[0] return await convert_flag(ctx, argument, flag, annotation) - elif origin is Union and annotation.__args__[-1] is type(None): + elif origin is Union and type(None) in annotation.__args__: # typing.Optional[x] - annotation = Union[annotation.__args__[:-1]] + annotation = Union[tuple(arg for arg in annotation.__args__ if arg is not type(None))] # type: ignore return await run_converters(ctx, annotation, argument, param) elif origin is dict: # typing.Dict[K, V] -> typing.Tuple[K, V] diff --git a/discord/player.py b/discord/player.py index e8e2e8e54..d6b6f8a05 100644 --- a/discord/player.py +++ b/discord/player.py @@ -38,6 +38,7 @@ import io from typing import Any, Callable, Generic, IO, Optional, TYPE_CHECKING, Tuple, Type, TypeVar, Union +from .enums import SpeakingState from .errors import ClientException from .opus import Encoder as OpusEncoder from .oggparse import OggStream @@ -656,7 +657,7 @@ class AudioPlayer(threading.Thread): # getattr lookup speed ups play_audio = self.client.send_audio_packet - self._speak(True) + self._speak(SpeakingState.voice) while not self._end.is_set(): # are we paused? @@ -714,19 +715,19 @@ class AudioPlayer(threading.Thread): def stop(self) -> None: self._end.set() self._resumed.set() - self._speak(False) + self._speak(SpeakingState.none) def pause(self, *, update_speaking: bool = True) -> None: self._resumed.clear() if update_speaking: - self._speak(False) + self._speak(SpeakingState.none) def resume(self, *, update_speaking: bool = True) -> None: self.loops = 0 self._start = time.perf_counter() self._resumed.set() if update_speaking: - self._speak(True) + self._speak(SpeakingState.voice) def is_playing(self) -> bool: return self._resumed.is_set() and not self._end.is_set() @@ -740,7 +741,7 @@ class AudioPlayer(threading.Thread): self.source = source self.resume(update_speaking=False) - def _speak(self, speaking: bool) -> None: + def _speak(self, speaking: SpeakingState) -> None: try: asyncio.run_coroutine_threadsafe(self.client.ws.speak(speaking), self.client.loop) except Exception as e: diff --git a/discord/shard.py b/discord/shard.py index 4a297ac65..351a63ecc 100644 --- a/discord/shard.py +++ b/discord/shard.py @@ -481,7 +481,7 @@ class AutoShardedClient(Client): *, activity: Optional[BaseActivity] = None, status: Optional[Status] = None, - shard_id: int = None, + shard_id: Optional[int] = None, ) -> None: """|coro| diff --git a/discord/voice_client.py b/discord/voice_client.py index 200feffd2..96e52445f 100644 --- a/discord/voice_client.py +++ b/discord/voice_client.py @@ -563,7 +563,7 @@ class VoiceClient(VoiceProtocol): return header + box.encrypt(bytes(data), bytes(nonce)).ciphertext + nonce[:4] - def play(self, source: AudioSource, *, after: Callable[[Optional[Exception]], Any] = None) -> None: + def play(self, source: AudioSource, *, after: Optional[Callable[[Optional[Exception]], Any]] = None) -> None: """Plays an :class:`AudioSource`. The finalizer, ``after`` is called after the source has been exhausted