Describe the bug
Slicing of bracketed expressions is broken by too eager removal of the brackets.
To Reproduce
return ('%x' % value)[-12:]return '%x' % mac[-12:]Expected behavior
Expected to not remove the brackets in this case.
Environment (please complete the following information):
Does this bug also happen on master? To answer this, you have two options:
Yes.
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:
master (b59a5246577346e6da2cc2802015f08524abf545)pyproject.toml in the Black repoCPython 3.8.1Windows 10 Home Build 18363Oops, 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)
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.