Use typing.Self throughout library

This commit is contained in:
Josh
2022-03-01 22:53:24 +10:00
committed by GitHub
parent a90e1824f4
commit 147948af9b
28 changed files with 212 additions and 191 deletions

View File

@ -29,7 +29,7 @@ import inspect
import itertools
import sys
from operator import attrgetter
from typing import Any, Dict, List, Literal, Optional, TYPE_CHECKING, Tuple, Type, TypeVar, Union, overload
from typing import Any, Dict, List, Literal, Optional, TYPE_CHECKING, Tuple, Union
import discord.abc
@ -49,6 +49,8 @@ __all__ = (
)
if TYPE_CHECKING:
from typing_extensions import Self
from .asset import Asset
from .channel import DMChannel, VoiceChannel, StageChannel
from .flags import PublicUserFlags
@ -203,9 +205,6 @@ def flatten_user(cls):
return cls
M = TypeVar('M', bound='Member')
@flatten_user
class Member(discord.abc.Messageable, _UserTag):
"""Represents a Discord member to a :class:`Guild`.
@ -329,7 +328,7 @@ class Member(discord.abc.Messageable, _UserTag):
return hash(self._user)
@classmethod
def _from_message(cls: Type[M], *, message: Message, data: MemberPayload) -> M:
def _from_message(cls, *, message: Message, data: MemberPayload) -> Self:
author = message.author
data['user'] = author._to_minimal_user_json() # type: ignore
return cls(data=data, guild=message.guild, state=message._state) # type: ignore
@ -343,7 +342,7 @@ class Member(discord.abc.Messageable, _UserTag):
self.timed_out_until = utils.parse_time(data.get('communication_disabled_until'))
@classmethod
def _try_upgrade(cls: Type[M], *, data: UserWithMemberPayload, guild: Guild, state: ConnectionState) -> Union[User, M]:
def _try_upgrade(cls, *, data: UserWithMemberPayload, guild: Guild, state: ConnectionState) -> Union[User, Self]:
# A User object with a 'member' key
try:
member_data = data.pop('member')
@ -354,8 +353,8 @@ class Member(discord.abc.Messageable, _UserTag):
return cls(data=member_data, guild=guild, state=state) # type: ignore
@classmethod
def _copy(cls: Type[M], member: M) -> M:
self: M = cls.__new__(cls) # to bypass __init__
def _copy(cls, member: Self) -> Self:
self = cls.__new__(cls) # to bypass __init__
self._roles = utils.SnowflakeList(member._roles, is_sorted=True)
self.joined_at = member.joined_at