Skip to content
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

Closed
wants to merge 25 commits into from
Closed

Implement discord.Guild.get_or_fetch #1703

wants to merge 25 commits into from

Conversation

TheGiga
Copy link
Contributor

@TheGiga TheGiga commented Oct 15, 2022

Summary

Adds shortcut get_or_fetch method to discord.Guild. Shortcut to fetch stuff easily.

Information

  • This PR fixes an issue.
  • This PR adds something new (e.g. new method or parameters).
  • This PR is a breaking change (e.g. methods or parameters removed/renamed).
  • This PR is not a code change (e.g. documentation, README, typehinting,
    examples, ...).

Checklist

  • I have searched the open pull requests for duplicates.
  • If code changes were made then they have been tested.
    • I have updated the documentation to reflect the changes.
  • If type: ignore comments were used, a comment is also left explaining why.

@codecov
Copy link

codecov bot commented Oct 15, 2022

Codecov Report

Merging #1703 (0540992) into master (6b84ae9) will decrease coverage by 0.00%.
The diff coverage is 22.22%.

Additional details and impacted files

Impacted file tree graph

@@            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     
Flag Coverage Δ
macos-latest-3.10 33.12% <22.22%> (-0.01%) ⬇️
macos-latest-3.11 33.12% <22.22%> (-0.01%) ⬇️
macos-latest-3.8 33.13% <22.22%> (-0.01%) ⬇️
macos-latest-3.9 33.13% <22.22%> (-0.01%) ⬇️
ubuntu-latest-3.10 33.12% <22.22%> (-0.01%) ⬇️
ubuntu-latest-3.11 33.12% <22.22%> (-0.01%) ⬇️
ubuntu-latest-3.8 33.13% <22.22%> (-0.01%) ⬇️
ubuntu-latest-3.9 33.13% <22.22%> (-0.01%) ⬇️
windows-latest-3.10 33.12% <22.22%> (-0.01%) ⬇️
windows-latest-3.11 33.12% <22.22%> (-0.01%) ⬇️
windows-latest-3.8 33.13% <22.22%> (-0.01%) ⬇️
windows-latest-3.9 33.13% <22.22%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
discord/guild.py 23.81% <22.22%> (-0.02%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6b84ae9...0540992. Read the comment docs.

@BobDotCom
Copy link
Member

BobDotCom commented Oct 15, 2022

Since these are just shortcut methods, I think we should probably figure out a better long-term solution. Maybe a helper method, Guild.get_or_fetch("user", 123) or even Guild.get_or_fetch(User, 123). This could also handle other objects besides users and could even be added to other objects like Client. A further discussion in the discord might be helpful (See thread).

@Lulalaby Lulalaby added priority: medium Medium Priority feature Implements a feature labels Oct 16, 2022
@BobDotCom BobDotCom added the more info required more information is required to proceed label Oct 17, 2022
@BobDotCom BobDotCom marked this pull request as draft October 17, 2022 19:20
@TheGiga
Copy link
Contributor Author

TheGiga commented Oct 17, 2022

Since it's planned to make universal get_or_fetch method, is this PR still needed?

@NeloBlivion
Copy link
Member

Since it's planned to make universal get_or_fetch method, is this PR still needed?

The universal get_or_fetch method has always existed under utils, but it was only documented recently in #1682
Even though it exists, having these aliases is ultimately for convenience and has already been seen before with Client.get_or_fetch_user

return await utils.get_or_fetch(obj=self, attr="user", id=id, default=None)

TheGiga and others added 5 commits October 18, 2022 01:20
@TheGiga TheGiga changed the title Add get_or_fetch_member to discord.Guild Implement discord.Guild.get_or_fetch Oct 17, 2022
@TheGiga
Copy link
Contributor Author

TheGiga commented Oct 17, 2022

I'm not exactly sure about implementation, it seems pretty shitty, but it's the only way i made this thing work.
I'm may be missing something, but TypeVar doesn't throw an error if you pass into argument something that is not declared in that same TypeVar.

@TheGiga TheGiga marked this pull request as ready for review October 17, 2022 22:35
discord/guild.py Outdated Show resolved Hide resolved
@BobDotCom BobDotCom added status: in progress Work in Progess and removed more info required more information is required to proceed labels Oct 18, 2022
discord/guild.py Show resolved Hide resolved
discord/guild.py Outdated Show resolved Hide resolved
discord/guild.py Outdated Show resolved Hide resolved
@TheGiga
Copy link
Contributor Author

TheGiga commented Oct 18, 2022

Union just have better type hinting for this specific case, using bound didn't help with type checking. May be i am still missing on something.

@TheGiga TheGiga requested review from Dorukyum and BobDotCom and removed request for Dorukyum and BobDotCom October 18, 2022 18:59
@BobDotCom
Copy link
Member

Union just have better type hinting for this specific case, using bound didn't help with type checking. May be i am still missing on something.

Using bound does work. You need to use bound=Union[...].

@TheGiga
Copy link
Contributor Author

TheGiga commented Oct 18, 2022

Leaving it as is (it works). Waiting for Bob's PR about docstrings and stuff.

@BobDotCom BobDotCom marked this pull request as draft October 19, 2022 17:34
@BobDotCom
Copy link
Member

I've been thinking, would it be better to add a use_cached kwarg to all fetch_xx methods? It would use the same functionality as get_or_fetch and would be much cleaner IMO.

Comment on lines +939 to +946
elif t in [
VoiceChannel,
TextChannel,
ForumChannel,
StageChannel,
CategoryChannel,
Thread,
]:
Copy link
Contributor

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()"
Copy link
Contributor

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(
Copy link
Contributor

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]`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""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]`]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
-------

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Comment on lines +931 to +933
Usage
-----
``await GUILD_OBJECT.get_or_fetch(OBJECT_TYPE, OBJECT_ID)``
Copy link
Contributor

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:
Copy link
Contributor

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.

@EmreTech
Copy link
Contributor

EmreTech commented Dec 1, 2022

This PR description is outdated. The description talks about implementing a get_or_fetch_member method, but this PR now implements a get_or_fetch method.

@Lulalaby
Copy link
Member

Lulalaby commented Jan 5, 2023

stale

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Implements a feature priority: medium Medium Priority status: in progress Work in Progess
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants