Compute the difference between this range and the `other`. This raises a ``ValueError`` exception if the two ranges are "disjunct", that is neither adjacent nor overlapping.
(self, other: Range[_T])
| 546 | return self.union(other) |
| 547 | |
| 548 | def difference(self, other: Range[_T]) -> Range[_T]: |
| 549 | """Compute the difference between this range and the `other`. |
| 550 | |
| 551 | This raises a ``ValueError`` exception if the two ranges are |
| 552 | "disjunct", that is neither adjacent nor overlapping. |
| 553 | """ |
| 554 | |
| 555 | # Subtracting an empty range is a no-op |
| 556 | if self.empty or other.empty: |
| 557 | return self |
| 558 | |
| 559 | slower = self.lower |
| 560 | slower_b = self.bounds[0] |
| 561 | supper = self.upper |
| 562 | supper_b = self.bounds[1] |
| 563 | olower = other.lower |
| 564 | olower_b = other.bounds[0] |
| 565 | oupper = other.upper |
| 566 | oupper_b = other.bounds[1] |
| 567 | |
| 568 | sl_vs_ol = self._compare_edges(slower, slower_b, olower, olower_b) |
| 569 | su_vs_ou = self._compare_edges(supper, supper_b, oupper, oupper_b) |
| 570 | if sl_vs_ol < 0 and su_vs_ou > 0: |
| 571 | raise ValueError( |
| 572 | "Subtracting a strictly inner range is not implemented" |
| 573 | ) |
| 574 | |
| 575 | sl_vs_ou = self._compare_edges(slower, slower_b, oupper, oupper_b) |
| 576 | su_vs_ol = self._compare_edges(supper, supper_b, olower, olower_b) |
| 577 | |
| 578 | # If the ranges do not overlap, result is simply the first |
| 579 | if sl_vs_ou > 0 or su_vs_ol < 0: |
| 580 | return self |
| 581 | |
| 582 | # If this range is completely contained by the other, result is empty |
| 583 | if sl_vs_ol >= 0 and su_vs_ou <= 0: |
| 584 | return Range(None, None, empty=True) |
| 585 | |
| 586 | # If this range extends to the left of the other and ends in its |
| 587 | # middle |
| 588 | if sl_vs_ol <= 0 and su_vs_ol >= 0 and su_vs_ou <= 0: |
| 589 | rupper_b = ")" if olower_b == "[" else "]" |
| 590 | if ( |
| 591 | slower_b != "[" |
| 592 | and rupper_b != "]" |
| 593 | and self._compare_edges(slower, slower_b, olower, rupper_b) |
| 594 | == 0 |
| 595 | ): |
| 596 | return Range(None, None, empty=True) |
| 597 | else: |
| 598 | return Range( |
| 599 | slower, |
| 600 | olower, |
| 601 | bounds=cast(_BoundsType, slower_b + rupper_b), |
| 602 | ) |
| 603 | |
| 604 | # If this range starts in the middle of the other and extends to its |
| 605 | # right |
no test coverage detected