|
39 | 39 | Generator, |
40 | 40 | Sequence, |
41 | 41 | TypeVar, |
| 42 | + overload, |
42 | 43 | ) |
43 | 44 |
|
44 | 45 | import aiohttp |
|
68 | 69 | from .threads import Thread |
69 | 70 | from .ui.view import View |
70 | 71 | from .user import ClientUser, User |
71 | | -from .utils import _FETCHABLE, MISSING |
| 72 | +from .utils import _FETCHABLE, MISSING, _D |
72 | 73 | from .voice_client import VoiceClient |
73 | 74 | from .webhook import Webhook |
74 | 75 | from .widget import Widget |
@@ -247,9 +248,9 @@ def __init__( |
247 | 248 | self.loop: asyncio.AbstractEventLoop = ( |
248 | 249 | asyncio.get_event_loop() if loop is None else loop |
249 | 250 | ) |
250 | | - self._listeners: dict[str, list[tuple[asyncio.Future, Callable[..., bool]]]] = ( |
251 | | - {} |
252 | | - ) |
| 251 | + self._listeners: dict[ |
| 252 | + str, list[tuple[asyncio.Future, Callable[..., bool]]] |
| 253 | + ] = {} |
253 | 254 | self.shard_id: int | None = options.get("shard_id") |
254 | 255 | self.shard_count: int | None = options.get("shard_count") |
255 | 256 |
|
@@ -1186,37 +1187,64 @@ async def get_or_fetch_user(self, id: int, /) -> User | None: # TODO: Remove in |
1186 | 1187 |
|
1187 | 1188 | return await self.get_or_fetch(object_type=User, object_id=id, default=None) |
1188 | 1189 |
|
| 1190 | + @overload |
| 1191 | + async def get_or_fetch( |
| 1192 | + self: Client, |
| 1193 | + object_type: type[_FETCHABLE], |
| 1194 | + object_id: None, |
| 1195 | + default: _D = ..., |
| 1196 | + ) -> None | _D: ... |
| 1197 | + |
| 1198 | + @overload |
1189 | 1199 | async def get_or_fetch( |
1190 | 1200 | self: Client, |
1191 | 1201 | object_type: type[_FETCHABLE], |
| 1202 | + object_id: int, |
| 1203 | + default: _D, |
| 1204 | + ) -> _FETCHABLE | _D: ... |
| 1205 | + |
| 1206 | + @overload |
| 1207 | + async def get_or_fetch( |
| 1208 | + self: "Client", |
| 1209 | + object_type: type[_FETCHABLE], |
| 1210 | + object_id: int, |
| 1211 | + ) -> _FETCHABLE: ... |
| 1212 | + |
| 1213 | + async def get_or_fetch( |
| 1214 | + self: "Client", |
| 1215 | + object_type: type[_FETCHABLE], |
1192 | 1216 | object_id: int | None, |
1193 | | - default: Any = MISSING, |
1194 | | - ) -> _FETCHABLE | None: |
| 1217 | + default: _D = MISSING, |
| 1218 | + ) -> _FETCHABLE | _D | None: |
1195 | 1219 | """ |
1196 | 1220 | 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. |
1197 | 1221 |
|
1198 | 1222 | Parameters |
1199 | 1223 | ---------- |
1200 | | - object_type: Union[:class:`VoiceChannel`, :class:`TextChannel`, :class:`ForumChannel`, :class:`StageChannel`, :class:`CategoryChannel`, :class:`Thread`, :class:`User`, :class:`Guild`, :class:`GuildEmoji`, :class:`AppEmoji`] |
| 1224 | + object_type: VoiceChannel | TextChannel | ForumChannel | StageChannel | CategoryChannel | Thread | User | Guild | GuildEmoji | AppEmoji |
1201 | 1225 | Type of object to fetch or get. |
1202 | | - object_id: :class:`int` |
1203 | | - ID of object to get. |
1204 | | - default : Any, optional |
| 1226 | + object_id: int | None |
| 1227 | + ID of object to get. If None, returns default if provided, else None. |
| 1228 | + default: Any, optional |
1205 | 1229 | A default to return instead of raising if fetch fails. |
1206 | 1230 |
|
1207 | 1231 | Returns |
1208 | 1232 | ------- |
1209 | | - Optional[Union[:class:`VoiceChannel`, :class:`TextChannel`, :class:`ForumChannel`, :class:`StageChannel`, :class:`CategoryChannel`, :class:`Thread`, :class:`User`, :class:`Guild`, :class:`GuildEmoji`, :class:`AppEmoji`]] |
1210 | | - The object of type that was specified or ``None`` if not found. |
| 1233 | + VoiceChannel | TextChannel | ForumChannel | StageChannel | CategoryChannel | Thread | User | Guild | GuildEmoji | AppEmoji | None |
| 1234 | + The object of the specified type, or default if provided and not found, or None if not found and no default is provided. |
1211 | 1235 |
|
1212 | 1236 | Raises |
1213 | 1237 | ------ |
| 1238 | + :exc:`TypeError` |
| 1239 | + Raised when required parameters are missing or invalid types are provided. |
| 1240 | + :exc:`InvalidArgument` |
| 1241 | + Raised when an unsupported or incompatible object type is used. |
1214 | 1242 | :exc:`NotFound` |
1215 | | - Invalid ID for the object |
| 1243 | + Invalid ID for the object. |
1216 | 1244 | :exc:`HTTPException` |
1217 | | - An error occurred fetching the object |
| 1245 | + An error occurred fetching the object. |
1218 | 1246 | :exc:`Forbidden` |
1219 | | - You do not have permission to fetch the object |
| 1247 | + You do not have permission to fetch the object. |
1220 | 1248 | """ |
1221 | 1249 | return await utils.get_or_fetch( |
1222 | 1250 | obj=self, object_type=object_type, object_id=object_id, default=default |
|
0 commit comments