Skip to content

Return NotImplemented from SortedSet set-algebra operators (#219)#251

Open
uttam12331 wants to merge 1 commit into
grantjenks:masterfrom
uttam12331:fix/sortedset-operators-notimplemented-219
Open

Return NotImplemented from SortedSet set-algebra operators (#219)#251
uttam12331 wants to merge 1 commit into
grantjenks:masterfrom
uttam12331:fix/sortedset-operators-notimplemented-219

Conversation

@uttam12331

Copy link
Copy Markdown

Closes #219.

Problem

SortedSet.__and__, __or__, __sub__ and __xor__ were aliased directly to intersection/union/difference/symmetric_difference:

__and__ = intersection
__rand__ = __and__

So an unsupported (non-iterable) right-hand operand raised TypeError from deep inside the underlying set operation instead of returning NotImplemented. Per the data model, a binary operator should return NotImplemented for an operand type it doesn't support, letting Python try the other operand's reflected operator. Because SortedSet raised instead, this fell through:

class Interval:  # not iterable
    def __rand__(self, other):
        return SortedSet(x for x in other if ...)

sorted_set & interval   # TypeError, instead of using Interval.__rand__

The comparison operators (__eq__, __lt__, …) already handle this correctly via __make_cmp, which returns NotImplemented for 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 NotImplemented when the operand is not iterable, and otherwise delegates to the existing method:

def __and__(self, other):
    if isinstance(other, Iterable):
        return self.intersection(other)
    return NotImplemented

__rand__ = __and__

This matches the approach @grantjenks outlined in the issue. Behavior is otherwise unchanged:

  • Iterable operands (the existing, documented behavior — ss & [1, 2], ss - range(3), etc.) work exactly as before.
  • A plain non-iterable operand with no reflected operator still ends up raising TypeError, just via Python's normal NotImplemented fallback rather than from inside the set operation.
  • A non-iterable operand that defines a reflected operator now works (the motivating Interval.__rand__ case).

The named methods (intersection, union, difference, symmetric_difference) are untouched and still accept *iterables.

Tests

Added test_set_op_returns_notimplemented in tests/test_coverage_sortedset.py covering: the four dunders returning NotImplemented for a non-iterable, iterable operands still producing the right results, a reflected-operator type winning, and a plain non-iterable still raising TypeError. It fails on master (raises TypeError) and passes with this change. Full test_coverage_sortedset.py + test_stress_sortedset.py suite passes (53 passed); ruff check and ruff format --check are clean.

`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
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.

Return NotImplemented for SortedSet.__and__

1 participant