Fix __eq__ and __ne__ for classes implementing them #707
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.
Main issue, which concerns
Vector
,Location
, andShapeList
:Comparison with an object of a different type should not cause an exception - they are simply not equal. Raising an exception in
__eq__
can (and will*) break unrelated code that expects__eq__
to be well-behaved.(* I noticed this bug when cq-editor choked on it while trying to find a name for an object in a dictionary of local variables)
There's a second more minor issue, which concerns the rest of the classes:
When the other type in
__eq__
is not supported, one should technically returnNotImplemented
instead ofFalse
, to allow the other type to take part in the comparison, in case they know about our type.(
__ne__
should also not generally be implemented as just the negation of__eq__
because of this, but the one forPlane
can just be removed - Python will automatically do the right thing based on__eq__
there --ShapeList
, on the other hand, needs to also have its__ne__
overridden, because it directly subclasseslist
, which already implements it)Technically, the
__eq__
forVector
andPlane
is also broken in another way: It's not transitive.They should really eg. have a separate
is_close()
or something for approximate comparison, but this isn't fixed here, since I have no idea how many places it'd break, for one.I also noticed that
Shape.__eq__
callsShape.is_same
instead ofShape.is_equal
which just seems... wrong.