fix: get_terms() to handle missing local_terms gracefully - #102
Conversation
The AI response may omit `local_terms` or return it as null, in which case `self.data.local_terms` resolves to None and accessing `.description`/`.incoterms` on it raises AttributeError during Sales Order generation. Fall back to an empty frappe._dict so the attribute access resolves to None gracefully and an empty terms string is returned. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B3hHouqds5m6xkcYetE8KY
Confidence Score: 5/5Safe to merge — the change is a narrow, well-tested guard around a single attribute access with no side effects. The production change is a two-line fix that adds a null-safe fallback before attribute access. The existing test suite is extended with cases that exercise every realistic local_terms state, and all assertions match the expected output. No other code paths are touched. No files require special attention.
|
| Filename | Overview |
|---|---|
| transaction_parser/transaction_parser/controllers/transaction.py | Replaces walrus-operator attribute access on potentially-None local_terms with a safe or frappe._dict() fallback; fix is correct and minimal. |
| transaction_parser/tests/test_sales_order.py | Adds four new test cases covering normal, None, partial, and absent local_terms scenarios; all assertions are correct for the updated logic. |
Reviews (2): Last reviewed commit: "test: add omitted local_terms key case f..." | Re-trigger Greptile
📝 WalkthroughWalkthrough
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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 |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 34cd9197-7ee5-48b1-9d58-c9a1192e5e1b
📒 Files selected for processing (2)
transaction_parser/tests/test_sales_order.pytransaction_parser/transaction_parser/controllers/transaction.py
Covers the truly-absent key path (not just an explicit None value), confirming get_terms relies on frappe._dict returning None for a missing attribute. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B3hHouqds5m6xkcYetE8KY
Summary
Fixed the
get_terms()method in the SalesOrder controller to gracefully handle cases wherelocal_termsisNoneor missing, preventingAttributeErrorexceptions when the AI response omits this field.Key Changes
get_terms()method: Replaced walrus operator with explicit null-coalescing logic to safely handleNonevalues forlocal_termslocal_termsto an emptyfrappe._dict()when it'sNone, allowing safe attribute accessImplementation Details
The original implementation used a walrus operator that would fail with
AttributeErrorifself.data.local_termswasNone. The updated approach:local_termsexists, defaulting to an empty dict if notTesting
Added comprehensive test coverage:
test_get_terms_with_local_terms: Validates normal operation with complete datatest_get_terms_with_missing_local_terms: Ensures graceful handling whenlocal_termsisNonetest_get_terms_with_partial_local_terms: Tests edge case with only description presenthttps://claude.ai/code/session_01B3hHouqds5m6xkcYetE8KY