@@ -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
599630async 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
743779def _unique (iterable : Iterable [T ]) -> list [T ]:
0 commit comments