-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Black thinks that a file containing a syntax error is well formatted #2604
Comments
On the cases where Not a black maintainer but I think this is the appropriate case since this is simply not black's job. If you use |
As an outsider to the project, this didn't make much sense:
But having dug in, I think I understand better. Black's algorithm is:
lib2to3 doesn't handle f-strings, and so it is the AST parsing that finds the In this case, my example is unchanged after reformatting, and so the input and output strings are the same, so Black doesn't need to check that they parse to the same AST, and Black thinks the file is correctly formatted. Is that correct? I don't know whether it's reasonable to expect Black to detect syntax errors -- but I do think it's surprising that it sometimes does and sometimes doesn't. |
That's fair, and it does seem a bit surprising. But we only promise to format correct Python. |
@JelleZijlstra I guess this can be closed after #2645 |
Thanks, agree that #2645 addresses this. (For this specific case, we should catch this error in the future if we end up formatting the expression bits of f-strings.) |
Describe the bug
Black thinks that a file containing a syntax error is well formatted.
To Reproduce
With a file containing just this code:
Black does not give an error:
$ black bad.py All done! ✨ 🍰 ✨ 1 file left unchanged.
But there is a syntax error in the file:
Expected behavior
I would expect Black to complain!
Environment
Additional context
Black does complain as expected with the following similar files:
It also complains as expected if there is an extra newline at the end of the file, but that doesn't render in GHFM.
The text was updated successfully, but these errors were encountered: