Skip to content

Fix --pretty crash on EOF syntax errors - #22430

Merged
hauntsaninja merged 2 commits into
python:masterfrom
lianxiaozhi:fix/pretty-eof-crash
Nov 17, 2026
Merged

hauntsaninja merged 2 commits into
python:masterfrom
lianxiaozhi:fix/pretty-eof-crash

Conversation

@lianxiaozhi

@lianxiaozhi lianxiaozhi commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #22424.

EOF errors can point past the last source line. Check the line number before adding a source snippet so --pretty reports the syntax error without crashing. Add a regression test.

@github-actions

This comment has been minimized.

@chrikrah chrikrah 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.

@lianxiaozhi nothing blocks this from my side.

# head e0b9c1e, Python 3.12, tree from git archive
$ python -m pytest -q -n 4 mypy/test/testcmdline.py
149 passed
# errors.py from b83696e, new case kept
$ python -m pytest -q mypy/test/testcmdline.py -k testShowSourceCodeSnippetsUnexpectedEOF
1 failed   # IndexError: list index out of range
# a.py is `x = (1,`, ast_serialize 0.13.1
$ python -m mypy --pretty --no-incremental a.py
a.py:2: error: Unexpected EOF while parsing  [syntax]
$ python -m mypy --pretty --no-incremental --no-native-parser a.py
a.py:1: error: '(' was never closed  [syntax]
    x = (1,
         ^
# not run: the full mypy suite

non-blocking: the guard belongs here either way. The line 2 comes from the native parser, so I'll open that in mypyc/ast_serialize today.

@hauntsaninja hauntsaninja left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks! cmdline tests are slow, could you try a check-columns.test or somewhere else (assuming it would have demonstrated the crash)?

@lianxiaozhi

lianxiaozhi commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

@hauntsaninja Thanks! I tried check-columns.test, but it strips the trailing newline, so the test passed even without the fix. I've moved it to testformatter.py and checked that it fails with the original IndexError before the fix and passes afterward. I've pushed the updated test to the PR. Does this look better to you?

@chrikrah Thanks for verifying the fix and looking into the parser behavior.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@hauntsaninja hauntsaninja left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you!

@hauntsaninja
hauntsaninja merged commit 22b03fe into python:master Nov 17, 2026
25 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.

Crash with generic-of-generic when called with --prety

3 participants