Skip to content

Commit d0c524c

Browse files
committed
return None when fetching fails
1 parent b6ff5f3 commit d0c524c

File tree

3 files changed

+11
-125
lines changed

3 files changed

+11
-125
lines changed

discord/client.py

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,34 +1188,11 @@ async def get_or_fetch_user(self, id: int, /) -> User | None: # TODO: Remove in
11881188

11891189
return await self.get_or_fetch(object_type=User, object_id=id, default=None)
11901190

1191-
@overload
1192-
async def get_or_fetch(
1193-
self: Client,
1194-
object_type: type[_FETCHABLE],
1195-
object_id: Literal[None],
1196-
default: _D = ...,
1197-
) -> None | _D: ...
1198-
1199-
@overload
1200-
async def get_or_fetch(
1201-
self: Client,
1202-
object_type: type[_FETCHABLE],
1203-
object_id: int,
1204-
default: _D,
1205-
) -> _FETCHABLE | _D: ...
1206-
1207-
@overload
1208-
async def get_or_fetch(
1209-
self: Client,
1210-
object_type: type[_FETCHABLE],
1211-
object_id: int,
1212-
) -> _FETCHABLE: ...
1213-
12141191
async def get_or_fetch(
12151192
self: Client,
12161193
object_type: type[_FETCHABLE],
12171194
object_id: int | None,
1218-
default: _D = MISSING,
1195+
default: _D = None,
12191196
) -> _FETCHABLE | _D | None:
12201197
"""
12211198
Shortcut method to get data from an object either by returning the cached version, or if it does not exist, attempting to fetch it from the API.
@@ -1232,23 +1209,13 @@ async def get_or_fetch(
12321209
Returns
12331210
-------
12341211
VoiceChannel | TextChannel | ForumChannel | StageChannel | CategoryChannel | Thread | User | Guild | GuildEmoji | AppEmoji | None
1235-
The object of the specified type, or default if provided and not found, or None if not found and no default is provided.
1236-
1237-
Raises
1238-
------
1239-
:exc:`TypeError`
1240-
Raised when required parameters are missing or invalid types are provided.
1241-
:exc:`InvalidArgument`
1242-
Raised when an unsupported or incompatible object type is used.
1243-
:exc:`NotFound`
1244-
Invalid ID for the object.
1245-
:exc:`HTTPException`
1246-
An error occurred fetching the object.
1247-
:exc:`Forbidden`
1248-
You do not have permission to fetch the object.
1212+
The object if found, or `default` if provided when not found.
12491213
"""
12501214
return await utils.get_or_fetch(
1251-
obj=self, object_type=object_type, object_id=object_id, default=default
1215+
obj=self,
1216+
object_type=object_type,
1217+
object_id=object_id,
1218+
default=default,
12521219
)
12531220

12541221
# listeners/waiters

