Opened 5 years ago

Closed 5 years ago

#21163 closed enhancement (fixed)

In richcmp, fall back to reversed operation if coercion fails

Reported by: jdemeyer Owned by:
Priority: major Milestone: sage-7.3
Component: coercion Keywords:
Cc: Merged in:
Authors: Jeroen Demeyer Reviewers: Frédéric Chapoton
Report Upstream: N/A Work issues:
Branch: b3ea04f (Commits, GitHub, GitLab) Commit: b3ea04f051d8a06788671e0678c903c8aae186fa
Dependencies: Stopgaps:

Status badges

Description (last modified by jdemeyer)

Change the implementation of x.__richcmp__(y, op) to try y.__richcmp__(x, revop) if y not a Sage Element and coercion fails. In Python terms: if x.__ge__(y) fails, try y.__le__(x). This is precisely what Python does when a comparison returns NotImplemented.

If this reversed operation is not implemented (either the type does not define comparisons or the comparison returns NotImplemented), go on as usual. Because of this, we cannot just return NotImplemented in the coercion model, we need to implement the reversing ourselves.

In #20767, the analogous thing was done for arithmetic.

Change History (10)

comment:1 Changed 5 years ago by jdemeyer

  • Description modified (diff)

comment:2 Changed 5 years ago by jdemeyer

  • Description modified (diff)

comment:3 Changed 5 years ago by jdemeyer

  • Description modified (diff)

comment:4 Changed 5 years ago by jdemeyer

  • Description modified (diff)

comment:5 Changed 5 years ago by jdemeyer

  • Description modified (diff)

comment:6 Changed 5 years ago by jdemeyer

  • Branch set to u/jdemeyer/in_richcmp__fall_back_to_reversed_operation_if_coercion_fails

comment:7 Changed 5 years ago by jdemeyer

  • Commit set to b3ea04f051d8a06788671e0678c903c8aae186fa
  • Status changed from new to needs_review

New commits:

b3ea04fTry reversed operation in richcmp if coercion fails

comment:8 follow-up: Changed 5 years ago by chapoton

  • Reviewers set to Frédéric Chapoton
  • Status changed from needs_review to positive_review

ok, looks good to me. Nice formula for the revop involution.

comment:9 in reply to: ↑ 8 Changed 5 years ago by jdemeyer

Replying to chapoton:

ok, looks good to me. Nice formula for the revop involution.

Thanks. That was a nice exercise :-)

comment:10 Changed 5 years ago by vbraun

  • Branch changed from u/jdemeyer/in_richcmp__fall_back_to_reversed_operation_if_coercion_fails to b3ea04f051d8a06788671e0678c903c8aae186fa
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.