Add Thread to the return type of Client.get_channel

Also explains some type ignores.
This commit is contained in:
Stocker 2021-08-19 20:18:27 -04:00 committed by GitHub
parent 1d2eaf8526
commit f4d5fcc8f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 16 deletions

View File

@ -29,7 +29,7 @@ import logging
import signal import signal
import sys import sys
import traceback import traceback
from typing import Any, Callable, Coroutine, Dict, Generator, Iterable, List, Optional, Sequence, TYPE_CHECKING, Tuple, TypeVar, Union from typing import Any, Callable, Coroutine, Dict, Generator, List, Optional, Sequence, TYPE_CHECKING, Tuple, TypeVar, Union
import aiohttp import aiohttp
@ -206,6 +206,7 @@ class Client:
loop: Optional[asyncio.AbstractEventLoop] = None, loop: Optional[asyncio.AbstractEventLoop] = None,
**options: Any, **options: Any,
): ):
# self.ws is set in the connect method
self.ws: DiscordWebSocket = None # type: ignore self.ws: DiscordWebSocket = None # type: ignore
self.loop: asyncio.AbstractEventLoop = asyncio.get_event_loop() if loop is None else loop self.loop: asyncio.AbstractEventLoop = asyncio.get_event_loop() if loop is None else loop
self._listeners: Dict[str, List[Tuple[asyncio.Future, Callable[..., bool]]]] = {} self._listeners: Dict[str, List[Tuple[asyncio.Future, Callable[..., bool]]]] = {}
@ -682,7 +683,8 @@ class Client:
if value is None: if value is None:
self._connection._activity = None self._connection._activity = None
elif isinstance(value, BaseActivity): elif isinstance(value, BaseActivity):
self._connection._activity = value.to_dict() # ConnectionState._activity is typehinted as ActivityPayload, we're passing Dict[str, Any]
self._connection._activity = value.to_dict() # type: ignore
else: else:
raise TypeError('activity must derive from BaseActivity.') raise TypeError('activity must derive from BaseActivity.')
@ -716,8 +718,8 @@ class Client:
"""List[:class:`~discord.User`]: Returns a list of all the users the bot can see.""" """List[:class:`~discord.User`]: Returns a list of all the users the bot can see."""
return list(self._connection._users.values()) return list(self._connection._users.values())
def get_channel(self, id: int) -> Optional[Union[GuildChannel, PrivateChannel]]: def get_channel(self, id: int) -> Optional[Union[GuildChannel, Thread, PrivateChannel]]:
"""Returns a channel with the given ID. """Returns a channel or thread with the given ID.
Parameters Parameters
----------- -----------
@ -726,7 +728,7 @@ class Client:
Returns Returns
-------- --------
Optional[Union[:class:`.abc.GuildChannel`, :class:`.abc.PrivateChannel`]] Optional[Union[:class:`.abc.GuildChannel`, :class:`.Thread`, :class:`.abc.PrivateChannel`]]
The returned channel or ``None`` if not found. The returned channel or ``None`` if not found.
""" """
return self._connection.get_channel(id) return self._connection.get_channel(id)
@ -1473,11 +1475,14 @@ class Client:
raise InvalidData('Unknown channel type {type} for channel ID {id}.'.format_map(data)) raise InvalidData('Unknown channel type {type} for channel ID {id}.'.format_map(data))
if ch_type in (ChannelType.group, ChannelType.private): if ch_type in (ChannelType.group, ChannelType.private):
channel = factory(me=self.user, data=data, state=self._connection) # the factory will be a DMChannel or GroupChannel here
channel = factory(me=self.user, data=data, state=self._connection) # type: ignore
else: else:
guild_id = int(data['guild_id']) # the factory can't be a DMChannel or GroupChannel here
guild_id = int(data['guild_id']) # type: ignore
guild = self.get_guild(guild_id) or Object(id=guild_id) guild = self.get_guild(guild_id) or Object(id=guild_id)
channel = factory(guild=guild, state=self._connection, data=data) # GuildChannels expect a Guild, we may be passing an Object
channel = factory(guild=guild, state=self._connection, data=data) # type: ignore
return channel return channel

