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 hash eq and neq operations to TShape #1375

Conversation

JohannesVerherstraeten
Copy link

@JohannesVerherstraeten JohannesVerherstraeten commented Oct 21, 2024

Fixes the issue where the __hash__, __eq__ and __neq__ operators of TopoDS_TShape are not implemented, giving unexpected results like:

from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox

shape_1 = BRepPrimAPI_MakeBox(10, 20, 30).Shape()

shape_1.TShape() == shape_1.TShape()    # Expected: True, but is False
hash(shape_1.TShape()) == hash(shape_1.TShape())    # Expected: True, but is False

This fix allows properly comparing TopoDS_TShapes and using them in sets/dictionaries.

Summary by Sourcery

Implement hash, equality, and inequality operators for TopoDS_TShape to fix comparison issues and enable usage in sets and dictionaries, and add corresponding tests.

New Features:

  • Implement __hash__, __eq__, and __neq__ operators for TopoDS_TShape to enable proper comparison and usage in sets and dictionaries.

Tests:

  • Add tests for the __hash__, __eq__, and __neq__ operators of TopoDS_TShape to ensure correct functionality.

Copy link

sourcery-ai bot commented Oct 21, 2024

Reviewer's Guide by Sourcery

This pull request implements hash, equality, and inequality operations for the TShape class in the TopoDS module. It addresses an issue where these operations were not properly implemented, leading to unexpected behavior when comparing TShape objects or using them in sets/dictionaries.

Class diagram for updated TopoDS_TShape operations

classDiagram
    class TopoDS_TShape {
        +__hash__() size_t
        +__eq__(self, right) bool
        +__ne__(self, right) bool
    }
    note for TopoDS_TShape "Added hash, eq, and neq operations"
Loading

File-Level Changes

Change Details Files
Implement hash, eq, and ne methods for TopoDS_TShape
  • Add ne_wrapper method to handle inequality comparisons
  • Add eq_wrapper method to handle equality comparisons
  • Implement hash method using opencascade::hash
  • Add Python code to properly handle eq and ne operations
src/SWIG_files/wrapper/TopoDS.i
Add unit tests for new TShape operations
  • Create test_tshape_hash_operator to verify hash functionality
  • Create test_tshape_eq_operator to verify equality comparisons
  • Create test_tshape_neq_operator to verify inequality comparisons
test/test_core_wrapper_features.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on 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.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

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

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @JohannesVerherstraeten - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +1991 to +1995
def __ne__(self, right):
try:
return self.__ne_wrapper__(right)
except:
return True
Copy link

Choose a reason for hiding this comment

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

suggestion (bug_risk): Consider using more specific exception handling in ne and eq methods.

The current implementation catches all exceptions and returns a default value. This might hide unexpected errors. Consider catching specific exceptions that you expect might occur during the comparison.

Suggested change
def __ne__(self, right):
try:
return self.__ne_wrapper__(right)
except:
return True
def __ne__(self, right):
try:
return self.__ne_wrapper__(right)
except (TypeError, AttributeError):
return True

Choose a reason for hiding this comment

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

I copied these method implementations from TopoDS_Shape, but it's true that the try-except is a bit weird.

Unless there is a specific reason to do it this way, I would remove the try-except from both the Shape and TShape implementations.

Copy link

Choose a reason for hiding this comment

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

Thank you for your feedback, we will generate fewer comments like this in the future according to the following instructions:

- Avoid suggesting changes that are not directly related to the functionality or correctness of the code.
- Ensure that suggestions are necessary and improve the code's robustness or readability.
- Do not include suggestions that might introduce unnecessary complexity or are based on personal preference unless they address a clear issue.

Copy link
Owner

Choose a reason for hiding this comment

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

Without the try except statement, something like:

a_topods_shape != "Hi!"

would segfault because the right member is not a TopoDS_Shape. Using ``ìsinstance``` would certainly be better

Choose a reason for hiding this comment

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

Ah yes I see. I'll make a new commit using isinstance

@tpaviot
Copy link
Owner

tpaviot commented Nov 8, 2024

Thank you @JohannesVerherstraeten for this contribution. It is strange the neq_ or eq operators are not handled by default for the TopoDS_TShape class, all c++ classes that implement the != operator should benefit from this addition.

size_t __hash__() {
return opencascade::hash(self);}
};

Copy link
Owner

Choose a reason for hiding this comment

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

The TopoDS_Shape inherits from Standard_Transient, I think the __hash__ method could be moved to the base class, and would also benefit to all other classes derived from Standard_Transient

Choose a reason for hiding this comment

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

Good point, I'll move the __hash__ to Standard_Transient together with the __eq__ and __neq__ methods.

Copy link
Owner

Choose a reason for hiding this comment

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

no need to, I did it there's a build going on

@tpaviot
Copy link
Owner

tpaviot commented Nov 8, 2024

superseded by #1382

@JohannesVerherstraeten
Copy link
Author

Thanks tpaviot, I'll close this PR

@JohannesVerherstraeten JohannesVerherstraeten deleted the fix-tshape-hash branch November 8, 2024 15:23
@tpaviot
Copy link
Owner

tpaviot commented Nov 8, 2024

thank you @JohannesVerherstraeten feel free to contribute swig stuff whenever you want, I'll review changes as fast as I can

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.

2 participants