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: |
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 5 years ago by
- Description modified (diff)
comment:2 Changed 5 years ago by
- Description modified (diff)
comment:3 Changed 5 years ago by
- Description modified (diff)
comment:4 Changed 5 years ago by
- Description modified (diff)
comment:5 Changed 5 years ago by
- Description modified (diff)
comment:6 Changed 5 years ago by
- Branch set to u/jdemeyer/in_richcmp__fall_back_to_reversed_operation_if_coercion_fails
comment:7 Changed 5 years ago by
- Commit set to b3ea04f051d8a06788671e0678c903c8aae186fa
- Status changed from new to needs_review
comment:8 follow-up: ↓ 9 Changed 5 years ago by
- 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
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
- 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
New commits:
Try reversed operation in richcmp if coercion fails