Black: Numeric formatting

Created on 20 Aug 2018  路  3Comments  路  Source: psf/black

I recently landed a diff adding normalization of numeric literals (#454, #464), but I want to make sure we all agree on the behavior before this goes into a release. It would be bad for user trust in Black if we change formatting in a release, then change our mind again in another release, so I'd rather discuss these now before they're locked into a release.

Here are the formatting rules included in my diff:

  • 123456789 -> 123_456_789
  • 0.12345678 -> 0.12_345_678

    • Should this be 0.123_456_78 instead? Or perhaps we don't need underscores at all in the fractional part of floats; my code adds it to any part of a numeric literal that consists of multiple digits.

  • 1E10 -> 1e10, and similarly 2J -> 2j and (Python 2) 2L -> 2l
  • 1e+10 -> 1e10
  • 0XABCD -> 0xabcd (so generally, all letters are lowercased)
  • .1 -> 0.1 and 1. -> 1.0

    • Not too sure about whether this is a good idea

  • 0xabcdefgh stays unchanged, instead of changing to 0x_abcd_efgh or similar. Similarly, no underscores in octal or binary literals. PEP 515 (https://www.python.org/dev/peps/pep-0515/#id26) suggests splitting hex literals by words and binary literals by nybbles in examples. The current code also doesn't remove any underscores in hex/oct/bin literals.

Let me know if you disagree with any of these. If nobody feels strongly, we can just stick with the current behavior.

Most helpful comment

Hi,
0.12_345_678 has no sense for me (I'm french, regional choice ?), 0.123_456_78 is far better but seems unusual.
The others seems ok to me

All 3 comments

Hi,
0.12_345_678 has no sense for me (I'm french, regional choice ?), 0.123_456_78 is far better but seems unusual.
The others seems ok to me

(Python 2) 2L -> 2l

I realize this would make the rules less consistent, but I think this deserves a special case. In most fonts, 2l is hard to distinguish from 21 so I usually prefer 2L to enhance readability.

0.12345678 -> 0.12_345_678

I also think 0.123_456_78 is easier to read because you don't have to remember the initial "offset" all the way while reading from left to right. I think it's helpful to use underscores in this case because of the same reasons as with integers.

.1 -> 0.1 and 1. -> 1.0

I think the second one is actually really helpful because it's easy to miss an extra dot at the end of a line. I don't have as strong of an opinion on .1 probably because I've gotten used to it.

OK, I'm going to go with these two changes (capitalize 2L and change underscores in the fractional part of floats). Thanks for the responses!

Was this page helpful?
0 / 5 - 0 ratings