-
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 fails to format an un-formatted file with walrus operator #3143
Comments
Thanks for reporting in detail! If the problem is in the trailing comma, does the previous statement (walrus) affect it? So, does it fail if it's removed? |
You're using Python 3.6 to format a file using Python 3.8 feature. I don't think that's supported. |
@jack1142 Hahh, of course. 🤦🏻 Updating to Python 3.9 fixed the issue. Thank you. |
I think there's something to this. For example, I have 3.9 but I'm able to run Black on match statements with I'm unsure how the parsing works exactly, but having it work with no changes seems odd to me. |
Yeah, that's what was happening to me. It was not about the magic comma; it was about any reformatting at all. I think I even tried with It would be good if Black informed that targeting a higher version of Python that the Black itself is running on is not supported. |
The reason is because our main parser (blib2to3) is extremely lax (mostly to support Python 3.10 syntax without rewriting the whole parser and therefore codebase) so it doesn't care if the Python version is too low, it'll happily parse newer syntax. For the AST equivalence safety check though, it uses the built-in
Yeah we do lock the very coarse Python version configuration of blib2to3 to the target-version, but by default without a target-version specified, it should select all grammars. Are you sure you're running Black in a clean directory (without pyproject.toml?)
I like the idea, but doing it for auto-detected Python is a bit harder since we would want to warn the user only once. That's difficult to do when the files are being processed in different processes at the same time thanks to multiprocessing. |
Describe the bug
If a file is not black-formatted and it contains a walrus operator, Black fails.
If a file is already black-formatted and it contains a walrus operator, Black succeeds.
It happens on my machine on version 22.6.0. It doesn't happen in playground.
To Reproduce
With this code, which is already black-formatted, all is good:
$ black black_walrus.py All done! ✨ 🍰 ✨ 1 file left unchanged.
But with this code, which is not black-formatted (notice the magic comma), Black fails:
Expected behavior
In the second example, Black should re-format the file with no errors.
Environment
OS: Linux Mint 20.3, kernel 5.4.0-120-generic
Additional context
Since it works correctly in the playground, I suppose something is wrong with my local installation, but I have no idea what it can be.
The text was updated successfully, but these errors were encountered: