BUG-HUNT: [consistency] Incorrect dedent for try...except...finally in robot.indentation_library #2845

Closed
opened 2026-04-04 20:49:51 +00:00 by freemo · 4 comments
Owner

Metadata

  • Branch: fix/bug-hunt-indentation-library-try-except
  • Commit Message: fix(robot): correct dedent logic for try...except...finally in indentation_library
  • Milestone: v3.2.0
  • Parent Epic: #2810

Background and Context

The fix_python_indentation function in robot/indentation_library.py contains flawed logic for handling try...except...finally blocks. The dedentation logic for except and finally keywords is overly simplistic and does not correctly manage the indentation level, especially when multiple except blocks are present. This results in syntactically incorrect Python code being generated, which causes Robot Framework test cases to fail.

Given that try...except is one of the most common Python constructs, this bug has a high likelihood of being triggered in practice and a high impact when it is.

Current Behavior

The fix_python_indentation function generates Python code with incorrect indentation for try...except...finally blocks. Specifically:

  • Lines 46–55: The dedent level calculation for except and finally is overly simplistic and does not correctly account for the nested block structure.
  • Lines 64–76: The block_stack and indent_level management logic is flawed, failing to correctly handle multiple except clauses or nested try blocks.

The result is syntactically invalid Python code that causes Robot Framework test execution to fail.

Expected Behavior

The fix_python_indentation function should correctly reconstruct indentation for try...except...finally blocks, producing syntactically valid Python code in all cases, including:

  • Single except clause
  • Multiple except clauses
  • except with finally
  • Nested try blocks

Acceptance Criteria

  • fix_python_indentation correctly handles a try block with a single except clause
  • fix_python_indentation correctly handles a try block with multiple except clauses
  • fix_python_indentation correctly handles a try...except...finally block
  • fix_python_indentation correctly handles nested try blocks
  • All existing Robot Framework tests continue to pass
  • No regressions introduced in other indentation handling logic

Supporting Information

  • File: robot/indentation_library.py
  • Function: fix_python_indentation
  • Lines: 46–55 (dedent level calculation), 64–76 (block_stack / indent_level management)
  • Suggested Fix: Rewrite the stack-based logic using the following approach:
    1. When a try is encountered, push it onto the stack and increment the indent level.
    2. When an except or finally is encountered, pop from the stack until a try is found; set the indentation level to match the try block.
    3. After an except or finally line, increment the indent level again for the body of the block.
    4. Close the block after the body of the except/finally block.
    • Alternatively, consider using the ast module for a more robust, parser-based approach.

Subtasks

  • Reproduce the bug with a minimal Robot Framework test case using try...except...finally
  • Rewrite the try...except...finally handling logic in fix_python_indentation (lines 46–55 and 64–76)
  • Add unit tests covering single except, multiple except, except...finally, and nested try scenarios
  • Run nox (all default sessions) and fix any errors
  • Verify coverage >= 97% via nox -s coverage_report

Definition of Done

  • All subtasks above are checked off
  • fix_python_indentation produces syntactically valid Python for all try...except...finally variants
  • All Robot Framework integration tests pass
  • Commit created with message fix(robot): correct dedent logic for try...except...finally in indentation_library and pushed to branch fix/bug-hunt-indentation-library-try-except
  • Pull Request submitted, reviewed, and merged
  • All nox stages pass
  • Coverage >= 97%

Automated by CleverAgents Bot
Supervisor: Unknown | Agent: ca-new-issue-creator

## Metadata - **Branch**: `fix/bug-hunt-indentation-library-try-except` - **Commit Message**: `fix(robot): correct dedent logic for try...except...finally in indentation_library` - **Milestone**: v3.2.0 - **Parent Epic**: #2810 ## Background and Context The `fix_python_indentation` function in `robot/indentation_library.py` contains flawed logic for handling `try...except...finally` blocks. The dedentation logic for `except` and `finally` keywords is overly simplistic and does not correctly manage the indentation level, especially when multiple `except` blocks are present. This results in syntactically incorrect Python code being generated, which causes Robot Framework test cases to fail. Given that `try...except` is one of the most common Python constructs, this bug has a high likelihood of being triggered in practice and a high impact when it is. ## Current Behavior The `fix_python_indentation` function generates Python code with incorrect indentation for `try...except...finally` blocks. Specifically: - Lines 46–55: The dedent level calculation for `except` and `finally` is overly simplistic and does not correctly account for the nested block structure. - Lines 64–76: The `block_stack` and `indent_level` management logic is flawed, failing to correctly handle multiple `except` clauses or nested `try` blocks. The result is syntactically invalid Python code that causes Robot Framework test execution to fail. ## Expected Behavior The `fix_python_indentation` function should correctly reconstruct indentation for `try...except...finally` blocks, producing syntactically valid Python code in all cases, including: - Single `except` clause - Multiple `except` clauses - `except` with `finally` - Nested `try` blocks ## Acceptance Criteria - [ ] `fix_python_indentation` correctly handles a `try` block with a single `except` clause - [ ] `fix_python_indentation` correctly handles a `try` block with multiple `except` clauses - [ ] `fix_python_indentation` correctly handles a `try...except...finally` block - [ ] `fix_python_indentation` correctly handles nested `try` blocks - [ ] All existing Robot Framework tests continue to pass - [ ] No regressions introduced in other indentation handling logic ## Supporting Information - **File**: `robot/indentation_library.py` - **Function**: `fix_python_indentation` - **Lines**: 46–55 (dedent level calculation), 64–76 (`block_stack` / `indent_level` management) - **Suggested Fix**: Rewrite the stack-based logic using the following approach: 1. When a `try` is encountered, push it onto the stack and increment the indent level. 2. When an `except` or `finally` is encountered, pop from the stack until a `try` is found; set the indentation level to match the `try` block. 3. After an `except` or `finally` line, increment the indent level again for the body of the block. 4. Close the block after the body of the `except`/`finally` block. - Alternatively, consider using the `ast` module for a more robust, parser-based approach. ## Subtasks - [ ] Reproduce the bug with a minimal Robot Framework test case using `try...except...finally` - [ ] Rewrite the `try...except...finally` handling logic in `fix_python_indentation` (lines 46–55 and 64–76) - [ ] Add unit tests covering single `except`, multiple `except`, `except...finally`, and nested `try` scenarios - [ ] Run `nox` (all default sessions) and fix any errors - [ ] Verify coverage >= 97% via `nox -s coverage_report` ## Definition of Done - [ ] All subtasks above are checked off - [ ] `fix_python_indentation` produces syntactically valid Python for all `try...except...finally` variants - [ ] All Robot Framework integration tests pass - [ ] Commit created with message `fix(robot): correct dedent logic for try...except...finally in indentation_library` and pushed to branch `fix/bug-hunt-indentation-library-try-except` - [ ] Pull Request submitted, reviewed, and merged - All nox stages pass - Coverage >= 97% --- **Automated by CleverAgents Bot** Supervisor: Unknown | Agent: ca-new-issue-creator
freemo added this to the v3.2.0 milestone 2026-04-04 20:50:24 +00:00
Author
Owner

Starting implementation on branch fix/bug-hunt-indentation-library-try-except.

Bug Analysis:

  • Bug 1 (Critical): When finally: is encountered, the code pops the try from the block_stack. This means subsequent except: clauses (for an outer try) cannot find their matching try in the stack, resulting in incorrect indentation and syntactically invalid Python.
  • Bug 2: After an except block body, subsequent code at the same level is incorrectly indented inside the except block (the block_stack is not properly cleaned up after try...except blocks complete).

Approach: TDD — writing failing tests first, then fixing the logic.


Automated by CleverAgents Bot
Supervisor: Implementation | Agent: ca-issue-worker

Starting implementation on branch `fix/bug-hunt-indentation-library-try-except`. **Bug Analysis:** - **Bug 1 (Critical)**: When `finally:` is encountered, the code pops the `try` from the `block_stack`. This means subsequent `except:` clauses (for an outer `try`) cannot find their matching `try` in the stack, resulting in incorrect indentation and syntactically invalid Python. - **Bug 2**: After an `except` block body, subsequent code at the same level is incorrectly indented inside the except block (the `block_stack` is not properly cleaned up after `try...except` blocks complete). **Approach**: TDD — writing failing tests first, then fixing the logic. --- **Automated by CleverAgents Bot** Supervisor: Implementation | Agent: ca-issue-worker
Author
Owner

All subtasks complete. Quality gates passed. Creating PR.

Implementation Summary:

Fixed two bugs in fix_python_indentation in robot/indentation_library.py:

  1. Bug 1 (Critical): finally: was incorrectly popping the try from the stack, preventing outer except clauses from finding their matching try.

  2. Bug 2: The dedent calculation didn't correctly handle nested try blocks where the inner try's except/finally had already been processed.

Fix: Replaced the flat string block_stack with a tuple stack of (keyword, base_indent, has_seen_except_finally). The finally: handler now pops only the innermost try (leaving outer tries intact), and the except: handler correctly identifies whether it belongs to the inner or outer try based on whether the inner try has already seen an except/finally clause.

Tests: 6 BDD scenarios added covering all acceptance criteria. All pass.

PR #3202 created on branch fix/bug-hunt-indentation-library-try-except. PR review and merge handled by continuous review stream.


Automated by CleverAgents Bot
Supervisor: Implementation | Agent: ca-issue-worker

All subtasks complete. Quality gates passed. Creating PR. **Implementation Summary:** Fixed two bugs in `fix_python_indentation` in `robot/indentation_library.py`: 1. **Bug 1 (Critical)**: `finally:` was incorrectly popping the `try` from the stack, preventing outer `except` clauses from finding their matching `try`. 2. **Bug 2**: The dedent calculation didn't correctly handle nested try blocks where the inner try's except/finally had already been processed. **Fix**: Replaced the flat string `block_stack` with a tuple stack of `(keyword, base_indent, has_seen_except_finally)`. The `finally:` handler now pops only the innermost `try` (leaving outer tries intact), and the `except:` handler correctly identifies whether it belongs to the inner or outer `try` based on whether the inner try has already seen an except/finally clause. **Tests**: 6 BDD scenarios added covering all acceptance criteria. All pass. PR #3202 created on branch `fix/bug-hunt-indentation-library-try-except`. PR review and merge handled by continuous review stream. --- **Automated by CleverAgents Bot** Supervisor: Implementation | Agent: ca-issue-worker
Author
Owner

PR #3202 reviewed, approved, and merged.

The fix correctly handles all try...except...finally indentation configurations in robot/indentation_library.py, including the critical regression case (nested try with inner finally + outer except). All 6 BDD scenarios pass and all CI checks succeeded.


Automated by CleverAgents Bot
Supervisor: PR Review | Agent: ca-pr-self-reviewer

PR #3202 reviewed, approved, and merged. The fix correctly handles all `try...except...finally` indentation configurations in `robot/indentation_library.py`, including the critical regression case (nested try with inner finally + outer except). All 6 BDD scenarios pass and all CI checks succeeded. --- **Automated by CleverAgents Bot** Supervisor: PR Review | Agent: ca-pr-self-reviewer
Author
Owner

Issue transitioned to State/Completed and closed. PR #3202 has been merged to master.


Automated by CleverAgents Bot
Supervisor: PR Review | Agent: ca-pr-self-reviewer

Issue transitioned to `State/Completed` and closed. PR #3202 has been merged to master. --- **Automated by CleverAgents Bot** Supervisor: PR Review | Agent: ca-pr-self-reviewer
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
cleveragents/cleveragents-core#2845
No description provided.