Opened 6 years ago

Closed 6 years ago

## #21163 closed enhancement (fixed)

# In richcmp, fall back to reversed operation if coercion fails

Reported by: | Jeroen Demeyer | 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: |

### Description (last modified by )

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 6 years ago by

Description: | modified (diff) |
---|

### comment:2 Changed 6 years ago by

Description: | modified (diff) |
---|

### comment:3 Changed 6 years ago by

Description: | modified (diff) |
---|

### comment:4 Changed 6 years ago by

Description: | modified (diff) |
---|

### comment:5 Changed 6 years ago by

Description: | modified (diff) |
---|

### comment:6 Changed 6 years ago by

Branch: | → u/jdemeyer/in_richcmp__fall_back_to_reversed_operation_if_coercion_fails |
---|

### comment:7 Changed 6 years ago by

Commit: | → b3ea04f051d8a06788671e0678c903c8aae186fa |
---|---|

Status: | new → needs_review |

### comment:8 follow-up: 9 Changed 6 years ago by

Reviewers: | → Frédéric Chapoton |
---|---|

Status: | needs_review → positive_review |

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

### comment:9 Changed 6 years ago by

Replying to chapoton:

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

Thanks. That was a nice exercise :-)

### comment:10 Changed 6 years ago by

Branch: | u/jdemeyer/in_richcmp__fall_back_to_reversed_operation_if_coercion_fails → b3ea04f051d8a06788671e0678c903c8aae186fa |
---|---|

Resolution: | → fixed |

Status: | positive_review → closed |

**Note:**See TracTickets for help on using tickets.

New commits:

`Try reversed operation in richcmp if coercion fails`