Skip to content

Commit 3254ea2

Browse files
committed
style(typehints): Typehinted partial_emoji.py
Signed-off-by: Mathieu Corsham <[email protected]>
1 parent 442a74d commit 3254ea2

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

discord/partial_emoji.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@
2525
"""
2626
from __future__ import annotations
2727

28-
import re
29-
from .asset import Asset
30-
from . import utils
31-
3228
from typing import (
3329
Optional,
3430
TYPE_CHECKING
3531
)
32+
from typing_extensions import Literal
33+
34+
import re
35+
from .asset import Asset
36+
from . import utils
3637

3738
if TYPE_CHECKING:
39+
from .state import ConnectionState
3840
from datetime import datetime
41+
from .types.emoji import PartialEmoji as PartialEmojiPayload
3942

4043

4144
class _EmojiTag:
@@ -82,14 +85,14 @@ class PartialEmoji(_EmojiTag):
8285

8386
__slots__ = ('animated', 'name', 'id', '_state')
8487

85-
def __init__(self, *, name, animated=False, id=None):
86-
self.animated = animated
87-
self.name = name
88-
self.id = id
89-
self._state = None
88+
def __init__(self, *, name: str, animated: bool = False, id: Optional[int] = None) -> None :
89+
self.animated: bool = animated
90+
self.name: str = name
91+
self.id: Optional[int] = id
92+
self._state: Optional[ConnectionState] = None
9093

9194
@classmethod
92-
def from_string(cls, string: str):
95+
def from_string(cls, string: str) -> PartialEmoji:
9396
"""Converts a (custom) emoji as they are in a message into a partial emoji object.
9497
9598
.. note::
@@ -98,6 +101,11 @@ def from_string(cls, string: str):
98101
99102
.. warning::
100103
This does **not** support the :emoji: format for (standard) emojis
104+
105+
Returns
106+
--------
107+
:class:`PartialEmoji`
108+
The emoji object if the string was valid.
101109
"""
102110

103111
match = re.match('^<(a?):([\-\w]+):(\d+)>$', string)
@@ -106,14 +114,14 @@ def from_string(cls, string: str):
106114
return cls(name=string)
107115

108116
@classmethod
109-
def from_dict(cls, data):
117+
def from_dict(cls, data: PartialEmojiPayload) -> PartialEmoji:
110118
return cls(
111119
animated=data.get('animated', False),
112120
id=utils._get_as_snowflake(data, 'id'),
113121
name=data.get('name'),
114122
)
115123

116-
def to_dict(self):
124+
def to_dict(self) -> PartialEmojiPayload:
117125
o = {'name': self.name}
118126
if self.id:
119127
o['id'] = self.id
@@ -122,36 +130,43 @@ def to_dict(self):
122130
return o
123131

124132
@classmethod
125-
def with_state(cls, state, *, name, animated=False, id=None):
133+
def with_state(
134+
cls,
135+
state: ConnectionState,
136+
*,
137+
name: str,
138+
animated: bool = False,
139+
id: Optional[int] = None
140+
) -> PartialEmoji:
126141
self = cls(name=name, animated=animated, id=id)
127142
self._state = state
128143
return self
129144

130-
def __str__(self):
145+
def __str__(self) -> str:
131146
if self.id is None:
132147
return self.name
133148
if self.animated:
134149
return '<a:%s:%s>' % (self.name, self.id)
135150
return '<:%s:%s>' % (self.name, self.id)
136151

137-
def __len__(self):
152+
def __len__(self) -> int:
138153
return len(str(self))
139154

140-
def __repr__(self):
155+
def __repr__(self) -> str:
141156
return '<{0.__class__.__name__} animated={0.animated} name={0.name!r} id={0.id}>'.format(self)
142157

143158
def __eq__(self, other) -> bool:
144159
if self.is_unicode_emoji():
145160
return isinstance(other, PartialEmoji) and self.name == other.name
146161

147162
if isinstance(other, _EmojiTag):
148-
return self.id == other.id
163+
return self.id == other.id # type: ignore
149164
return False
150-
165+
151166
def __ne__(self, other) -> bool:
152167
return not self.__eq__(other)
153168

154-
def __hash__(self):
169+
def __hash__(self) -> int:
155170
return hash((self.id, self.name))
156171

157172
def is_custom_emoji(self) -> bool:
@@ -187,7 +202,12 @@ def url(self) -> Asset:
187202
"""
188203
return self.url_as(format=None)
189204

190-
def url_as(self, *, format: Optional[str] = None, static_format="png") -> Asset:
205+
def url_as(
206+
self,
207+
*,
208+
format: Optional[Literal['jpeg', 'jpg', 'webp', 'png', 'gif']] = None,
209+
static_format: Literal['jpeg', 'jpg', 'webp', 'png', ] = 'png'
210+
) -> Asset:
191211
"""Returns an :class:`Asset` for the emoji's url, if it is custom.
192212
193213
The format must be one of 'webp', 'jpeg', 'jpg', 'png' or 'gif'.

0 commit comments

Comments
 (0)