Skip to content

fix: don't crash linting on compile errors without line info (#3358)#5373

Open
xiangnuans wants to merge 1 commit into
Aider-AI:mainfrom
xiangnuans:fix/linter-compile-no-lineno-3358
Open

fix: don't crash linting on compile errors without line info (#3358)#5373
xiangnuans wants to merge 1 commit into
Aider-AI:mainfrom
xiangnuans:fix/linter-compile-no-lineno-3358

Conversation

@xiangnuans

Copy link
Copy Markdown

Problem

lint_python_compile (in aider/linter.py) assumed every compile error carries lineno / end_lineno. Two real cases break that assumption and crash linting for the whole file — reported as "Uncaught TypeError in linter.py" in #3358 (and the dup #3378, "source code string cannot contain null bytes"):

  • A source string containing null bytes raises ValueError with no lineno attribute at all, so getattr(err, "end_lineno", err.lineno) itself raised AttributeError.
  • Some SyntaxErrors carry lineno=None, so err.lineno - 1 raised TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'.

Confirmed on current main (Python 3.13): compile("x=1\0y=2", ...) raises a SyntaxError with lineno=None, and the existing code crashes with exactly that TypeError.

Fix

Read the line info defensively and fall back to an empty line list when it's missing/None, so the compile error is still surfaced to the user instead of taking down linting:

lineno = getattr(err, "lineno", None)
if lineno is None:
    line_numbers = []
else:
    end_lineno = getattr(err, "end_lineno", None) or lineno
    line_numbers = list(range(lineno - 1, end_lineno))

Tests

Added regression tests in tests/basic/test_linter.py:

  • null bytes no longer crash
  • SyntaxError with lineno=None no longer crashes
  • normal syntax error still reports its line (happy-path guard)
  • valid code still returns None

pytest tests/basic/test_linter.py → 10 passed, 1 skipped (Windows-only).

Fixes #3358

`lint_python_compile` assumed every compile error carries `lineno` /
`end_lineno`. Two real cases break that assumption:

- a source string containing null bytes raises `ValueError` with no
  `lineno` attribute at all, so `getattr(err, "end_lineno", err.lineno)`
  itself raised `AttributeError`;
- some `SyntaxError`s carry `lineno=None`, so `err.lineno - 1` raised
  `TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'`.

Both crashed linting for the whole file (reported as "Uncaught TypeError
in linter.py"). Guard the missing/None line info and fall back to an
empty line list so the compile error is still surfaced instead of
crashing. Adds regression tests for null bytes, lineno=None, the normal
syntax-error line-reporting path, and valid code.

Fixes Aider-AI#3358
@CLAassistant

CLAassistant commented Jul 1, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants