WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Improve syntax error reporting for a better user experience #4820

@samriddhitiwary

Description

@samriddhitiwary

Is your feature request related to a problem? Please describe.
When black encounters a Python file with a syntax error, the current error message is often dense, confusing. This forces the developer to spend extra time deciphering what went wrong and where.

Image Image

Describe the solution you'd like
I propose enhancing the error reporting for SyntaxErrors to be more user-friendly, clear, and actionable.
Instead of the current one-line error, black could print a more detailed, multi-line message that pinpoints the error, much like Python's own interpreter.

Proposed new error output:

$ black test.py
Error: Cannot parse test.py

black's parser found a syntax error on or near line 4.
This is often caused by a missing ':' on the line before.

File "test.py", line 3:
def my_func()
^
SyntaxError: expected ':'

Please fix the syntax error before formatting.

Describe alternatives you've considered
Another alternative is to use a separate linter, but it would be a significant user experience improvement if black could provide this simple feedback on its own.

This proposal is not about adding new formatting rules or configuration. It's about improving the core user experience and developer productivity. By providing clear, actionable error messages, black becomes an even more helpful tool.

This could likely be implemented by catching the SyntaxError (or parser-specific error) and extracting the line number, column, and text to format it into a friendlier "pretty print" error message before exiting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions