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

feat: add additional operations to BaseFlags #1486

Merged
merged 16 commits into from
Jul 12, 2022
Merged

feat: add additional operations to BaseFlags #1486

merged 16 commits into from
Jul 12, 2022

Conversation

celsiusnarhwal
Copy link
Contributor

@celsiusnarhwal celsiusnarhwal commented Jul 10, 2022

Summary

This pull request implements addition (+), subtraction (-), union (|), intersection (&), and inversion (~) operations on the BaseFlags class and accordingly updates the documentation for its inheritors. The addition and union operations are equivalent and their operators interchangeable.

I made this change with the Permissions class in mind, intending to enable functionality such as adding two Permissions objects together to obtain the combined set of permissions they represent; subtracting two Permissions objects to set all permissions that are True in one object to False in the other; finding the intersection of two Permissions objects to obtain the set of permissions that are True in both; and inverting a Permissions object to set all of its True permissions to False and vice versa. However, this PR enables these operations on all inheritors of BaseFlags - the previously-described actions could be similarly applied to objects of the Intents class, for example.

The addition, subtraction, union, and intersection operations can operate on a BaseFlags object and a) another BaseFlags object, or b) a flag_value object. Reverse methods for each of these operations are also implemented, allowing operations between BaseFlags and flag_value objects to be properly commutative.

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.

Implement addition (+), subtraction (-), union (|), intersection (&), and inverse (~) operations on the `Permissions` class and update the documentation accordingly. The addition and union operations are equivalent and their operators are interchangeable (and the latter is consequently omitted from the updated documentation).

This allows for things such as adding two `Permissions` objects together to obtain the combined set of permissions they represent; subtracting two `Permissions` objects to set all permissions that are `True` in one object to `False` in the other; finding the intersection of two `Permissions` objects to obtain the set of permissions that are `True` in both; and inverting a `Permissions` object to set all of its `True` permissions to `False` and vice versa.

The addition, subtraction, union, and intersection operations can operate on a `Permissions` object and a) another `Permissions` object, b) a `flag_value` object, or c) an integer. Reverse methods for each of these operations are also implemented, so both `Permissions.none() + Permissions.view_channel` and `Permissions.view_channel + Permissions.none()` will produce the same result, for example.
@EmmmaTech
Copy link
Contributor

Considered adding this to all flags in flag.py?

Copy link
Member

@plun1331 plun1331 left a comment

Choose a reason for hiding this comment

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

While this is a nice feature, implementing it in the BaseFlags class would probably be a better solution, also allowing you to perform the same operations on everything that inherits from it. (intents, etc)

discord/permissions.py Outdated Show resolved Hide resolved
discord/permissions.py Outdated Show resolved Hide resolved
discord/permissions.py Outdated Show resolved Hide resolved
@EmmmaTech
Copy link
Contributor

While this is a nice feature, implementing it in the BaseFlags class would probably be a better solution, also allowing you to perform the same operations on everything that inherits from it. (intents, etc)

Yeah this is what I was trying to say

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.

That looks like a mess

@celsiusnarhwal celsiusnarhwal marked this pull request as draft July 10, 2022 22:41
@celsiusnarhwal celsiusnarhwal changed the title feat: add additional operations to Permissions feat: add additional operations to BaseFlags Jul 10, 2022
@celsiusnarhwal
Copy link
Contributor Author

While this is a nice feature, implementing it in the BaseFlags class would probably be a better solution, also allowing you to perform the same operations on everything that inherits from it. (intents, etc)

That actually is a better idea; I made this PR with only the Permissions class in mind, so that did not initially occur to me. Nevertheless, I have moved my code changes to the BaseFlags class (with typehints this time) and updated the documentation for its inheritors accordingly.

@celsiusnarhwal celsiusnarhwal marked this pull request as ready for review July 10, 2022 23:15
Copy link
Contributor

@EmmmaTech EmmmaTech left a comment

Choose a reason for hiding this comment

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

Looks good otherwise

discord/flags.py Outdated Show resolved Hide resolved
discord/flags.py Outdated Show resolved Hide resolved
discord/flags.py Outdated Show resolved Hide resolved
discord/flags.py Outdated Show resolved Hide resolved
discord/flags.py Outdated Show resolved Hide resolved
discord/flags.py Outdated Show resolved Hide resolved
discord/flags.py Outdated Show resolved Hide resolved
discord/flags.py Outdated Show resolved Hide resolved
discord/permissions.py Outdated Show resolved Hide resolved
Copy link
Contributor

@EmmmaTech EmmmaTech left a comment

Choose a reason for hiding this comment

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

Sorry for the repetition, but it looks fine otherwise

discord/flags.py Show resolved Hide resolved
discord/flags.py Show resolved Hide resolved
discord/flags.py Show resolved Hide resolved
discord/flags.py Show resolved Hide resolved
discord/flags.py Show resolved Hide resolved
discord/permissions.py Outdated Show resolved Hide resolved
discord/permissions.py Show resolved Hide resolved
discord/permissions.py Show resolved Hide resolved
discord/permissions.py Show resolved Hide resolved
discord/flags.py Show resolved Hide resolved
discord/flags.py Outdated Show resolved Hide resolved
discord/flags.py Outdated Show resolved Hide resolved
discord/flags.py Outdated Show resolved Hide resolved
discord/flags.py Outdated Show resolved Hide resolved
discord/flags.py Outdated Show resolved Hide resolved
discord/flags.py Outdated Show resolved Hide resolved
discord/flags.py Outdated Show resolved Hide resolved
discord/permissions.py Outdated Show resolved Hide resolved
@Middledot Middledot added status: awaiting review Awaiting review from a maintainer feature Implements a feature Merge with squash labels Jul 12, 2022
@Lulalaby Lulalaby enabled auto-merge (squash) July 12, 2022 18:15
@Lulalaby Lulalaby merged commit 7accb95 into Pycord-Development:master Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Implements a feature status: awaiting review Awaiting review from a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants