Skip to content

Commit ea555a1

Browse files
committed
back to non breaking
1 parent d0c524c commit ea555a1

File tree

3 files changed

+84
-29
lines changed

3 files changed

+84
-29
lines changed

discord/client.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,13 +1210,23 @@ async def get_or_fetch(
12101210
-------
12111211
VoiceChannel | TextChannel | ForumChannel | StageChannel | CategoryChannel | Thread | User | Guild | GuildEmoji | AppEmoji | None
12121212
The object if found, or `default` if provided when not found.
1213+
1214+
Raises
1215+
------
1216+
:exc:`TypeError`
1217+
Raised when required parameters are missing or invalid types are provided.
1218+
:exc:`InvalidArgument`
1219+
Raised when an unsupported or incompatible object type is used.
12131220
"""
1214-
return await utils.get_or_fetch(
1215-
obj=self,
1216-
object_type=object_type,
1217-
object_id=object_id,
1218-
default=default,
1219-
)
1221+
try:
1222+
return await utils.get_or_fetch(
1223+
obj=self,
1224+
object_type=object_type,
1225+
object_id=object_id,
1226+
default=default,
1227+
)
1228+
except (HTTPException, ValueError):
1229+
return default
12201230

12211231
# listeners/waiters
12221232

discord/guild.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
VoiceRegion,
6565
try_enum,
6666
)
67-
from .errors import ClientException, InvalidArgument, InvalidData
67+
from .errors import ClientException, InvalidArgument, InvalidData, HTTPException
6868
from .file import File
6969
from .flags import SystemChannelFlags
7070
from .integrations import Integration, _integration_factory
@@ -891,13 +891,22 @@ async def get_or_fetch(
891891
VoiceChannel | TextChannel | ForumChannel | StageChannel | CategoryChannel | Thread | Role | Member | GuildEmoji | None
892892
The object if found, or `default` if provided when not found.
893893
894+
Raises
895+
------
896+
:exc:`TypeError`
897+
Raised when required parameters are missing or invalid types are provided.
898+
:exc:`InvalidArgument`
899+
Raised when an unsupported or incompatible object type is used.
894900
"""
895-
return await utils.get_or_fetch(
896-
obj=self,
897-
object_type=object_type,
898-
object_id=object_id,
899-
default=default,
900-
)
901+
try:
902+
return await utils.get_or_fetch(
903+
obj=self,
904+
object_type=object_type,
905+
object_id=object_id,
906+
default=default,
907+
)
908+
except (HTTPException, ValueError):
909+
return default
901910

902911
@property
903912
def premium_subscribers(self) -> list[Member]:

discord/utils.py

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -594,13 +594,44 @@ 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: ...
597628

598629

599630
async def get_or_fetch(
600631
obj: Guild | Client,
601632
object_type: type[_FETCHABLE] = MISSING,
602633
object_id: int | None = MISSING,
603-
default: _D = None,
634+
default: _D = MISSING,
604635
attr: str = MISSING,
605636
id: int = MISSING,
606637
) -> _FETCHABLE | _D | None:
@@ -625,6 +656,20 @@ async def get_or_fetch(
625656
626657
VoiceChannel | TextChannel | ForumChannel | StageChannel | CategoryChannel | Thread | User | Guild | Role | Member | GuildEmoji | AppEmoji | None
627658
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.
628673
"""
629674
from discord import AppEmoji, Client, Guild, Member, Role, User, abc, emoji
630675

@@ -672,19 +717,6 @@ async def get_or_fetch(
672717
if object_type is MISSING or object_id is MISSING:
673718
raise TypeError("required parameters: `object_type` and `object_id`.")
674719

675-
if issubclass(object_type, (Member, User, Guild)):
676-
attr = object_type.__name__.lower()
677-
elif issubclass(object_type, emoji._EmojiTag):
678-
attr = "emoji"
679-
elif issubclass(object_type, Role):
680-
attr = "role"
681-
elif issubclass(object_type, abc.GuildChannel):
682-
attr = "channel"
683-
else:
684-
raise InvalidArgument(
685-
f"Class {object_type.__name__} cannot be used with discord.{type(obj).__name__}.get_or_fetch()"
686-
)
687-
688720
if isinstance(obj, Guild) and object_type is User:
689721
raise InvalidArgument(
690722
"Guild cannot get_or_fetch discord.User. Use Client instead."
@@ -728,7 +760,9 @@ async def get_or_fetch(
728760
)
729761
getter, fetcher = getter_fetcher_map[base_type]
730762
except KeyError:
731-
raise InvalidArgument(f"Unsupported object type: {object_type.__name__}")
763+
raise InvalidArgument(
764+
f"Class {object_type.__name__} cannot be used with discord.{type(obj).__name__}.get_or_fetch()"
765+
)
732766

733767
result = getter(obj, object_id)
734768
if result is not None:
@@ -737,7 +771,9 @@ async def get_or_fetch(
737771
try:
738772
return await fetcher(obj, object_id)
739773
except (HTTPException, ValueError):
740-
return default
774+
if default is not MISSING:
775+
return default
776+
raise
741777

742778

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

0 commit comments

Comments
 (0)