Skip to content

fix: get_terms() to handle missing local_terms gracefully - #102

Merged
karm1000 merged 2 commits into
version-15from
claude/optimistic-cori-utj29x
Jun 26, 2026
Merged

fix: get_terms() to handle missing local_terms gracefully#102
karm1000 merged 2 commits into
version-15from
claude/optimistic-cori-utj29x

Conversation

@vorasmit

Copy link
Copy Markdown
Member

Summary

Fixed the get_terms() method in the SalesOrder controller to gracefully handle cases where local_terms is None or missing, preventing AttributeError exceptions when the AI response omits this field.

Key Changes

  • Refactored get_terms() method: Replaced walrus operator with explicit null-coalescing logic to safely handle None values for local_terms
  • Added defensive initialization: Initialize local_terms to an empty frappe._dict() when it's None, allowing safe attribute access
  • Improved readability: Simplified the conditional logic for better maintainability

Implementation Details

The original implementation used a walrus operator that would fail with AttributeError if self.data.local_terms was None. The updated approach:

  1. Safely checks if local_terms exists, defaulting to an empty dict if not
  2. Extracts description and incoterms attributes with fallback to empty string
  3. Maintains the same output format while being more robust

Testing

Added comprehensive test coverage:

  • test_get_terms_with_local_terms: Validates normal operation with complete data
  • test_get_terms_with_missing_local_terms: Ensures graceful handling when local_terms is None
  • test_get_terms_with_partial_local_terms: Tests edge case with only description present

https://claude.ai/code/session_01B3hHouqds5m6xkcYetE8KY

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
@greptile-apps

greptile-apps Bot commented Jun 26, 2026

Copy link
Copy Markdown

Confidence Score: 5/5

Safe 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.

Important Files Changed

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

@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

SalesOrder.get_terms() now handles missing local_terms by using a fallback dictionary before reading description and incoterms. The sales order test suite adds coverage for complete local_terms, local_terms = None, and partial local_terms with only a description present.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description check ✅ Passed The description is directly related to the code changes and test coverage in this pull request.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly matches the main change: making get_terms() handle missing local_terms safely.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 34cd9197-7ee5-48b1-9d58-c9a1192e5e1b

📥 Commits

Reviewing files that changed from the base of the PR and between b255a55 and aba5b1e.

📒 Files selected for processing (2)
  • transaction_parser/tests/test_sales_order.py
  • transaction_parser/transaction_parser/controllers/transaction.py

Comment thread transaction_parser/tests/test_sales_order.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
@karm1000 karm1000 changed the title Fix get_terms() to handle missing local_terms gracefully fix: get_terms() to handle missing local_terms gracefully Jun 26, 2026
@karm1000
karm1000 merged commit 86a6f0b into version-15 Jun 26, 2026
4 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.

3 participants