-
-
Notifications
You must be signed in to change notification settings - Fork 477
feat: Implement better get_or_fetch #2776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 89 commits
dce0206
b2b8884
8f9a257
9f6219c
4af5149
6f59fcd
c0689cc
b20822a
4a363f5
c3a7dd2
e614c47
0c0a042
466e41b
388a234
9737f69
656de14
461eab7
b6f8878
f2a7d72
5d68207
4ca87d8
9903fb7
a1ec6f9
47de080
4430825
010cc78
368a55d
76c56a8
811682d
a102d5d
528b84b
7e766be
f6b8e18
a5deffe
fff74c4
9e45627
0baae1d
a3aeb4b
2404d9c
23409a7
8f278f8
6d5be54
11fbbc9
590339a
267d574
dc45449
f22d8a2
cbb7951
fdbb7d3
5b35d95
b8a8628
ba5541e
5421a3f
50318c2
541e668
b20cffb
3e94a4f
7384d7c
b832b55
0ffaa4b
92d2987
b8c7f7f
53dc93c
6d89376
e2e8b3d
2abfba2
fb9e077
c60fbab
29863f4
4a5eca4
2ed0de6
0c99851
dffef1f
f81a338
cc273c2
0302fcd
2837e94
1149c0a
16fda2a
b70c666
198556d
91b3d68
cb38bd7
f13e86c
543dec8
da59485
fa0efba
b6ff5f3
7d12503
77b2a18
d0c524c
ddf2248
6c6e33a
ea555a1
e0a4745
6db3f56
59768c1
890cc53
f6d6206
d6daa2d
11035ec
8094315
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,17 @@ | |
import sys | ||
import traceback | ||
from types import TracebackType | ||
from typing import TYPE_CHECKING, Any, Callable, Coroutine, Generator, Sequence, TypeVar | ||
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
Callable, | ||
Coroutine, | ||
Generator, | ||
Literal, | ||
Sequence, | ||
TypeVar, | ||
overload, | ||
) | ||
|
||
import aiohttp | ||
|
||
|
@@ -60,18 +70,26 @@ | |
from .threads import Thread | ||
from .ui.view import View | ||
from .user import ClientUser, User | ||
from .utils import MISSING | ||
from .utils import _D, _FETCHABLE, MISSING | ||
from .voice_client import VoiceClient | ||
from .webhook import Webhook | ||
from .widget import Widget | ||
|
||
if TYPE_CHECKING: | ||
from .abc import GuildChannel, PrivateChannel, Snowflake, SnowflakeTime | ||
from .channel import DMChannel | ||
from .channel import ( | ||
CategoryChannel, | ||
DMChannel, | ||
ForumChannel, | ||
StageChannel, | ||
TextChannel, | ||
VoiceChannel, | ||
) | ||
from .interaction import Interaction | ||
from .member import Member | ||
from .message import Message | ||
from .poll import Poll | ||
from .threads import Thread, ThreadMember | ||
from .ui.item import Item | ||
from .voice_client import VoiceProtocol | ||
|
||
|
@@ -1147,7 +1165,12 @@ def get_all_members(self) -> Generator[Member]: | |
for guild in self.guilds: | ||
yield from guild.members | ||
|
||
async def get_or_fetch_user(self, id: int, /) -> User | None: | ||
Lumabots marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@utils.deprecated( | ||
instead="Client.get_or_fetch(User, id)", | ||
since="2.7", | ||
removed="3.0", | ||
) | ||
async def get_or_fetch_user(self, id: int, /) -> User | None: # TODO: Remove in 3.0 | ||
"""|coro| | ||
|
||
Looks up a user in the user cache or fetches if not found. | ||
|
@@ -1163,7 +1186,70 @@ async def get_or_fetch_user(self, id: int, /) -> User | None: | |
The user or ``None`` if not found. | ||
""" | ||
|
||
return await utils.get_or_fetch(obj=self, attr="user", id=id, default=None) | ||
return await self.get_or_fetch(object_type=User, object_id=id, default=None) | ||
|
||
@overload | ||
async def get_or_fetch( | ||
self: Client, | ||
object_type: type[_FETCHABLE], | ||
object_id: Literal[None], | ||
default: _D = ..., | ||
) -> None | _D: ... | ||
|
||
@overload | ||
async def get_or_fetch( | ||
self: Client, | ||
object_type: type[_FETCHABLE], | ||
object_id: int, | ||
default: _D, | ||
) -> _FETCHABLE | _D: ... | ||
|
||
@overload | ||
async def get_or_fetch( | ||
self: Client, | ||
object_type: type[_FETCHABLE], | ||
object_id: int, | ||
) -> _FETCHABLE: ... | ||
|
||
async def get_or_fetch( | ||
self: Client, | ||
object_type: type[_FETCHABLE], | ||
object_id: int | None, | ||
default: _D = MISSING, | ||
) -> _FETCHABLE | _D | None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These type vars don't have to and should not be imported from utils afaik, also no need to type self. Also because some things can't be getorfetched from Client vs Guild so they should have their own There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we talked about it with plun and he suggests me to import it from utils There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure about that. @plun1331 what's your reasoning ? |
||
""" | ||
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. | ||
|
||
Parameters | ||
---------- | ||
object_type: VoiceChannel | TextChannel | ForumChannel | StageChannel | CategoryChannel | Thread | User | Guild | GuildEmoji | AppEmoji | ||
Type of object to fetch or get. | ||
object_id: int | None | ||
ID of object to get. If None, returns default if provided, else None. | ||
default: Any | None | ||
A default to return instead of raising if fetch fails. | ||
|
||
Returns | ||
------- | ||
VoiceChannel | TextChannel | ForumChannel | StageChannel | CategoryChannel | Thread | User | Guild | GuildEmoji | AppEmoji | None | ||
The object of the specified type, or default if provided and not found, or None if not found and no default is provided. | ||
|
||
Raises | ||
------ | ||
:exc:`TypeError` | ||
Raised when required parameters are missing or invalid types are provided. | ||
:exc:`InvalidArgument` | ||
Raised when an unsupported or incompatible object type is used. | ||
:exc:`NotFound` | ||
Invalid ID for the object. | ||
:exc:`HTTPException` | ||
An error occurred fetching the object. | ||
:exc:`Forbidden` | ||
You do not have permission to fetch the object. | ||
""" | ||
return await utils.get_or_fetch( | ||
obj=self, object_type=object_type, object_id=object_id, default=default | ||
) | ||
|
||
# listeners/waiters | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.