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

Return new instance in Poll.end to avoid inconsistencies #10056

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ilovetocode2019
Copy link

Summary

Currently, the Poll.end method updates the Poll.message attribute with the response from the API call, but still returns the current instance. This can cause Poll.message to be newer than the rest of the data stored in Poll. This also means that the the instance is being updated in-place regardless of whether it is from the state, which I can't find an intentional reason for. Both of these seem inconsistent with usual library behavior.

The following example shows this behavior:

poll = discord.Poll(question="What pet is better?", duration=datetime.timedelta(hours=1))
poll.add_answer(text="Dogs")
poll.add_answer(text="Cats")
message = await messageable.send(poll=poll)

# after waiting for a bit
stopped_poll = await poll.end()
stopped_poll.answers[0].vote_count == stopped_poll.message.poll.answers[0].vote_count # false if people voted
message is poll.message # always false

This PR changes the behavior to instead return the poll created from the API call inside Poll.end. This means that making an API call will not modify the instance used to make it, instead returning a fresh one, with more updated and consistent data.

Checklist

  • If code changes were made then they have been tested.
    • I have updated the documentation to reflect the changes.
  • 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, ...)

self._message = await self._message.end_poll()

return self
message = await self._message.end_poll()
Copy link
Contributor

Choose a reason for hiding this comment

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

._message still needs to be set, plus, you can simply do self._update(message) to update the data

Copy link
Author

Choose a reason for hiding this comment

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

Ah, I went with this way intentionally because I wasn’t sure if the update should even be in-place. I’ll change it to make sure everything is done to the current instance.

Copy link
Contributor

Choose a reason for hiding this comment

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

You could change the _update behaviour to update the current poll vote count and such, though

discord/poll.py Outdated
@@ -547,7 +547,7 @@ def get_answer(

return self._answers.get(id)

async def end(self) -> Self:
async def end(self) -> Poll:
Copy link
Contributor

@DA-344 DA-344 Dec 28, 2024

Choose a reason for hiding this comment

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

This is typed likeSelf for subclasses

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants