Skip to content

fix: Number rich comparisons return NotImplemented for foreign types#19

Merged
hozblok merged 2 commits into
masterfrom
fix/05-number-eq-notimplemented
May 16, 2026
Merged

fix: Number rich comparisons return NotImplemented for foreign types#19
hozblok merged 2 commits into
masterfrom
fix/05-number-eq-notimplemented

Conversation

@hozblok

@hozblok hozblok commented May 11, 2026

Copy link
Copy Markdown
Owner

Problem. 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 return NotImplemented so the other side has a chance, then falls back to identity for == or TypeError for ordering.

Fix. src/formula/formula.py — guard __eq__, __lt__, __gt__, __le__, __ge__ with isinstance(__value, (Number, str, int, float)). Foreign-type == now falls back to False; foreign-type ordering raises TypeError as Python expects.

Test. New file tests/test_number_eq_notimplemented.py covers None, list, and object() on the right side, plus the preserved equal/unequal cases for Number/str/int/float. All pass; full suite 325/325.

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>
Comment thread tests/test_number_eq_notimplemented.py
@hozblok hozblok marked this pull request as ready for review May 16, 2026 21:21
@hozblok hozblok merged commit c0bba2f into master May 16, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants