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

@ -24,7 +24,7 @@ DEALINGS IN THE SOFTWARE.
from __future__ import annotations
from typing import Any, Dict, Optional, TYPE_CHECKING, Type, TypeVar, Union
from typing import Any, Dict, Optional, TYPE_CHECKING, Union
import re
from .asset import Asset, AssetMixin
@ -37,6 +37,8 @@ __all__ = (
# fmt: on
if TYPE_CHECKING:
from typing_extensions import Self
from .state import ConnectionState
from datetime import datetime
from .types.message import PartialEmoji as PartialEmojiPayload
@ -51,9 +53,6 @@ class _EmojiTag:
raise NotImplementedError
PE = TypeVar('PE', bound='PartialEmoji')
class PartialEmoji(_EmojiTag, AssetMixin):
"""Represents a "partial" emoji.
@ -106,7 +105,7 @@ class PartialEmoji(_EmojiTag, AssetMixin):
self._state: Optional[ConnectionState] = None
@classmethod
def from_dict(cls: Type[PE], data: Union[PartialEmojiPayload, Dict[str, Any]]) -> PE:
def from_dict(cls, data: Union[PartialEmojiPayload, Dict[str, Any]]) -> Self:
return cls(
animated=data.get('animated', False),
id=utils._get_as_snowflake(data, 'id'),
@ -114,7 +113,7 @@ class PartialEmoji(_EmojiTag, AssetMixin):
)
@classmethod
def from_str(cls: Type[PE], value: str) -> PE:
def from_str(cls, value: str) -> Self:
"""Converts a Discord string representation of an emoji to a :class:`PartialEmoji`.
The formats accepted are:
@ -161,8 +160,13 @@ class PartialEmoji(_EmojiTag, AssetMixin):
@classmethod
def with_state(
cls: Type[PE], state: ConnectionState, *, name: str, animated: bool = False, id: Optional[int] = None
) -> PE:
cls,
state: ConnectionState,
*,
name: str,
animated: bool = False,
id: Optional[int] = None,
) -> Self:
self = cls(name=name, animated=animated, id=id)
self._state = state
return self