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

Numeric formatting #467

Closed
JelleZijlstra opened this issue Aug 20, 2018 · 3 comments
Closed

Numeric formatting #467

JelleZijlstra opened this issue Aug 20, 2018 · 3 comments

Comments

@JelleZijlstra
Copy link
Collaborator

JelleZijlstra commented Aug 20, 2018

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.

@jgirardet
Copy link
Contributor

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

@zsol
Copy link
Collaborator

zsol commented Aug 20, 2018

(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.

@JelleZijlstra
Copy link
Collaborator Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants