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

Add underscores to numeric literals with more than 6 digits #529

Merged
merged 5 commits into from
Sep 26, 2018

Conversation

zsol
Copy link
Collaborator

@zsol zsol commented Sep 25, 2018

No description provided.

@coveralls
Copy link

coveralls commented Sep 25, 2018

Pull Request Test Coverage Report for Build 751

  • 14 of 15 (93.33%) changed or added relevant lines in 3 files are covered.
  • 28 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.3%) to 96.197%

Changes Missing Coverage Covered Lines Changed/Added Lines %
black.py 4 5 80.0%
Files with Coverage Reduction New Missed Lines %
blackd.py 2 38.89%
tests/test_black.py 3 67.36%
black.py 23 66.14%
Totals Coverage Status
Change from base Build 743: 0.3%
Covered Lines: 2909
Relevant Lines: 3024

💛 - Coveralls

@zsol zsol changed the title Add underscores to numeric literals with more than 3 consecutive repeating digits Add underscores to numeric literals with more than 4 digits Sep 25, 2018
@zsol
Copy link
Collaborator Author

zsol commented Sep 25, 2018

@JelleZijlstra thanks for reviewing :) We decided to just use underscores for numbers longer than 4 digits as that's simpler to understand for users (less surprising behavior) as well as 12345 is just as unreadable as 10000 for some people.

@JelleZijlstra
Copy link
Collaborator

Sure, that's fine with me too, probably better because it's a simpler rule.

@ghost
Copy link

ghost commented Sep 25, 2018

I like the idea but black is supposed to be the same for everyone and this would force western digit grouping onto all users. Concrete example: India would write 1_00_000 and not 100_000.

https://en.wikipedia.org/wiki/Indian_numbering_system

You wouldn't change quoted text strings, so why digits?

@ambv
Copy link
Collaborator

ambv commented Sep 26, 2018

Four digits is a little noisy, it's not really necessary for readability at that level. It would be way less controversial to standardize from six digits up. E.g.:

1
123
1023
12023
123_023
1_123_023
...

@Thalessa, thanks for bringing this up! The point of Black is to standardize everything so that code will look the same regardless of your project. Countries have differing standards, another popular example is the usage of a comma or a period for the decimal point separator (in Poland we use the comma). In this case, unless your project is local to the Indian subcontinent and does not anticipate any external contribution, standardizing on Indian separators is out of the question. So Black won't do that.

However, I do recognize that if you were raised with the Indian numbering system in school, your brain is hard-wired now to read numbers like this. In other words, it's not just about the separators, it's about reading the numbers in your mind. Forcing an incompatible standard here is harsh.

Thus, I think we need -N or --skip-number-normalization. Just like with -S, it doesn't let you configure how to standardize, but it lets you opt out if the standardization hurts productivity on your particular project.

@ambv
Copy link
Collaborator

ambv commented Sep 26, 2018

I'm reading about this some more now and in fact in Chinese numerals are grouped by tens-of-thousands:

System Notation Reading
Western 12,345,678,902,345 (12) [1,000,000,000,000] (345) [1,000,000,000] (678) [1,000,000] (902) [1,000] (345) [1]
Chinese 12,3456,7890,2345 (12) [1,0000,0000,0000] (3456) [1,0000,0000] (7890) [1,0000] (2345) [1]

This reinforces my thinking that we should let certain projects opt out with -N.

@zsol zsol changed the title Add underscores to numeric literals with more than 4 digits Add underscores to numeric literals with more than 5 digits and add flag to disable this Sep 26, 2018
@@ -618,7 +636,8 @@ def format_str(
remove_u_prefix=py36 or "unicode_literals" in future_imports,
is_pyi=is_pyi,
normalize_strings=normalize_strings,
allow_underscores=py36,
allow_underscores=py36
and not bool(mode & FileMode.NO_NUMERIC_UNDERSCORE_NORMALIZATION),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow this looks ugly

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you rather have this be extra one-indented? That's possible but unprecedented in Black so far. We can also wrap the entire value in optional parentheses but then you will have four lines instead of two.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally prefer the optional parentheses even if it makes the expression break over 4 lines. Not sure if that is the best way to go in the general case, though.

@ambv ambv changed the title Add underscores to numeric literals with more than 5 digits and add flag to disable this Add underscores to numeric literals with more than 6 digits Sep 26, 2018
@ambv ambv merged commit 5f9eb9e into master Sep 26, 2018
@zsol zsol deleted the repeating-digits branch March 14, 2019 17:35
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

Successfully merging this pull request may close these issues.

4 participants