Skip to content

fix(colang): raise ValueError for unbalanced closing bracket in split_args#2145

Open
vidigoat wants to merge 1 commit into
NVIDIA-NeMo:developfrom
vidigoat:fix/split-args-unbalanced-closing-bracket
Open

fix(colang): raise ValueError for unbalanced closing bracket in split_args#2145
vidigoat wants to merge 1 commit into
NVIDIA-NeMo:developfrom
vidigoat:fix/split-args-unbalanced-closing-bracket

Conversation

@vidigoat

@vidigoat vidigoat commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Fixes #2144

Description

split_args reads closing_char[stack[-1]] for every closing character, but when a ), ], or } appears with an empty bracket stack, stack[-1] raises an uncaught IndexError. (Quote characters are unaffected — they are treated as openers on an empty stack.) This adds an explicit empty-stack guard that raises the same ValueError the function already uses to report malformed input, so callers get a consistent, catchable error instead of an IndexError.

Steps to reproduce (before this fix)

from nemoguardrails.colang.v1_0.lang.utils import split_args
split_args("x=1, y)")   # IndexError: list index out of range

Also affected: ")", "a)b", "foo]", "}".

Test plan

  • Added a parametrized regression test in tests/test_parser_utils.py covering ")", "a)b", "foo]", "x=1, y)", "}"; each now asserts ValueError.
  • Verified the new test fails before the fix (IndexError) and passes after; existing split_args tests and the mismatched-bracket ValueError path are unchanged.

AI disclosure

This change was drafted with the assistance of an AI coding assistant; I reproduced the bug and verified the fix and tests before submitting.

Summary by CodeRabbit

  • Bug Fixes
    • Improved argument parsing to handle unexpected closing brackets more gracefully.
    • Replaced an internal crash case with a clear error message when bracket or quote characters are unbalanced.
    • Added coverage for several malformed input examples to prevent regressions.

@github-actions github-actions Bot added needs: rebase status: needs triage New issues that have not yet been reviewed or categorized. size: S labels Jul 8, 2026
@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 4c73ce7f-30c3-4477-aead-1dbedf415640

📥 Commits

Reviewing files that changed from the base of the PR and between 89df3de and d78589e.

📒 Files selected for processing (2)
  • nemoguardrails/colang/v1_0/lang/utils.py
  • tests/test_parser_utils.py

📝 Walkthrough

Walkthrough

The split_args function in nemoguardrails now raises ValueError when encountering a closing quote or bracket character with an empty stack, replacing the previous unhandled IndexError. A parametrized test was added to verify this behavior across multiple unbalanced input cases.

Changes

split_args bracket validation fix

Layer / File(s) Summary
Empty-stack validation and regression test
nemoguardrails/colang/v1_0/lang/utils.py, tests/test_parser_utils.py
split_args now raises ValueError when a closing character appears with no matching opener (empty stack) instead of raising IndexError; a new parametrized test validates ValueError is raised for inputs like ")", "a)b", "foo]", "x=1, y)", and "}".

Estimated code review effort: 1 (Trivial) | ~5 minutes

🚥 Pre-merge checks | ✅ 6
✅ Passed checks (6 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly states the main fix: split_args now raises ValueError for unbalanced closing brackets.
Linked Issues check ✅ Passed The code and regression test match issue #2144 by converting stray closing brackets from IndexError to ValueError.
Out of Scope Changes check ✅ Passed The changes stay focused on split_args and its regression test, with no unrelated edits.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Test Results For Major Changes ✅ Passed PASS: This is a small bug fix, and the PR description includes a Test plan plus before/after verification for the new regression test.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
⚔️ Resolve merge conflicts
  • Resolve merge conflict in branch fix/split-args-unbalanced-closing-bracket

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

…_args

split_args read closing_char[stack[-1]] for every closing character, but when
a ) ] or } appeared with an empty bracket stack, stack[-1] raised an uncaught
IndexError. Quote characters are unaffected (they are treated as openers on an
empty stack). This adds an explicit empty-stack guard that raises the same
ValueError the function already uses to report malformed input, so callers get
a consistent, catchable error instead of an IndexError.

Signed-off-by: Vidit Patankar <vidit.patankar16@gmail.com>
@vidigoat vidigoat force-pushed the fix/split-args-unbalanced-closing-bracket branch from d78589e to 0d5c246 Compare July 8, 2026 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: XS status: needs triage New issues that have not yet been reviewed or categorized.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

split_args raises IndexError on a stray closing bracket instead of ValueError

1 participant