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

@ -25,7 +25,7 @@ DEALINGS IN THE SOFTWARE.
from __future__ import annotations
import datetime
from typing import Any, Dict, Final, List, Mapping, Protocol, TYPE_CHECKING, Type, TypeVar, Union
from typing import Any, Dict, Final, List, Mapping, Protocol, TYPE_CHECKING, TypeVar, Union
from . import utils
from .colour import Colour
@ -66,10 +66,10 @@ class EmbedProxy:
return EmptyEmbed
E = TypeVar('E', bound='Embed')
if TYPE_CHECKING:
from discord.types.embed import Embed as EmbedData, EmbedType
from typing_extensions import Self
from .types.embed import Embed as EmbedData, EmbedType
T = TypeVar('T')
MaybeEmpty = Union[T, _EmptyEmbed]
@ -207,7 +207,7 @@ class Embed:
self.timestamp = timestamp
@classmethod
def from_dict(cls: Type[E], data: Mapping[str, Any]) -> E:
def from_dict(cls, data: Mapping[str, Any]) -> Self:
"""Converts a :class:`dict` to a :class:`Embed` provided it is in the
format that Discord expects it to be in.
@ -223,7 +223,7 @@ class Embed:
The dictionary to convert into an embed.
"""
# we are bypassing __init__ here since it doesn't apply here
self: E = cls.__new__(cls)
self = cls.__new__(cls)
# fill in the basic fields
@ -263,7 +263,7 @@ class Embed:
return self
def copy(self: E) -> E:
def copy(self) -> Self:
"""Returns a shallow copy of the embed."""
return self.__class__.from_dict(self.to_dict())
@ -347,7 +347,7 @@ class Embed:
# Lying to the type checker for better developer UX.
return EmbedProxy(getattr(self, '_footer', {})) # type: ignore
def set_footer(self: E, *, text: MaybeEmpty[Any] = EmptyEmbed, icon_url: MaybeEmpty[Any] = EmptyEmbed) -> E:
def set_footer(self, *, text: MaybeEmpty[Any] = EmptyEmbed, icon_url: MaybeEmpty[Any] = EmptyEmbed) -> Self:
"""Sets the footer for the embed content.
This function returns the class instance to allow for fluent-style
@ -370,7 +370,7 @@ class Embed:
return self
def remove_footer(self: E) -> E:
def remove_footer(self) -> Self:
"""Clears embed's footer information.
This function returns the class instance to allow for fluent-style
@ -401,7 +401,7 @@ class Embed:
# Lying to the type checker for better developer UX.
return EmbedProxy(getattr(self, '_image', {})) # type: ignore
def set_image(self: E, *, url: MaybeEmpty[Any]) -> E:
def set_image(self, *, url: MaybeEmpty[Any]) -> Self:
"""Sets the image for the embed content.
This function returns the class instance to allow for fluent-style
@ -444,7 +444,7 @@ class Embed:
# Lying to the type checker for better developer UX.
return EmbedProxy(getattr(self, '_thumbnail', {})) # type: ignore
def set_thumbnail(self: E, *, url: MaybeEmpty[Any]) -> E:
def set_thumbnail(self, *, url: MaybeEmpty[Any]) -> Self:
"""Sets the thumbnail for the embed content.
This function returns the class instance to allow for fluent-style
@ -508,7 +508,7 @@ class Embed:
# Lying to the type checker for better developer UX.
return EmbedProxy(getattr(self, '_author', {})) # type: ignore
def set_author(self: E, *, name: Any, url: MaybeEmpty[Any] = EmptyEmbed, icon_url: MaybeEmpty[Any] = EmptyEmbed) -> E:
def set_author(self, *, name: Any, url: MaybeEmpty[Any] = EmptyEmbed, icon_url: MaybeEmpty[Any] = EmptyEmbed) -> Self:
"""Sets the author for the embed content.
This function returns the class instance to allow for fluent-style
@ -536,7 +536,7 @@ class Embed:
return self
def remove_author(self: E) -> E:
def remove_author(self) -> Self:
"""Clears embed's author information.
This function returns the class instance to allow for fluent-style
@ -562,7 +562,7 @@ class Embed:
# Lying to the type checker for better developer UX.
return [EmbedProxy(d) for d in getattr(self, '_fields', [])] # type: ignore
def add_field(self: E, *, name: Any, value: Any, inline: bool = True) -> E:
def add_field(self, *, name: Any, value: Any, inline: bool = True) -> Self:
"""Adds a field to the embed object.
This function returns the class instance to allow for fluent-style
@ -591,7 +591,7 @@ class Embed:
return self
def insert_field_at(self: E, index: int, *, name: Any, value: Any, inline: bool = True) -> E:
def insert_field_at(self, index: int, *, name: Any, value: Any, inline: bool = True) -> Self:
"""Inserts a field before a specified index to the embed.
This function returns the class instance to allow for fluent-style
@ -652,7 +652,7 @@ class Embed:
except (AttributeError, IndexError):
pass
def set_field_at(self: E, index: int, *, name: Any, value: Any, inline: bool = True) -> E:
def set_field_at(self, index: int, *, name: Any, value: Any, inline: bool = True) -> Self:
"""Modifies a field to the embed object.
The index must point to a valid pre-existing field.