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

Add rich comparison methods for Text #2922

Conversation

msempere
Copy link

@msempere msempere commented Apr 12, 2023

Type of changes

  • Bug fix
  • New feature
  • Documentation / docstrings
  • Tests
  • Other

Checklist

  • I've run the latest black with default args on new code.
  • I've updated CHANGELOG.md and CONTRIBUTORS.md where appropriate.
  • I've added tests for new code.
  • I accept that @willmcgugan may be pedantic in the code review.

Description

This pull request adds rich comparison methods to the rich.Text class in order to address the issue #2921. The added methods enable comparison of rich text objects using Python's rich comparison operators like <, <=, ==, !=, >= and >. These methods make it easy for users to compare rich text objects and perform conditional logic on them. With this addition, users can now leverage the full power of the Python programming language with rich text objects.
Closes #2921

rich/text.py Outdated
return NotImplemented
return self.plain > other.plain

def __ne__(self, other: object) -> bool:
Copy link
Collaborator

Choose a reason for hiding this comment

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

We already have an __eq__ which takes in to account the spans (style information).

Having a __ne__ which is not the logical inverse of __eq__ is likely to create to have undesired effects.

Copy link
Author

@msempere msempere Apr 12, 2023

Choose a reason for hiding this comment

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

Mmmm I see. I'll address that to make __ne__ the logical inverse, np at all.

Copy link
Author

Choose a reason for hiding this comment

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

From the docs:

By default, __ne__() delegates to __eq__() and inverts the result unless it is NotImplemented.

So I'm removing the unnecessary __ne__ implementation.

@msempere msempere requested a review from willmcgugan April 12, 2023 14:19
@msempere
Copy link
Author

@willmcgugan is there anything else you would like me to address here?

@msempere
Copy link
Author

@willmcgugan any chance you could review this PR please? thanks

@msempere msempere closed this Apr 25, 2023
@msempere msempere deleted the 2921/rich-comparison-methods-for-Text branch April 25, 2023 04:08
@aaronst
Copy link
Contributor

aaronst commented Sep 21, 2023

@msempere was this deleted and closed for any particular reason? It'd be great to get this merged.

@msempere
Copy link
Author

@msempere was this deleted and closed for any particular reason? It'd be great to get this merged.

@aaronst Since there was no participation or interaction on this pull request, I've opted to close it.

@aaronst
Copy link
Contributor

aaronst commented Sep 22, 2023

@msempere I'd encourage you to re-open and give @willmcgugan and folks more time. They're quite busy but they will eventually review the PR, I think this would be a nice addition.

@msempere msempere restored the 2921/rich-comparison-methods-for-Text branch September 25, 2023 08:10
@msempere msempere reopened this Sep 25, 2023
@msempere
Copy link
Author

@msempere I'd encourage you to re-open and give @willmcgugan and folks more time. They're quite busy but they will eventually review the PR, I think this would be a nice addition.

@aaronst reopening this PR. Thanks for the ping.

@msempere msempere closed this Nov 3, 2023
@msempere msempere deleted the 2921/rich-comparison-methods-for-Text branch November 3, 2023 18:42
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.

Add comparison methods to rich Text
3 participants