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

Remove underscores in numeric literals #2957

Closed
jpy-git opened this issue Mar 26, 2022 · 4 comments
Closed

Remove underscores in numeric literals #2957

jpy-git opened this issue Mar 26, 2022 · 4 comments
Labels
R: rejected This will not be worked on T: style What do we want Blackened code to look like?

Comments

@jpy-git
Copy link
Contributor

jpy-git commented Mar 26, 2022

Is your feature request related to a problem? Please describe.

I've just learned that this

a = 1_2_3_4_5.6_7_8_9

is a valid alternative way of writing

a = 12345.6789

I think the intention is to let people break up large numbers if they want i.e. 1_000_000 instead of 1000000

Describe the solution you'd like

Should we decide on a consistent format for black and remove these underscores?

Describe alternatives you've considered

I've double checked and the AST is the same regardless of the presence of the underscores so we should be able to fix them without changing the AST

import ast
import astpretty

tree = ast.parse("a = 1_2_3_4_5.6_7_8_9")
astpretty.pprint(tree)
Module(
    body=[
        Assign(
            lineno=1,
            col_offset=0,
            end_lineno=1,
            end_col_offset=21,
            targets=[Name(lineno=1, col_offset=0, end_lineno=1, end_col_offset=1, id='a', ctx=Store())],
            value=Constant(lineno=1, col_offset=4, end_lineno=1, end_col_offset=21, value=12345.6789, kind=None),
            type_comment=None,
        ),
    ],
    type_ignores=[],
)
@jpy-git jpy-git added the T: enhancement New feature or request label Mar 26, 2022
@JelleZijlstra
Copy link
Collaborator

We previously did this (inserting underscores every third digit only), but undid it because it was causing issues when we inserted underscores into numeric ids, making them harder to grep.

I suppose we could remove extra underscores in cases like your first example, but I'm not sure that's really worth the complexity.

@ichard26 ichard26 added T: style What do we want Blackened code to look like? and removed T: enhancement New feature or request labels Mar 26, 2022
@jpy-git
Copy link
Contributor Author

jpy-git commented Mar 26, 2022

I agree it would be a hassle to try and handle the every third digit logic.

My suggestion was actually just remove all underscores since that's consistent/simple.

Although, if it turns out using these underscores is a common occurrence then I'd be happy to leave this as out of scope for black 👍

@JelleZijlstra
Copy link
Collaborator

Well, I'd feel bad about making Black totally remove all usage of a language feature :)

The place where the underscores shine is if you have ONE_MILLION = 1_000_000, because 1000000 makes it really hard to verify the number of zeros is right. I wouldn't want Black to remove underscores in that case.

For reference: #696, #549, #467, #454, #452.

@jpy-git
Copy link
Contributor Author

jpy-git commented Mar 26, 2022

haha that's a fair point, in that case I understand why we leave it the users to handle these! Thanks 😄

@jpy-git jpy-git closed this as completed Mar 26, 2022
@ichard26 ichard26 added the R: rejected This will not be worked on label Mar 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
R: rejected This will not be worked on T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

3 participants