fix(test/mocks): correct three Python truthiness/isinstance traps#748
Conversation
isinstance(p, int) is True for bool values in Python (bool subclasses int). The two LIMIT-detection sites in the psycopg2 mock — _handle_semantic_search line 184 and _extract_limit line 308 — were matching boolean params (e.g. False from `isDeleted = %s`) and assigning them as the SQL LIMIT, silently breaking WHERE filtering. Add `not isinstance(p, bool)` guard to both checks. Refs rocketride-org#747
`if limit and ...` treated limit=0 as "no limit" because 0 is falsy in Python, so calling get(limit=0) returned all rows instead of zero. Use `is not None` so that None means unlimited and 0 means zero. Refs rocketride-org#747
The sort key collapsed 0.0 cosine distance (exact match) into the 1.0 fallback because 0.0 is falsy in Python, ranking exact matches at the bottom of nearest-neighbor results instead of the top. Use `is not None` so only missing distances get the fallback. Refs rocketride-org#747
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThree test-mock fixes: ChromaDB now respects ChangesTest Mock Bug Fixes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.Comment |
|
No description provided. |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
nodes/test/mocks/chromadb/__init__.py (1)
186-203:⚠️ Potential issue | 🟠 Major | ⚡ Quick win
limit=0still returns one row.The new check runs after appending, so a zero limit still leaks the first item before the loop breaks. Move the limit guard before the append, or special-case
0up front.🐛 Proposed fix
count = 0 for id, data in items: metadata = data.get("metadata", {}) document = data.get("document", "") @@ if count < offset: count += 1 continue + + if limit is not None and len(results["ids"]) >= limit: + break results["ids"].append(id) if "metadatas" in include: results["metadatas"].append(self._serialize_metadata(metadata)) @@ if "embeddings" in include: results["embeddings"].append(data.get("embedding")) count += 1 - - # Apply limit - if limit is not None and len(results["ids"]) >= limit: - break🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@nodes/test/mocks/chromadb/__init__.py` around lines 186 - 203, The loop appends an item before enforcing the limit, so passing limit=0 still returns one row; update the loop in nodes/test/mocks/chromadb/__init__.py (the block that manipulates count, results, ids, include, metadata, document, data and calls self._serialize_metadata) to guard against the limit before appending (or handle limit==0 at the top of the method) — i.e., check if limit is not None and len(results["ids"]) >= limit (or limit == 0) and break/return prior to pushing id/metadatas/documents/embeddings to results so no item is emitted when limit is zero.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@nodes/test/mocks/chromadb/__init__.py`:
- Around line 186-203: The loop appends an item before enforcing the limit, so
passing limit=0 still returns one row; update the loop in
nodes/test/mocks/chromadb/__init__.py (the block that manipulates count,
results, ids, include, metadata, document, data and calls
self._serialize_metadata) to guard against the limit before appending (or handle
limit==0 at the top of the method) — i.e., check if limit is not None and
len(results["ids"]) >= limit (or limit == 0) and break/return prior to pushing
id/metadatas/documents/embeddings to results so no item is emitted when limit is
zero.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: c3ec8356-5940-4c92-9656-770c6b16cd42
📒 Files selected for processing (3)
nodes/test/mocks/chromadb/__init__.pynodes/test/mocks/psycopg2/__init__.pynodes/test/mocks/weaviate/__init__.py
The previous fix correctly distinguished None from 0 for the limit parameter, but the break check still ran AFTER appending, so limit=0 returned one item instead of zero. Caught by CodeRabbit on rocketride-org#748. Move the check ahead of the append so the loop short-circuits before emitting the first row when the cap is already reached. Verified the three cases trace through correctly: limit=0 returns 0 items, limit=2 returns 2 items, limit=None returns all items. Refs rocketride-org#747
|
Good catch — the v1 fix correctly distinguished |
asclearuc
left a comment
There was a problem hiding this comment.
Thanks for tackling these, @TrishaReddygari — three real bugs in one tightly-scoped PR, with clear per-commit messages and a good follow-up after CodeRabbit caught the chromadb append-ordering subtlety. Nice cleanup.
Value Assessment
These are mock-only bugs, but mock-only bugs are the worst kind: tests pass against the mock, real behavior diverges in production, and the gap is invisible until something breaks live. Catching the entire bool ⊂ int / 0 is falsy family across all three vector-store mocks at once is the right call — the maintenance cost of three one-liners is trivial, and bundling them avoids three round-trip reviews of the same class of fix.
Summary
Fixes three pre-existing Python truthiness /
isinstancebugs in test mocks, identified by CodeRabbit during review of #744. All three are the same class of bug, applied as one commit per file:nodes/test/mocks/psycopg2/__init__.py— booleans were matchingisinstance(p, int)and getting assigned as a SQLLIMIT. Fixed by addingnot isinstance(p, bool)at both call sites (_handle_semantic_searchline 184 and_extract_limitline 308).nodes/test/mocks/chromadb/__init__.py—if limit and ...treatedlimit=0as "no limit," returning all rows instead of zero. Fixed withis not None.nodes/test/mocks/weaviate/__init__.py— sort key collapsed0.0cosine distance (exact match) into the1.0fallback, ranking exact matches last. Fixed withis not None.Why this matters
Mock-only bugs are sneaky: tests pass against the mock, real behavior diverges in production, and the gap is hard to spot. Catching the truthiness trap class across all three mocks at once seemed worth doing as one PR.
Testing
Each fix is a one-liner with no behavior change in the cases that previously worked. Existing test suite should remain green; the bugs only manifested in edge cases that no current test exercises.
Breaking changes
None.
Fixes #747
Summary by CodeRabbit