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

combination of unary and power operator leads to misleading formatting. #646

Closed
drj11 opened this issue Dec 20, 2018 · 2 comments · Fixed by #909
Closed

combination of unary and power operator leads to misleading formatting. #646

drj11 opened this issue Dec 20, 2018 · 2 comments · Fixed by #909
Labels
F: parentheses Too many parentheses, not enough parentheses, and so on. T: enhancement New feature or request

Comments

@drj11
Copy link

drj11 commented Dec 20, 2018

Operating system: Ubuntu 18.04.1 LTS
Python version: Python 3.6.7
Black version: 18.9b0
Does also happen on master: as of 2018-12-20T09:32, yes.

(as per my tweet: https://twitter.com/llanga/status/1075484480436080640 )

The code

-2**8

blacks to

-2 ** 8

I think this is misleading because the grammar reduction rules mean that the code actually parses as -(2**8).
The result of this code, however you format it, is -256, not 256.

I'm not sure what to suggest. Perhaps not altering the space around **?
Though I can see that almost any suggestion will be controversial.

@JelleZijlstra
Copy link
Collaborator

Maybe we should insert parentheses when the unary negation operator applies to more than just a literal (so -(2 ** 8)).

@zsol zsol added the T: enhancement New feature or request label Feb 14, 2019
@dimaqq
Copy link

dimaqq commented Jun 24, 2019

Indeed, assert -16 == -2 ** 4 is looks pretty weird!

@zsol zsol added the F: parentheses Too many parentheses, not enough parentheses, and so on. label Jun 24, 2019
ambv added a commit that referenced this issue Oct 28, 2019
This was caused by an overly liberal application of parentheses in #909 that
fixed #646.

Fixes #1041
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: parentheses Too many parentheses, not enough parentheses, and so on. T: enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants