Black: Slicing of bracketed expressions is broken by removal of the brackets

Created on 7 Aug 2020  ยท  6Comments  ยท  Source: psf/black

Describe the bug

Slicing of bracketed expressions is broken by too eager removal of the brackets.

To Reproduce

  1. Try to reformat a file containing return ('%x' % value)[-12:]
  2. The resulting expression is not producing equivalent result: return '%x' % mac[-12:]

Expected behavior

Expected to not remove the brackets in this case.

Environment (please complete the following information):

  • Version: master
  • OS and Python version: Linux, Python 3.7.4

Does this bug also happen on master? To answer this, you have two options:

Yes.

bug

Most helpful comment

Oops, I thought I was on master but was actually in 19.3b1. It does reproduce on master. This is a bad bug; we should fix it before the next release.

All 6 comments

I can't reproduce this on master:

$ echo "('%x' % value)[-12:]" | python3 -m black -
("%x" % value)[-12:]
reformatted -
All done! โœจ ๐Ÿฐ โœจ
1 file reformatted.

@JelleZijlstra Interestingly I can reproduce this on master.

| Condition | Command | Output |
|---|---|---|
| Black with -c | black -c "return ('%x' % value)[-12:]" | return "%x" % value[-12:] |
| Black with a file | black test.py | error: cannot format test.py: INTERNAL ERROR: Black produced code that is not equivalent to the source. |
| Black with input read from stdin | echo ('%x' % value)[-12:] \| black - | error: cannot format -: INTERNAL ERROR: Black produced code that is not equivalent to the source. |

This was my session:

(black-rHKUX7ap) R:\Programming\black>echo return ('%x' % value)[-12:] > test.py

(black-rHKUX7ap) R:\Programming\black>type test.py                                                                                                                      
return ('%x' % value)[-12:]

(black-rHKUX7ap) R:\Programming\black>black test.py --check                                                                                                                                                                                                                    
error: cannot format test.py: INTERNAL ERROR: Black produced code that is not equivalent to the source.  Please report a bug on https://github.com/psf/black/issues.  This diff might be helpful: C:\Users\User\AppData\Local\Temp\blk_0tsx_tzh.log                                                                                             
Oh no! ๐Ÿ’ฅ ๐Ÿ’” ๐Ÿ’ฅ                                                                                                                                                         
1 file would fail to reformat.

(black-rHKUX7ap) R:\Programming\black>black test.py --fast --check                                                                                                                                                                                                              
would reformat test.py                                                                                                                                                  
Oh no! ๐Ÿ’ฅ ๐Ÿ’” ๐Ÿ’ฅ                                                                                                                                                         
1 file would be reformatted.

(black-rHKUX7ap) R:\Programming\black>black test.py --fast                                                                                                                                                                                                                      
reformatted test.py                                                                                                                                                     
All done! โœจ ๐Ÿฐ โœจ                                                                                                                                                      
1 file reformatted.

(black-rHKUX7ap) R:\Programming\black>type test.py                                                                                                                      
return "%x" % value[-12:]

(black-rHKUX7ap) R:\Programming\black>black -c "return ('%x' % value)[-12:]"                                                                                            
return "%x" % value[-12:]

(black-rHKUX7ap) R:\Programming\black>echo return ('%x' % value)[-12:] | black -                                                                                                                                                                                                                                    
error: cannot format -: INTERNAL ERROR: Black produced code that is not equivalent to the source.  Please report a bug on https://github.com/psf/black/issues.  This diff might be helpful: C:\Users\User\AppData\Local\Temp\blk_i2fy39_v.log                                                                                                   
Oh no! ๐Ÿ’ฅ ๐Ÿ’” ๐Ÿ’ฅ                                                                                                                                                         
1 file failed to reformat.                                                                                                                                                                                                                                                                                                                      

Environment:

  • Black version: master (b59a5246577346e6da2cc2802015f08524abf545)

    • Black configuration: pyproject.toml in the Black repo

  • Python version: CPython 3.8.1
  • OS version: Windows 10 Home Build 18363

Oops, I thought I was on master but was actually in 19.3b1. It does reproduce on master. This is a bad bug; we should fix it before the next release.

Git bisect points to commit 544ea9c217cad9459d7b60665db787e94b52f93d as the first bad commit.

Git bisect points to commit 544ea9c as the first bad commit.

that's a merge, i think the original commit is https://github.com/psf/black/pull/1132/commits/459eb35dc4541cd73a9a1c4abae2a3f0d7c26fd0

not sure what "This commit does not belong to any branch on this repository." means

i think we can close this? (possibly pending #1600)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

J0 picture J0  ยท  3Comments

brettcannon picture brettcannon  ยท  3Comments

nottrobin picture nottrobin  ยท  3Comments

kissgyorgy picture kissgyorgy  ยท  3Comments

JelleZijlstra picture JelleZijlstra  ยท  3Comments