Refactor division methods in Unit class and improve reverse functionality#38
Merged
chaoming0625 merged 1 commit intomainfrom Jun 2, 2025
Merged
Refactor division methods in Unit class and improve reverse functionality#38chaoming0625 merged 1 commit intomainfrom
chaoming0625 merged 1 commit intomainfrom
Conversation
Contributor
Reviewer's GuideThis PR refactors the Unit class’s division logic by extracting inversion into a new reverse() method, simplifies rdiv to use reverse(), and updates/removes tests to reference reverse() instead of computing 1/u and drop outdated assertions. Sequence Diagram for
|
| Change | Details | Files |
|---|---|---|
| Extracted unit inversion logic into a new reverse() method and simplified rdiv |
|
saiunit/_base.py |
| Adapted and pruned tests to use reverse() and removed obsolete division cases |
|
saiunit/_base_test.py |
Tips and commands
Interacting with Sourcery
- Trigger a new review: Comment
@sourcery-ai reviewon the pull request. - Continue discussions: Reply directly to Sourcery's review comments.
- Generate a GitHub issue from a review comment: Ask Sourcery to create an
issue from a review comment by replying to it. You can also reply to a
review comment with@sourcery-ai issueto create an issue from it. - Generate a pull request title: Write
@sourcery-aianywhere in the pull
request title to generate a title at any time. You can also comment
@sourcery-ai titleon the pull request to (re-)generate the title at any time. - Generate a pull request summary: Write
@sourcery-ai summaryanywhere in
the pull request body to generate a PR summary at any time exactly where you
want it. You can also comment@sourcery-ai summaryon the pull request to
(re-)generate the summary at any time. - Generate reviewer's guide: Comment
@sourcery-ai guideon the pull
request to (re-)generate the reviewer's guide at any time. - Resolve all Sourcery comments: Comment
@sourcery-ai resolveon the
pull request to resolve all Sourcery comments. Useful if you've already
addressed all the comments and don't want to see them anymore. - Dismiss all Sourcery reviews: Comment
@sourcery-ai dismisson the pull
request to dismiss all existing Sourcery reviews. Especially useful if you
want to start fresh with a new review - don't forget to comment
@sourcery-ai reviewto trigger a new review!
Customizing Your Experience
Access your dashboard to:
- Enable or disable review features such as the Sourcery-generated pull request
summary, the reviewer's guide, and others. - Change the review language.
- Add, remove or edit custom review instructions.
- Adjust other review settings.
Getting Help
- Contact our support team for questions or feedback.
- Visit our documentation for detailed guides and information.
- Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.
Contributor
There was a problem hiding this comment.
Hey @chaoming0625 - I've reviewed your changes - here's some feedback:
- The new reverse() method largely duplicates the inversion logic from rdiv; consider extracting a single helper to keep the code DRY.
- The commented-out tests for
1/meterand the large removed block intest_str_reprshould be either deleted or accompanied by a clear comment explaining why they’re no longer valid. - If reverse() is intended as a public API you might choose a more descriptive name (e.g. invert()), or otherwise mark it as a private helper to clarify its usage.
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| @@ -556,21 +556,21 @@ def test_multiplication_division(self): | |||
| for q in quantities: | |||
| # Scalars and array scalars | |||
| assert_quantity(q / 3, q.mantissa / 3, u) | |||
Contributor
There was a problem hiding this comment.
issue (code-quality): Extract code out into method (extract-method)
ChromatinRemodeling
pushed a commit
to ChromatinRemodeling/saiunit
that referenced
this pull request
Jun 12, 2025
chaoming0625
added a commit
that referenced
this pull request
Jun 12, 2025
* Move info from setup.py to pyproject.toml * Move more info from setup.py to pyproject.toml * Revert numpy version change * Fix typo * Refactor debug_info import and update version check in wrap_init (#36) * Refactor debug_info import and update version check in wrap_init * Bump version to 0.0.14 * Refactor division methods in Unit class and improve reverse functionality (#38) * Bump version to 0.0.15 and update wheel upload paths in build_wheel.sh * Clean up pyproject.toml[.template] and setup.py[.template] * Fix typos * Update build_wheel.sh --------- Co-authored-by: Tairan Li <trli@DESKTOP-F9E5SRV.localdomain> Co-authored-by: Chaoming Wang <adaduo@outlook.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Now,
1/unitis aQuantity, rather aUnit.Summary by Sourcery
Extract reciprocal logic into a new
Unit.reverse()method, refactor the reverse-division operator to use it, and update tests accordingly.New Features:
Unit.reverse()method to return the reciprocal unit.Enhancements:
Unit.__rdiv__to delegate toUnit.__div__orQuantityand usereverse()instead of inline reciprocal logic.1/selfunit construction withself.reverse().Tests:
reverse()for asserting reciprocal units instead of1 / unitor1 / quantity.isinstance(1 / unit, Unit).