-
-
Notifications
You must be signed in to change notification settings - Fork 455
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
Implement discord.Guild.get_or_fetch
#1703
Conversation
i hope it works lol
for more information, see https://pre-commit.ci
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## master #1703 +/- ##
==========================================
- Coverage 33.14% 33.13% -0.01%
==========================================
Files 96 96
Lines 18513 18522 +9
==========================================
+ Hits 6136 6138 +2
- Misses 12377 12384 +7
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
Since these are just shortcut methods, I think we should probably figure out a better long-term solution. Maybe a helper method, |
Since it's planned to make universal |
The universal Line 1017 in 75bc6fe
|
add discord.Guild.get_or_fetch()
for more information, see https://pre-commit.ci
why can't people just see spelling mistakes before commiting
for more information, see https://pre-commit.ci
get_or_fetch_member
to discord.Guilddiscord.Guild.get_or_fetch
I'm not exactly sure about implementation, it seems pretty shitty, but it's the only way i made this thing work. |
for more information, see https://pre-commit.ci
Union just have better type hinting for this specific case, using |
Using bound does work. You need to use |
for more information, see https://pre-commit.ci
Leaving it as is (it works). Waiting for Bob's PR about docstrings and stuff. |
I've been thinking, would it be better to add a |
elif t in [ | ||
VoiceChannel, | ||
TextChannel, | ||
ForumChannel, | ||
StageChannel, | ||
CategoryChannel, | ||
Thread, | ||
]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The list of channel classes here should be a tuple because you do not need mutability here.
return "channel" | ||
|
||
raise InvalidArgument( | ||
f"Class {object_type} cannot be used with discord.Guild.get_or_fetch()" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use the name of the class (__name__
) here so the exception looks more human friendly.
@@ -895,6 +896,64 @@ def get_member(self, user_id: int, /) -> Member | None: | |||
""" | |||
return self._members.get(user_id) | |||
|
|||
_FETCHABLE = TypeVar( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually type variables are defined at the very top of a Python file, is there any specific reason it's defined in the Guild class here?
|
||
Parameters | ||
---------- | ||
object_type: :class:`Type[VoiceChannel, TextChannel, ForumChannel, StageChannel, CategoryChannel, Thread, Member]` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
object_type: :class:`Type[VoiceChannel, TextChannel, ForumChannel, StageChannel, CategoryChannel, Thread, Member]` | |
object_type: Union[:class:`VoiceChannel`, :class:`TextChannel`, :class:`ForumChannel`, :class:`StageChannel`, :class:`CategoryChannel`, :class:`Thread`, :class:`Member`] |
async def get_or_fetch( | ||
self, object_type: Type[_FETCHABLE], object_id: int, / | ||
) -> _FETCHABLE | None: | ||
"""Shortcut method to get data from guild object if it's cached or fetch from api if it's not. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"""Shortcut method to get data from guild object if it's cached or fetch from api if it's not. | |
"""Shortcut method to get data from guild object either by returning the cached version, or if it does not exist, attempt to fetch it from the api. |
Returns | ||
------- | ||
|
||
Optional[:class:`~Type[VoiceChannel, TextChannel, ForumChannel, StageChannel, CategoryChannel, Thread, Member]`] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional[:class:`~Type[VoiceChannel, TextChannel, ForumChannel, StageChannel, CategoryChannel, Thread, Member]`] | |
Optional[Union[:class:`VoiceChannel`, :class:`TextChannel`, :class:`ForumChannel`, :class:`StageChannel`, :class:`CategoryChannel`, :class:`Thread`, :class:`Member`]] |
|
||
Returns | ||
------- | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usage | ||
----- | ||
``await GUILD_OBJECT.get_or_fetch(OBJECT_TYPE, OBJECT_ID)`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the point of this? It should be very obvious by reading the rest of the documentation for this method. Instead, include a proper example (you can look at other portions of the code to see how examples are showcased).
``await GUILD_OBJECT.get_or_fetch(OBJECT_TYPE, OBJECT_ID)`` | ||
""" | ||
|
||
def get_attr_name(t: object_type) -> str: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was the point of even including the main portion of the code in a nested function? get_or_fetch
doesn't seem to take in a function argument that it can use to derive the name of the attribute.
This PR description is outdated. The description talks about implementing a |
stale |
Summary
Adds shortcut
get_or_fetch
method to discord.Guild. Shortcut to fetch stuff easily.Information
examples, ...).
Checklist
type: ignore
comments were used, a comment is also left explaining why.