Skip to content
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 cannot parse list unpacking #3977

Closed
ThGaskin opened this issue Oct 25, 2023 · 6 comments
Closed

Black cannot parse list unpacking #3977

ThGaskin opened this issue Oct 25, 2023 · 6 comments
Labels
C: parser How we parse code. Or fail to parse it. T: bug Something isn't working

Comments

@ThGaskin
Copy link

Describe the bug

The following snippet blocks git commits:

    # Get the counts and the bin centres
    counts, bin_centres = np.take(res, 0, axis=axis), np.take(res, 1, axis=axis)
    sel = [0] * len(np.shape(bin_centres))
    sel[axis] = None
    bin_centres = bin_centres[*sel].flatten() # <---- Error!

with error

error: cannot format model_plots/data_ops.py: Cannot parse: 462:30:     bin_centres = bin_centres[*sel].flatten()
  • Black's version: black==23.10.1
  • OS and Python version:macOS Ventura 13.4.1, Python 3.11.6
@ThGaskin ThGaskin added the T: bug Something isn't working label Oct 25, 2023
@JelleZijlstra JelleZijlstra added the C: parser How we parse code. Or fail to parse it. label Oct 25, 2023
@JelleZijlstra
Copy link
Collaborator

Note this is new syntax in Python 3.11, introduced by PEP 646. See python/cpython#31018 for the Python grammar changes; we'll need something similar.

@hauntsaninja
Copy link
Collaborator

I cannot reproduce. Black already fully supports PEP 646 as far as I can tell, see https://github.com/psf/black/pull/3071/files/ Are you sure you're using 23.10.1?

λ cat xx.py
counts, bin_centres = np.take(res, 0, axis=axis), np.take(res, 1, axis=axis)
sel = [0] * len(np.shape(bin_centres))
sel[axis] = None
bin_centres = bin_centres[*sel].flatten() # <---- Error!

λ python -m black xx.py --check --diff
--- xx.py	2023-10-26 05:26:40.021527+00:00
+++ xx.py	2023-10-26 05:30:33.375899+00:00
@@ -1,4 +1,4 @@
 counts, bin_centres = np.take(res, 0, axis=axis), np.take(res, 1, axis=axis)
 sel = [0] * len(np.shape(bin_centres))
 sel[axis] = None
-bin_centres = bin_centres[*sel].flatten() # <---- Error!
+bin_centres = bin_centres[*sel].flatten()  # <---- Error!
would reformat xx.py

Oh no! 💥 💔 💥
1 file would be reformatted.

@henriholopainen
Copy link
Contributor

Also can't repro.

% black --version
black, 23.10.1 (compiled: no)
Python (CPython) 3.11.3
% black --diff t.py
--- t.py	2023-10-27 09:11:03.653578+00:00
+++ t.py	2023-10-27 09:11:04.873324+00:00
@@ -1,5 +1,5 @@
 # Get the counts and the bin centres
 counts, bin_centres = np.take(res, 0, axis=axis), np.take(res, 1, axis=axis)
 sel = [0] * len(np.shape(bin_centres))
 sel[axis] = None
-bin_centres = bin_centres[*sel].flatten() # <---- Error!
+bin_centres = bin_centres[*sel].flatten()  # <---- Error!
would reformat t.py

All done! ✨ 🍰 ✨
1 file would be reformatted.

@JelleZijlstra
Copy link
Collaborator

Also can't reproduce, so closing.

It's possible there's another syntax issue later in the file that makes Black try falling back to a grammar without PEP 646 support, though I can't reproduce that either.

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Oct 28, 2023
@ThGaskin
Copy link
Author

The error occurs when committing via git:

sort imports.............................................................Passed
black code formatting....................................................Failed
- hook id: black
- exit code: 123

error: cannot format model_plots/data_ops.py: Cannot parse: 383:34:         bin_centres = bin_centres[*sel].flatten()

Oh no! 💥 💔 💥
6 files left unchanged, 1 file failed to reformat.

check RST backticks..................................(no files to check)Skipped
fix end of files.........................................................Passed
fix trailing whitespace..................................................Passed

When I run black path/to/file it reformats the file just fine.

@henriholopainen
Copy link
Contributor

@ThGaskin is the pre-commit hook running a different version/configuration of black? Could you share the full file file or try to isolate the issue and share a minimal snippet where it reproduces?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: parser How we parse code. Or fail to parse it. T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants