question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Black fails to format an un-formatted file with walrus operator

See original GitHub issue

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:

if 3 > (a := 3):
    pass

x = [1, 2]
$ 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:

if 3 > (a := 3):
    pass

x = [1, 2,]
$ black white_walrus.py 
error: cannot format white_walrus.py: cannot use --safe with this file; failed to parse source file AST: invalid syntax (<unknown>, line 1)
This could be caused by running Black with an older Python version that does not support new syntax used in your source file.

Oh no! 💥 💔 💥
1 file failed to reformat.

Expected behavior

In the second example, Black should re-format the file with no errors.

Environment

$ black --version
black, 22.6.0 (compiled: yes)
Python (CPython) 3.6.10

$ python --version
Python 3.6.10 :: Anaconda, Inc.

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.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ichard26commented, Jun 28, 2022

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 ast module which obviously does not support syntax beyond the running Python. We only run the safety check if changes were made so that’s why if no changes are made, Black “works.”

I think there’s something to this. For example, I have 3.9 but I’m able to run Black on match statements with --target-version py310. However, it only works when there’s nothing to change, but fails with “cannot parse” when the target version is left out and “invalid syntax” when there are changes.

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?)

It would be good if Black informed that targeting a higher version of Python that the Black itself is running on is not supported. Actually, it could also work for auto-detected target Python version.

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.

0reactions
michcio1234commented, Jun 28, 2022

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 --target-version py39 and got the same behaviour (I’m not 100% sure though).

It would be good if Black informed that targeting a higher version of Python that the Black itself is running on is not supported. Actually, it could also work for auto-detected target Python version.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Black fails to format over-length line containing walrus ...
As per title - lines longer than 88 containing a walrus operator fail to be formatted. It works on master though (tested in...
Read more >
Walrus operator in Python interpreter - Stack Overflow
1 Answer 1. Sorted by: Reset to default. Highest score (default) ...
Read more >
The Walrus Operator: Python 3.8 Assignment Expressions
This script can read one or several text files and report how many lines, words, and characters each of them contains. Here's a...
Read more >
What is List Comprehension in Python? - Scaler Topics
Takeaway: Walrus operator allows us to both assign a value to a variable, and to return that value, all in the same expression....
Read more >
How to use pydantic to read environment variables and secret ...
A common practice is to store the credentials as environmental variables or secret files on the machine on which the application is running....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found