Black: combination of unary and power operator leads to misleading formatting.

Created on 20 Dec 2018  路  2Comments  路  Source: psf/black

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.

enhancement parentheses

Most helpful comment

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

All 2 comments

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

decibyte picture decibyte  路  3Comments

J0 picture J0  路  3Comments

dimaqq picture dimaqq  路  3Comments

madig picture madig  路  3Comments

kissgyorgy picture kissgyorgy  路  3Comments