fix: Number rich comparisons return NotImplemented for foreign types#19
Merged
Conversation
Before, `Number("1") == None` (and against lists, objects, etc.) raised
ValueError from the inner C++ expression parser, because str(other) was
fed straight into a Solver. Python's data model expects unknown operand
types to be handled by returning NotImplemented so the other side has a
chance, then by falling back to identity (==) or TypeError (ordering).
Guard __eq__, __lt__, __gt__, __le__, __ge__ with an isinstance check
against (Number, str, int, float). Foreign-type equality falls back to
False; foreign-type ordering raises TypeError as expected.
Regression test in tests/test_number_eq_notimplemented.py covers None,
list, object on the right side, plus existing equal/unequal cases.
See ai/improvements_2026-05-09.md item #5.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced May 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem.
Number("1") == None(and against lists, objects, etc.) raisedValueErrorfrom the inner C++ expression parser, becausestr(other)was fed straight into aSolver. Python's data model expects unknown operand types to returnNotImplementedso the other side has a chance, then falls back to identity for==orTypeErrorfor ordering.Fix.
src/formula/formula.py— guard__eq__,__lt__,__gt__,__le__,__ge__withisinstance(__value, (Number, str, int, float)). Foreign-type==now falls back toFalse; foreign-type ordering raisesTypeErroras Python expects.Test. New file
tests/test_number_eq_notimplemented.pycoversNone, list, andobject()on the right side, plus the preserved equal/unequal cases forNumber/str/int/float. All pass; full suite 325/325.