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

refactor: make ApplicationContext.user non-optional #1647

Merged
merged 6 commits into from Nov 18, 2022

Conversation

Dorukyum
Copy link
Member

Summary

At least one of the member and user fields in the interaction payload will be present unless it's a PING interaction. This change prevents type issues from occuring almost everywhere Interaction.user or ApplicationContext.user is used without a type check.

This will be a breaking change in case this attribute is accessed in a PING handler (such a case isn't in the lib) even though it would always be None.

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.

@Dorukyum Dorukyum added bug Something isn't working priority: low Low Priority status: awaiting review Awaiting review from a maintainer labels Sep 24, 2022
@Dorukyum Dorukyum self-assigned this Sep 24, 2022
Lulalaby
Lulalaby previously approved these changes Sep 24, 2022
Copy link
Member

@Lulalaby Lulalaby left a comment

Choose a reason for hiding this comment

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

lgtm

@Dorukyum Dorukyum enabled auto-merge (squash) September 27, 2022 08:39
Copy link
Member

@BobDotCom BobDotCom left a comment

Choose a reason for hiding this comment

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

Alternatively I feel it would be better to keep Interaction.user optional (since it is), and instead make ApplicationContext.user non-optional with a type: ignore.

discord/interactions.py Outdated Show resolved Hide resolved
discord/interactions.py Outdated Show resolved Hide resolved
discord/interactions.py Outdated Show resolved Hide resolved
discord/interactions.py Outdated Show resolved Hide resolved
discord/commands/context.py Outdated Show resolved Hide resolved
@BobDotCom BobDotCom added status: in progress Work in Progess and removed status: awaiting review Awaiting review from a maintainer labels Sep 30, 2022
@BobDotCom BobDotCom added this to the v2.2 milestone Sep 30, 2022
@Dorukyum
Copy link
Member Author

Dorukyum commented Oct 1, 2022

Alternatively I feel it would be better to keep Interaction.user optional (since it is), and instead make ApplicationContext.user non-optional with a type: ignore.

Why keep Interaction.user the same? Not to remove the tons of type errors that occur in components or on_interaction? If we're changing one attribute, I think we should change the other too here.

@BobDotCom
Copy link
Member

Alternatively I feel it would be better to keep Interaction.user optional (since it is), and instead make ApplicationContext.user non-optional with a type: ignore.

Why keep Interaction.user the same? Not to remove the tons of type errors that occur in components or on_interaction? If we're changing one attribute, I think we should change the other too here.

Because we should not make invalid type hints, which is what this would currently do. We can set it as optional and type: ignore when we know it will be present. Otherwise it defeats the purpose of having type hints, if they're wrong anyways.

@Dorukyum
Copy link
Member Author

Dorukyum commented Oct 1, 2022

Alternatively I feel it would be better to keep Interaction.user optional (since it is), and instead make ApplicationContext.user non-optional with a type: ignore.

Why keep Interaction.user the same? Not to remove the tons of type errors that occur in components or on_interaction? If we're changing one attribute, I think we should change the other too here.

Because we should not make invalid type hints, which is what this would currently do. We can set it as optional and type: ignore when we know it will be present. Otherwise it defeats the purpose of having type hints, if they're wrong anyways.

It'll only be wrong internally when PONG-ing interactions, if done

@BobDotCom BobDotCom modified the milestones: v2.2, v2.3 Oct 2, 2022
@Lulalaby
Copy link
Member

ping pong

@EmreTech
Copy link
Contributor

Alternatively I feel it would be better to keep Interaction.user optional (since it is), and instead make ApplicationContext.user non-optional with a type: ignore.

I do not agree with this approach. It's a way better idea to just change the type hints. It'll be better in the long run.
It would also be a fantastic idea to split interactions into different classes like Disnake did, but that's for another PR.

@BobDotCom
Copy link
Member

I do not agree with this approach. It's a way better idea to just change the type hints. It'll be better in the long run.

Why? In the solution I proposed, Interaction.user is type-hinted to Optional[User] which granted in most cases is a bit loose as it's typically not None. However, we cannot say that it is never None, because sometimes it is. That being said, we can say that it is never None for app command interactions, hence why ApplicationContext would not have an optional typehint.

It would also be a fantastic idea to split interactions into different classes like Disnake did, but that's for another PR.

That sounds like a potentially better approach. If we go this route, the best course of action may be closing this PR in favor of a new PR for the split-interaction approach.

@Dorukyum
Copy link
Member Author

Splitting interaction classes is a major change and will most likely be breaking. It might just as well create more type issues like the ones this pr aims to solve.

@BobDotCom BobDotCom removed this from the v2.3 milestone Nov 11, 2022
@Dorukyum Dorukyum changed the title Make interaction.user non-optional Make ApplicationContext.user non-optional Nov 18, 2022
Co-authored-by: BobDotCom <71356958+BobDotCom@users.noreply.github.com>
@Dorukyum Dorukyum changed the title Make ApplicationContext.user non-optional refactor: make ApplicationContext.user non-optional Nov 18, 2022
@Dorukyum
Copy link
Member Author

I've made changes after we decided to only make ApplicationContext.user non-optional and keep Interaction.user as-is.

@Lulalaby Lulalaby enabled auto-merge (squash) November 18, 2022 17:28
@Lulalaby Lulalaby merged commit ca7e541 into Pycord-Development:master Nov 18, 2022
@Dorukyum Dorukyum deleted the user-not-optional branch January 25, 2024 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority: low Low Priority status: in progress Work in Progess
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants