Skip to content

Commit 35e0e39

Browse files
committed
style: A few typing additions to utils.py
Signed-off-by: Mathieu Corsham <McCuber04@outlook.de>
1 parent e3ab3b4 commit 35e0e39

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

discord/abc.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,10 @@ class User(Snowflake, Protocol):
136136
137137
Attributes
138138
-----------
139-
name: :class:`str`
139+
username: :class:`str`
140140
The user's username.
141+
global_name: Optional[:class:`str`]
142+
The users display name, if set.
141143
discriminator: :class:`str`
142144
The user's discriminator.
143145
avatar: Optional[:class:`str`]
@@ -147,9 +149,18 @@ class User(Snowflake, Protocol):
147149
"""
148150

149151
__slots__ = ()
150-
152+
username: str
153+
global_name: Optional[str]
154+
discriminator: str
155+
avatar: Optional[str]
156+
bot: bool
157+
158+
@property
159+
def name(self):
160+
""":class:`str`: An alias for :attr:`name`."""
161+
raise NotImplementedError
162+
151163
@property
152-
@abc.abstractmethod
153164
def display_name(self):
154165
""":class:`str`: Returns the user's display name."""
155166
raise NotImplementedError

discord/utils.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141
Callable,
4242
TypeVar,
4343
Protocol,
44+
overload,
4445
TYPE_CHECKING,
45-
runtime_checkable
46+
runtime_checkable, Generic,
4647
)
4748

4849
import os
@@ -69,7 +70,7 @@
6970
from operator import attrgetter
7071

7172
from .errors import InvalidArgument
72-
from .enums import TimestampStyle
73+
from .enums import TimestampStyle, InviteTargetType
7374

7475
if TYPE_CHECKING:
7576
from typing_extensions import ParamSpec, TypeGuard, Self
@@ -86,6 +87,7 @@
8687

8788
T = TypeVar('T')
8889
R = TypeVar('R')
90+
_IterableClassType = TypeVar('_IterableClassType', bound=Type[Iterable])
8991
_Iterable = TypeVar('_Iterable', bound=Iterable)
9092
MaybeAwaitable = Union[Awaitable[T], T]
9193

@@ -508,17 +510,27 @@ async def countdown(ctx, seconds: int):
508510

509511

510512
async def create_voice_activity(channel: VoiceChannel, target_application_id: int, **kwargs):
511-
return await channel.create_invite(targe_type=2, target_application_id=target_application_id, **kwargs)
512-
513-
514-
def _unique(iterable: _Iterable[T]) -> _Iterable[T]:
513+
return await channel.create_invite(
514+
target_type=InviteTargetType.embedded_application,
515+
target_application_id=target_application_id,
516+
**kwargs
517+
)
518+
519+
@overload
520+
def _unique(iterable: _Iterable[T]) -> _Iterable[T]: ...
521+
522+
@overload
523+
def _unique(iterable: _Iterable[T], return_type: _IterableClassType) -> _IterableClassType[T]: ...
524+
# idk why this typing this doesn't work
525+
526+
def _unique(
527+
iterable: _Iterable[T],
528+
return_type: Optional[_IterableClassType] = None
529+
) -> Union[_Iterable[T], _IterableClassType[T]]:
515530
seen = set()
516531
adder = seen.add
517-
origin_type = type(iterable)
518-
if origin_type not in {list, tuple, set}:
519-
origin_type = list
520-
return origin_type([x for x in iterable if not (x in seen or adder(x))])
521-
532+
return_type = return_type or type(iterable)
533+
return return_type(x for x in iterable if not (x in seen or adder(x)))
522534

523535
def _get_as_snowflake(data: Dict[str, Any], key: str) -> Optional[int]:
524536
try:
@@ -532,7 +544,7 @@ def _get_as_snowflake(data: Dict[str, Any], key: str) -> Optional[int]:
532544
def _get_mime_type_for_image(data: bytes) -> str:
533545
if data.startswith(b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A'):
534546
return 'image/png'
535-
elif data[0:3] == b'\xff\xd8\xff' or data[6:10] in (b'JFIF', b'Exif'):
547+
elif data[:3] == b'\xff\xd8\xff' or data[6:10] in (b'JFIF', b'Exif'):
536548
return 'image/jpeg'
537549
elif data.startswith((b'\x47\x49\x46\x38\x37\x61', b'\x47\x49\x46\x38\x39\x61')):
538550
return 'image/gif'

0 commit comments

Comments
 (0)