Fix TypeError when comparing heap-allocated strings#159
Open
sathish-t wants to merge 1 commit intopydantic:mainfrom
Open
Fix TypeError when comparing heap-allocated strings#159sathish-t wants to merge 1 commit intopydantic:mainfrom
TypeError when comparing heap-allocated strings#159sathish-t wants to merge 1 commit intopydantic:mainfrom
Conversation
`sorted()`, `min()`, `max()`, and comparison operators (`<`, `>`, `<=`, `>=`) failed with `TypeError: '<' not supported between instances of 'str' and 'str'` when operating on heap-allocated strings (e.g. from `str.split()`). `Value::py_cmp()` only handled string ordering for `InternString` vs `InternString`. The `Ref` vs `Ref` arm only compared `LongInt` values, returning `None` for heap `Str` pairs. Added comparison support for: - `Ref(Str)` vs `Ref(Str)` - `InternString` vs `Ref(Str)` and vice versa Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Author
|
Hello I really like your repo! Makes it easy for me to interact with LLMs. Please let me know if you want any changes with the pull request here and I can gladly do it! I know some Rust as well. |
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.
Bug
A human user encountered this bug while using Monty via the JavaScript bindings. This PR was authored with Claude Code to diagnose the root cause and implement the fix.
sorted(),min(),max(), and comparison operators (<,>,<=,>=) fail with:when operating on strings created at runtime (e.g. from
str.split()), even though the same operations work fine on string literals.Reproduction
Equivalent Python reproduction:
Root Cause
Value::py_cmp()invalue.rsonly handled string ordering forInternStringvsInternString(compile-time string literals). String literals are interned at compile time, but strings created at runtime (fromsplit(), concatenation, etc.) are heap-allocated asValue::Ref(HeapData::Str).The
(Ref, Ref)match arm inpy_cmp()only comparedLongIntvalues, returningOk(None)for any other heap type — whichsorted()andmin()/max()interpret as "comparison not supported", triggering theTypeError.Note:
py_eq()already handled all three string representation combinations correctly —py_cmp()was simply missing the same coverage.Fix
Extended
Value::py_cmp()with three new comparison cases:Ref(Str)vsRef(Str)— two heap-allocated strings (added to the existingRefvsRefarm alongsideLongInt)InternStringvsRef(Str)— interned literal compared with heap stringRef(Str)vsInternString— heap string compared with interned literalTests
Added tests to existing test files:
builtin__more_iter_funcs.py—sorted(),min(),max()with heap strings fromsplit()compare__mixed_types.py— direct</>/<=/>=between heap-allocated and interned stringsAll 760 test cases pass, plus the full
make test-ref-count-panicsuite.🤖 Generated with Claude Code