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
Enforce consistent octal, hex, and binary numbers #420
Comments
That's doubt. Zero padding exists for readability, but not a mistake reasons. |
@kxepal can you please provide an example where having multiple zeros improves readability? |
Well, let's take short hand encoding example: https://github.com/python/cpython/blob/master/Lib/encodings/cp866.py#L47-L176 Each character of single byte cp866 maps on one-two bytes unicode one. While keys in this example may be left zero-trimmed, the values will reduce readability by such action. Having both length synced looks nicer and reads well. |
@kxepal thanks for bringing this up. Indeed, sometimes inconsistent behaviour can be occasionally more readable. But, this discussion motivated me to formulate the philosophy behind my decisions: https://wemake-python-styleguide.readthedocs.io/en/latest/pages/api.html#philosophy So, we are interested in these two parts:
I strongly believe that it is easier to read numbers when there's only a one way to write them. But, you suggestion is perfectly reasonable and helpful. Much thanks, so awesome. |
You're welcome! 🍻 |
What do you thing about such rules?
|
I really like the suggestion made by @astynax |
@AndreyErmilov what do you mean by "complete issue description"? |
@AndreyErmilov now I get it. Here's the full description:
|
Ok, after reviewing two PRs, thinking hard, and playing around, I have made a decision about this rule. We are not going to support any leading zeros. If someone wants formatting, switch off this rule for this file, line, etc. We value consistency more. Why do leading zeros not fit in this rule? Let me show several examples to you: >>> [0b01, 0b0001, 0b000001]
[1, 1, 1] We also cannot replicate this example: https://github.com/python/cpython/blob/master/Lib/encodings/cp866.py#L47-L176 This code will pass, but it is incorrect: decoding_map.update({
0x80: 0x0410, # 0 leading zeros: one leading zero
0x0081: 0x0411, # 2 leading zeros, one leading zero
} We need to explicit mark places where invalid code might be located. And allow only valid code by default. |
Rule request
Thesis
We should write consistent numbers:
0b1
instead of0b0001
,0b001 == 0b1
0x1
instead of0x001
0o5
instead of0o05
Reasoning
Numbers should be consistent.
The text was updated successfully, but these errors were encountered: