Skip to content

test(file-io): cover the Python syntax gate and drop dead mocks - #6960

Merged
kovtcharov-amd merged 1 commit into
amd:mainfrom
swastiktawate:fix/3955-python-syntax-gate-tests
Nov 7, 2026
Merged

kovtcharov-amd merged 1 commit into
amd:mainfrom
swastiktawate:fix/3955-python-syntax-gate-tests

Conversation

@swastiktawate

Copy link
Copy Markdown
Contributor

Removing the Python syntax check entirely would have kept CI green: the fixtures mocked _validate_python_syntax and _parse_python_code, two helpers the code no longer calls, so no test touched the real gate.

This adds tests for the three tools that enforce the gate, write_python_file, edit_python_file and replace_function. Valid Python writes through, invalid Python is refused with the file left untouched, and validate=False bypasses the gate on write_python_file. It also removes the dead mocks from all three fixtures that set them.

Note: the issue names write_file, edit_file and replace_lines, but the gate now lives in the Python-specific tools. write_file and edit_file write first and only report a syntax problem afterwards, which test_write_reports_syntax.py already covers.

Fixes #3955

Test plan: pytest on the four affected test files, 271 passed, 4 skipped. Temporarily removing the gate makes the new invalid-Python tests fail, so they genuinely exercise it.

The guard that stops broken Python reaching a source file had no test
behind it: the fixtures mocked _validate_python_syntax and
_parse_python_code, two helpers the code no longer calls, so the tests
passed without touching the real gate. Add tests for write_python_file,
edit_python_file and replace_function covering valid Python writing
through, invalid Python being refused with the file left untouched, and
validate=False bypassing the gate on write_python_file. Remove the dead
mocks from all three fixtures that set them.

Fixes amd#3955
@github-actions github-actions Bot added the tests Test changes label Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

The checks that stop broken Python reaching a source file now have real tests behind them. Before this PR, the test setup faked two helper functions the code no longer calls, so deleting those checks would have left CI green. The new tests run the real checks for all three Python editing tools, and I found no correctness problems. The one suggestion is optional: trim a test docstring that tells the story of the fix.

Real-world evidence

N/A: this PR only changes tests, so there is nothing a user can run. The verdict rests on static review. pytest isn't installed on this runner, so I couldn't run the new tests. The author reports 271 passed, 4 skipped, and that the new invalid-Python tests fail when the check is removed.

🔍 Technical details

Verified

  • _validate_python_syntax and _parse_python_code appear nowhere in src/ or hub/, so the removed mocks were dead. All three tools call ast.parse inline instead.
  • The error strings the new tests assert match the source exactly:
    • write_python_file: "Invalid Python syntax" (file_io_tools.py:652)
    • edit_python_file: "Edit would result in invalid Python syntax" (:840)
    • replace_function: "Replacement would result in invalid syntax" (:1742)
  • write_python_file runs the syntax check before the path check and before any write, so test_write_python_file_invalid_is_refused_and_creates_nothing is checking a real ordering. validate=False skips the check, as the bypass test expects.
  • MagicMock is still imported in test_file_write_guardrails.py because other tests use it (:863). In the other two files the import was dropped along with its only use.
  • _tool reuses TestEditingLeavesTheWorkspaceClean._tool, which reads an existing file before changing it, the same way an agent does. Without that read, the edit tests would hit the read-before-edit refusal rather than the syntax check.

Issues

🟢 History narration in a test docstring (tests/unit/test_file_write_guardrails.py, TestPythonSyntaxGate docstring). The sentences starting "Before this fix the fixtures above mocked…" describe the PR, not the test. CLAUDE.md asks that this kind of history live in the commit message, because an inline note goes stale once the code moves.

    """The gate that stops broken Python reaching a source file.

    write_python_file, edit_python_file and replace_function parse the new
    content with ast.parse before writing anything. Valid Python writes
    through, invalid Python is refused with the file left untouched, and
    validate=False bypasses the gate on write_python_file.
    """

Strengths

  • Every invalid-input test checks that the file on disk is unchanged, not just that the tool returned an error. That is the property users rely on.
  • The PR description explains why the tests target the Python-specific tools rather than the write_file/edit_file named in the issue, and points to where those are already covered.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Nov 7, 2026
Merged via the queue into amd:main with commit c11ea99 Nov 7, 2026
26 checks passed
@kovtcharov-amd kovtcharov-amd mentioned this pull request Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The Python syntax gate that stops agents corrupting source files is untested — its tests mock methods the code no longer calls

2 participants