fix: test(mocks) resolve Python truthiness and isinstance traps#1016
fix: test(mocks) resolve Python truthiness and isinstance traps#1016aryaMehta26 wants to merge 1 commit into
Conversation
Fixes multiple issues in mocks where empty lists or 0 were incorrectly evaluated as False, or where bool matched isinstance(x, int). This resolves issue rocketride-org#747.
📝 WalkthroughWalkthroughThree test mock modules are updated to fix Python truthiness bugs: chromadb's get/delete/update methods now check ChangesFalsy-value semantics in test mocks
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested labels
Suggested reviewers
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. 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 (2)
nodes/test/mocks/chromadb/__init__.py (1)
153-186:⚠️ Potential issue | 🟠 Major | ⚡ Quick win
limit=0is still handled as “no limit” inget().
if limit and ...at Line 185 still relies on truthiness, so an explicitlimit=0returns rows instead of none. Use an explicitis not Nonecheck (and ideally early-return for zero).Proposed fix
def get( self, ids: List[str] = None, where: Optional[Dict] = None, where_document: Optional[Dict] = None, include: List[str] = None, limit: int = None, offset: int = 0, ) -> Dict[str, Any]: """Get items from the collection.""" if include is None: include = ['metadatas', 'documents'] results = {'ids': [], 'metadatas': [], 'documents': [], 'embeddings': []} + if limit == 0: + return results @@ - if limit and len(results['ids']) >= limit: + if limit is not None and len(results['ids']) >= limit: break🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@nodes/test/mocks/chromadb/__init__.py` around lines 153 - 186, The loop in the mock Chroma get() treats limit=0 as no limit because it uses truthiness (the `if limit and len(results['ids']) >= limit` check); update the logic in the method (the loop that appends to results and currently checks `if limit and ...`) to explicitly handle limit being None vs zero: if limit is 0, early-return or stop adding results immediately (return empty), otherwise use an explicit check like `if limit is not None and len(results['ids']) >= limit` to enforce the limit; adjust the limit handling in the same function where `results['ids']` are appended so zero behaves as “no rows” and None behaves as “no limit.”nodes/test/mocks/weaviate/__init__.py (1)
280-282:⚠️ Potential issue | 🟠 Major | ⚡ Quick winDistance sorting still treats
0.0as falsy and de-prioritizes exact matches.The sort key uses truthiness (
if x.metadata.distance else 1.0), so0.0is converted to1.0. This breaks exact-match-first ordering.Proposed fix
- results.sort(key=lambda x: x.metadata.distance if x.metadata.distance else 1.0) + results.sort(key=lambda x: x.metadata.distance if x.metadata.distance is not None else 1.0)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@nodes/test/mocks/weaviate/__init__.py` around lines 280 - 282, The sort key currently treats 0.0 as falsy in results.sort(key=lambda x: x.metadata.distance if x.metadata.distance else 1.0), causing exact zero-distance matches to be replaced with 1.0; change the conditional to check explicitly for None (e.g., use "if x.metadata.distance is not None" or "if x.metadata.distance is None then use a large value") so that 0.0 remains a valid low distance when sorting by x.metadata.distance.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@nodes/test/mocks/chromadb/__init__.py`:
- Around line 153-186: The loop in the mock Chroma get() treats limit=0 as no
limit because it uses truthiness (the `if limit and len(results['ids']) >=
limit` check); update the logic in the method (the loop that appends to results
and currently checks `if limit and ...`) to explicitly handle limit being None
vs zero: if limit is 0, early-return or stop adding results immediately (return
empty), otherwise use an explicit check like `if limit is not None and
len(results['ids']) >= limit` to enforce the limit; adjust the limit handling in
the same function where `results['ids']` are appended so zero behaves as “no
rows” and None behaves as “no limit.”
In `@nodes/test/mocks/weaviate/__init__.py`:
- Around line 280-282: The sort key currently treats 0.0 as falsy in
results.sort(key=lambda x: x.metadata.distance if x.metadata.distance else 1.0),
causing exact zero-distance matches to be replaced with 1.0; change the
conditional to check explicitly for None (e.g., use "if x.metadata.distance is
not None" or "if x.metadata.distance is None then use a large value") so that
0.0 remains a valid low distance when sorting by x.metadata.distance.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: c2caba8f-e0ed-4f17-89dc-45a5a5f4c18a
📒 Files selected for processing (3)
nodes/test/mocks/chromadb/__init__.pynodes/test/mocks/psycopg2/__init__.pynodes/test/mocks/weaviate/__init__.py
Summary
isinstancetraps in test mocks for ChromaDB, psycopg2, and Weaviate.[]or integers like0were incorrectly evaluating toFalse(e.g.,if ids:vsif ids is not None:).isinstancetrap whereFalsematchedisinstance(x, int)since booleans are a subclass of integers in Python.False in paramsevaluated toTrueifparamscontained a0value.Type
fix
Testing
./builder testpassesChecklist
Linked Issue
Fixes #747
Summary by CodeRabbit