-
-
Notifications
You must be signed in to change notification settings - Fork 467
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
Conversation
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.
…ethod calls where possible
Considered adding this to all flags in |
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.
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 |
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.
That looks like a mess
…d update documentation accordingly
BaseFlags
That actually is a better idea; I made this PR with only the |
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.
Looks good otherwise
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.
Sorry for the repetition, but it looks fine otherwise
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 twoPermissions
objects together to obtain the combined set of permissions they represent; subtracting twoPermissions
objects to set all permissions that areTrue
in one object toFalse
in the other; finding the intersection of twoPermissions
objects to obtain the set of permissions that areTrue
in both; and inverting aPermissions
object to set all of itsTrue
permissions toFalse
and vice versa. However, this PR enables these operations on all inheritors ofBaseFlags
- the previously-described actions could be similarly applied to objects of theIntents
class, for example.The addition, subtraction, union, and intersection operations can operate on a
BaseFlags
object and a) anotherBaseFlags
object, or b) aflag_value
object. Reverse methods for each of these operations are also implemented, allowing operations betweenBaseFlags
andflag_value
objects to be properly commutative.Information
Checklist
type: ignore
comments were used, a comment is also left explaining why.