View File

@ -1313,8 +1313,8 @@ msgstr "戻り値の型"
#: discord.Client.get_channel:7 of #: discord.Client.get_channel:7 of
#, fuzzy #, fuzzy
msgid "Optional[Union[:class:`.abc.GuildChannel`, :class:`.abc.PrivateChannel`]]" msgid "Optional[Union[:class:`.abc.GuildChannel`, :class:`.Thread`, :class:`.abc.PrivateChannel`]]"
msgstr "Union[:class:`.abc.GuildChannel`, :class:`.abc.PrivateChannel`]" msgstr "Union[:class:`.abc.GuildChannel`, :class:`.Thread`, :class:`.abc.PrivateChannel`]"
#: discord.Client.get_guild:1 of #: discord.Client.get_guild:1 of
#, fuzzy #, fuzzy
@ -16949,11 +16949,11 @@ msgstr ":class:`~discord.User`"
#~ ":class:`.Streaming`]] -- ログイン時のアクティビティ。" #~ ":class:`.Streaming`]] -- ログイン時のアクティビティ。"
#~ msgid "" #~ msgid ""
#~ "Optional[Union[:class:`.abc.GuildChannel`, " #~ "Optional[Union[:class:`.abc.GuildChannel`, :class:`.Thread`, "
#~ ":class:`.abc.PrivateChannel`]]: Returns a channel" #~ ":class:`.abc.PrivateChannel`]]: Returns a channel"
#~ " with the given ID." #~ " with the given ID."
#~ msgstr "" #~ msgstr ""
#~ "Optional[Union[:class:`.abc.GuildChannel`, " #~ "Optional[Union[:class:`.abc.GuildChannel`, :class:`.Thread`, "
#~ ":class:`.abc.PrivateChannel`]]: 与えられたIDを持つチャンネルを返します。" #~ ":class:`.abc.PrivateChannel`]]: 与えられたIDを持つチャンネルを返します。"
#~ msgid "If not found, returns ``None``." #~ msgid "If not found, returns ``None``."

View File

@ -1466,7 +1466,7 @@ msgid ":class:`.Member` -- A member the client can see."
msgstr "" msgstr ""
#: discord.ext.commands.Bot.get_channel:1 of #: discord.ext.commands.Bot.get_channel:1 of
msgid "Returns a channel with the given ID." msgid "Returns a channel or thread with the given ID."
msgstr "" msgstr ""
#: discord.ext.commands.Bot.get_channel:3 discord.ext.commands.Bot.get_emoji:3 #: discord.ext.commands.Bot.get_channel:3 discord.ext.commands.Bot.get_emoji:3
@ -1481,8 +1481,8 @@ msgstr ""
#: discord.ext.commands.Bot.get_channel:7 of #: discord.ext.commands.Bot.get_channel:7 of
#, fuzzy #, fuzzy
msgid "Optional[Union[:class:`.abc.GuildChannel`, :class:`.abc.PrivateChannel`]]" msgid "Optional[Union[:class:`.abc.GuildChannel`, :class:`.Thread`, :class:`.abc.PrivateChannel`]]"
msgstr "Union[ :class:`.abc.GuildChannel` , :class:`.abc.PrivateChannel` ]" msgstr "Union[:class:`.abc.GuildChannel`, :class:`.Thread`, :class:`.abc.PrivateChannel`]"
#: discord.ext.commands.Bot.get_cog:1 of #: discord.ext.commands.Bot.get_cog:1 of
msgid "Gets the cog instance requested." msgid "Gets the cog instance requested."
@ -6008,7 +6008,7 @@ msgstr ""
#~ msgstr "" #~ msgstr ""
#~ msgid "" #~ msgid ""
#~ "Optional[Union[:class:`.abc.GuildChannel`, " #~ "Optional[Union[:class:`.abc.GuildChannel`, :class:`.Thread`, "
#~ ":class:`.abc.PrivateChannel`]]: Returns a channel" #~ ":class:`.abc.PrivateChannel`]]: Returns a channel"
#~ " with the given ID." #~ " with the given ID."
#~ msgstr "" #~ msgstr ""