Refactor Guild to support type hints
This patch also does the following: * Sets some parameters to be positional only * Changes Guild.edit to use the MISSING sentinel * Changes the various create_channel methods to be type safe * Changes many parameters from Optional[T] to use MISSING * Changes Guild.create_role to use MISSING sentinel This refactor is mostly partial but lays a decent foundation
This commit is contained in:
parent
2247fbb23a
commit
7dccbace78
@ -48,7 +48,6 @@ __all__ = (
|
||||
'CategoryChannel',
|
||||
'StoreChannel',
|
||||
'GroupChannel',
|
||||
'_channel_factory',
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@ -1834,18 +1833,19 @@ class GroupChannel(discord.abc.Messageable, Hashable):
|
||||
|
||||
await self._state.http.leave_group(self.id)
|
||||
|
||||
def _channel_factory(channel_type):
|
||||
value = try_enum(ChannelType, channel_type)
|
||||
def _coerce_channel_type(value: Union[ChannelType, int]) -> ChannelType:
|
||||
if isinstance(value, ChannelType):
|
||||
return value
|
||||
return try_enum(ChannelType, value)
|
||||
|
||||
def _guild_channel_factory(channel_type: Union[ChannelType, int]):
|
||||
value = _coerce_channel_type(channel_type)
|
||||
if value is ChannelType.text:
|
||||
return TextChannel, value
|
||||
elif value is ChannelType.voice:
|
||||
return VoiceChannel, value
|
||||
elif value is ChannelType.private:
|
||||
return DMChannel, value
|
||||
elif value is ChannelType.category:
|
||||
return CategoryChannel, value
|
||||
elif value is ChannelType.group:
|
||||
return GroupChannel, value
|
||||
elif value is ChannelType.news:
|
||||
return TextChannel, value
|
||||
elif value is ChannelType.store:
|
||||
@ -1854,3 +1854,12 @@ def _channel_factory(channel_type):
|
||||
return StageChannel, value
|
||||
else:
|
||||
return None, value
|
||||
|
||||
def _channel_factory(channel_type: Union[ChannelType, int]):
|
||||
cls, value = _guild_channel_factory(channel_type)
|
||||
if value is ChannelType.private:
|
||||
return DMChannel, value
|
||||
elif value is ChannelType.group:
|
||||
return GroupChannel, value
|
||||
else:
|
||||
return cls, value
|
||||
|
916
discord/guild.py
916
discord/guild.py
File diff suppressed because it is too large
Load Diff
@ -44,6 +44,7 @@ from .mentions import AllowedMentions
|
||||
from .partial_emoji import PartialEmoji
|
||||
from .message import Message
|
||||
from .channel import *
|
||||
from .channel import _channel_factory
|
||||
from .raw_models import *
|
||||
from .member import Member
|
||||
from .role import Role
|
||||
|
Loading…
x
Reference in New Issue
Block a user