discord/guild.py

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -865,32 +865,11 @@ def get_member(self, user_id: int, /) -> Member | None:
865865
"""
866866
return self._members.get(user_id)
867867

868-
@overload
869-
async def get_or_fetch(
870-
self: Guild,
871-
object_type: type[_FETCHABLE],
872-
object_id: Literal[None],
873-
default: _D = ...,
874-
) -> None | _D: ...
875-
@overload
876-
async def get_or_fetch(
877-
self: Guild,
878-
object_type: type[_FETCHABLE],
879-
object_id: int,
880-
default: _D,
881-
) -> _FETCHABLE | _D: ...
882-
@overload
883-
async def get_or_fetch(
884-
self: Guild,
885-
object_type: type[_FETCHABLE],
886-
object_id: int,
887-
) -> _FETCHABLE: ...
888-
889868
async def get_or_fetch(
890869
self: Guild,
891870
object_type: type[_FETCHABLE],
892871
object_id: int | None,
893-
default: _D = MISSING,
872+
default: _D = None,
894873
) -> _FETCHABLE | _D | None:
895874
"""
896875
Shortcut method to get data from this guild either by returning the cached version,
@@ -910,21 +889,8 @@ async def get_or_fetch(
910889
Returns
911890
-------
912891
VoiceChannel | TextChannel | ForumChannel | StageChannel | CategoryChannel | Thread | Role | Member | GuildEmoji | None
913-
The object if found, or ``default`` if provided when not found.
914-
Returns ``None`` only if ``object_id`` is ``None`` and no ``default`` is given.
892+
The object if found, or `default` if provided when not found.
915893
916-
Raises
917-
------
918-
:exc:`TypeError`
919-
Raised when required parameters are missing or invalid types are provided.
920-
:exc:`InvalidArgument`
921-
Raised when an unsupported or incompatible object type is used.
922-
:exc:`NotFound`
923-
Invalid ID for the object.
924-
:exc:`HTTPException`
925-
An error occurred fetching the object.
926-
:exc:`Forbidden`
927-
You do not have permission to fetch the object.
928894
"""
929895
return await utils.get_or_fetch(
930896
obj=self,

discord/utils.py

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -594,44 +594,13 @@ def get(iterable: Iterable[T], **attrs: Any) -> T | None:
594594

595595
# TODO: In version 3.0, remove the 'attr' and 'id' arguments.
596596
# Also, eliminate the default 'MISSING' value for both 'object_type' and 'object_id'.
597-
@overload
598-
async def get_or_fetch(
599-
obj: Guild | Client,
600-
object_type: type[_FETCHABLE],
601-
object_id: Literal[None],
602-
default: _D = ...,
603-
attr: str = ...,
604-
id: int = ...,
605-
) -> None | _D: ...
606-
607-
608-
@overload
609-
async def get_or_fetch(
610-
obj: Guild | Client,
611-
object_type: type[_FETCHABLE],
612-
object_id: int,
613-
default: _D,
614-
attr: str = ...,
615-
id: int = ...,
616-
) -> _FETCHABLE | _D: ...
617-
618-
619-
@overload
620-
async def get_or_fetch(
621-
obj: Guild | Client,
622-
object_type: type[_FETCHABLE],
623-
object_id: int,
624-
*,
625-
attr: str = ...,
626-
id: int = ...,
627-
) -> _FETCHABLE: ...
628597

629598

630599
async def get_or_fetch(
631600
obj: Guild | Client,
632601
object_type: type[_FETCHABLE] = MISSING,
633602
object_id: int | None = MISSING,
634-
default: _D = MISSING,
603+
default: _D = None,
635604
attr: str = MISSING,
636605
id: int = MISSING,
637606
) -> _FETCHABLE | _D | None:
@@ -645,7 +614,7 @@ async def get_or_fetch(
645614
object_type: VoiceChannel | TextChannel | ForumChannel | StageChannel | CategoryChannel | Thread | User | Guild | Role | Member | GuildEmoji | AppEmoji
646615
Type of object to fetch or get.
647616
648-
object_id: :class:`int`
617+
object_id: int | None
649618
ID of object to get.
650619
651620
default : Any | None
@@ -656,20 +625,6 @@ async def get_or_fetch(
656625
657626
VoiceChannel | TextChannel | ForumChannel | StageChannel | CategoryChannel | Thread | User | Guild | Role | Member | GuildEmoji | AppEmoji | None
658627
The object if found, or `default` if provided when not found.
659-
Returns `None` only if `object_id` is None and no `default` is given.
660-
661-
Raises
662-
------
663-
:exc:`TypeError`
664-
Raised when required parameters are missing or invalid types are provided.
665-
:exc:`InvalidArgument`
666-
Raised when an unsupported or incompatible object type is used.
667-
:exc:`NotFound`
668-
Invalid ID for the object.
669-
:exc:`HTTPException`
670-
An error occurred fetching the object.
671-
:exc:`Forbidden`
672-
You do not have permission to fetch the object.
673628
"""
674629
from discord import AppEmoji, Client, Guild, Member, Role, User, abc, emoji
675630

@@ -782,9 +737,7 @@ async def get_or_fetch(
782737
try:
783738
return await fetcher(obj, object_id)
784739
except (HTTPException, ValueError):
785-
if default is not MISSING:
786-
return default
787-
raise
740+
return default
788741

789742

790743
def _unique(iterable: Iterable[T]) -> list[T]:

0 commit comments

Comments
 (0)