Return NotImplemented from SortedSet set-algebra operators (#219)#251
Open
uttam12331 wants to merge 1 commit into
Open
Return NotImplemented from SortedSet set-algebra operators (#219)#251uttam12331 wants to merge 1 commit into
uttam12331 wants to merge 1 commit into
Conversation
`SortedSet.__and__`, `__or__`, `__sub__` and `__xor__` were aliased directly to `intersection`/`union`/`difference`/`symmetric_difference`, so an unsupported (non-iterable) right-hand operand raised `TypeError` from deep inside the set operation instead of returning `NotImplemented`. That prevented Python from falling back to the other operand's reflected operator (e.g. an `Interval` type defining `__rand__`). Give each operator an explicit implementation that returns `NotImplemented` when the operand is not iterable and otherwise delegates to the existing method, mirroring how the comparison operators already use `__make_cmp`. Iterable operands (the existing, documented behavior) are unchanged, and a plain non-iterable operand with no reflected operator still ends up raising `TypeError` as before. Closes grantjenks#219
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.
Closes #219.
Problem
SortedSet.__and__,__or__,__sub__and__xor__were aliased directly tointersection/union/difference/symmetric_difference:So an unsupported (non-iterable) right-hand operand raised
TypeErrorfrom deep inside the underlying set operation instead of returningNotImplemented. Per the data model, a binary operator should returnNotImplementedfor an operand type it doesn't support, letting Python try the other operand's reflected operator. BecauseSortedSetraised instead, this fell through:The comparison operators (
__eq__,__lt__, …) already handle this correctly via__make_cmp, which returnsNotImplementedfor unsupported operands — the set-algebra operators just hadn't been given the same treatment.Change
Give each set-algebra operator an explicit implementation that returns
NotImplementedwhen the operand is not iterable, and otherwise delegates to the existing method:This matches the approach @grantjenks outlined in the issue. Behavior is otherwise unchanged:
ss & [1, 2],ss - range(3), etc.) work exactly as before.TypeError, just via Python's normalNotImplementedfallback rather than from inside the set operation.Interval.__rand__case).The named methods (
intersection,union,difference,symmetric_difference) are untouched and still accept*iterables.Tests
Added
test_set_op_returns_notimplementedintests/test_coverage_sortedset.pycovering: the four dunders returningNotImplementedfor a non-iterable, iterable operands still producing the right results, a reflected-operator type winning, and a plain non-iterable still raisingTypeError. It fails onmaster(raisesTypeError) and passes with this change. Fulltest_coverage_sortedset.py+test_stress_sortedset.pysuite passes (53 passed);ruff checkandruff format --checkare clean.