Skip to content

Improve parse error readability#5068

Open
Chandana098-learn wants to merge 7 commits intopsf:mainfrom
Chandana098-learn:improve-parse-error-message
Open

Improve parse error readability#5068
Chandana098-learn wants to merge 7 commits intopsf:mainfrom
Chandana098-learn:improve-parse-error-message

Conversation

@Chandana098-learn
Copy link
Copy Markdown

@Chandana098-learn Chandana098-learn commented Mar 29, 2026

Closes #4820
Closes #4992

I updated the parse error message while still keeping it in readable format.
New Change: Now it shows the error in multiple lines , points to the error position with ^.

Copy link
Copy Markdown
Collaborator

@cobaltt7 cobaltt7 left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Can you add an entry to CHANGES.md and run Black on itself?

Comment on lines +96 to +99
f"Cannot parse{tv_str}: {lineno}:{column}\n"
f" {te.args[0]}\n"
f" {' ' * (column - 1)}^\n"
"SyntaxError: invalid syntax"
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.

te.args[0] isn't the source code, it's the error message. The faulty line would have to be extracted from the source the same way it was for ParseError. And again, could the last line be changed to TokenError: {te.args[0]}? Thanks!

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.

I didn't realize this earlier, but it isn't fixed still, it needs to extract the error line

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 29, 2026

diff-shades results comparing this PR (2550fe8) to main (9e969dd):

--preview style: no changes

--stable style: no changes


What is this? | Workflow run | diff-shades documentation

@Chandana098-learn
Copy link
Copy Markdown
Author

Thanks for the review! I’ll complete the remaining changes you mentioned (docs and tests) by today. Apologies for the delay , I’ll update everything by tonight.


### Highlights

- Improve parse error readability by showing multi-line output with an error pointer.
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.

This should be in the "output" section

Comment on lines +96 to +99
f"Cannot parse{tv_str}: {lineno}:{column}\n"
f" {te.args[0]}\n"
f" {' ' * (column - 1)}^\n"
"SyntaxError: invalid syntax"
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.

I didn't realize this earlier, but it isn't fixed still, it needs to extract the error line

Comment on lines +92 to +95
"Cannot parse for target version Python 3.10: 10:11\n"
" case a := b:\n"
" ^\n"
"ParseError: invalid syntax"
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.

This test fails - the code should be indented another level

black.lib2to3_parse("print(", {})

exc_info.match("Cannot parse: 1:6: Unexpected EOF in multi-line statement")
exc_info.match(
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.

This call was deindented a level, that's not correct

Comment on lines +356 to +359
error: cannot format src/black_primer/cli.py: Cannot parse: 5:6
import asyncio
^
ParseError: invalid syntax
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.

That's not the actual error message, it should be:

Suggested change
error: cannot format src/black_primer/cli.py: Cannot parse: 5:6
import asyncio
^
ParseError: invalid syntax
error: cannot format src/black_primer/cli.py: Cannot parse: 5:6
mport asyncio
^
ParseError: bad input

Applies to the other two as well

Comment on lines +1993 to +1998
exc_info.match(
"Cannot parse: 1:6\n"
" Unexpected EOF in multi-line statement\n"
" ^\n"
"TokenError: Unexpected EOF in multi-line statement"
)
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.

These lines got unindented which isn't correct

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.

Improve syntax error reporting for a better user experience

2 participants