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

Use lowercase hex numbers fixes #1692 #1775

Merged
merged 10 commits into from Nov 13, 2020
Merged

Use lowercase hex numbers fixes #1692 #1775

merged 10 commits into from Nov 13, 2020

Conversation

C0DK
Copy link
Contributor

@C0DK C0DK commented Oct 21, 2020

First PR onto black. Please tell me if i did something wrong according to your ordinary rules.

The issue, #1692, argues whether we/you should "simply" do this, as it might impact source code of a large set of users, however you can either merge it if you like it or wait if not. :) Please write me if I can do anything in that regard.

I created two seperate commits:

  • Made hex lower case
  • Refactored numeric formatting section

This was done to seperate the two focus points, the first one solves the issue, whereas the second adds a bit of refactoring to that part of the code. Whether my version has a higher code quality or not, is definitely subjective, so please ignore it and cherry-pick if you dislike it :)

I am looking forward to contributing to black, long time user, and all that, so I hope you like it. I'll try to do something once in a while - Am doing this as part of hacktoberfest though, so if you can either label it hacktoberfest-accepted or merge it before the end of the month, that will be great ;) I feel super cheap writing that, but i am trying to pimp my bike with a yearly sticker from hacktoberfest, and this month is running a bit short. If any changes are needed, I'll gladly make those :)

Thank you!

Copy link
Collaborator

@ichard26 ichard26 left a comment

Choose a reason for hiding this comment

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

Didn't do a deep check on the code, but I did notice a few stylistic things.

Comment on lines 72 to 73
# All possible string prefix characters.
STRING_PREFIX_CHARS: Final = "furbFURB"
Copy link
Collaborator

Choose a reason for hiding this comment

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

NIT: IMO this is unnecessary and just ruins blame information.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

was a result of autoformatter. I see the point. will revert.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed (also all the rest)

Comment on lines 5344 to 5345
# Can be a container node with the `leaf`.
first = ignored_nodes[0]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto

Comment on lines 1905 to 1906
# Line is empty, don't emit. Creating a new one unnecessary.
return
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto

.gitignore Outdated
Comment on lines 1 to 3
.venv/


Copy link
Collaborator

Choose a reason for hiding this comment

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

I would prefer if the two extra empty lines were removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will fix. Thank you.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

.venv Outdated
@@ -0,0 +1 @@
black
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is this file for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was an error. sorry. should have been caught by the gitignore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

# Leave octal and binary literals alone.
pass
elif text.startswith("0x"):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we need this refactoring; to me it just creates an excessive number of small functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is fair - I relied on the Uncle Bob principle of "If you need a comment, then you should probably just have a more descriptive function name", but i might be misquoting or misunderstanding that. It is definitely a subjective matter. I liked the, in my opinion, higher level of readability, but the, already long, file gets longer which is maybe not cool. We can also drop the whole refactoring commit (or only take the formatting parts).

It's subjective :) I'll go with what you guys prefer.

Copy link

Choose a reason for hiding this comment

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

It doesn't hurt to add a comment to some of these if statements. E.g.:

elif "e" in text: # scientific notation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The function, in my opinion, does exactly the same without duplication and without revealing implementation details. Is a general consesus that the functions decrease quality? As mentioned, i explicitly added it in a seperate commit to make it easy to drop :)

coverage >= 5.2.1
aiohttp >= 3.3.2
aiohttp-cors
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these two additions needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes - i was not able to run all the tests witout them :) Some tests rely on AIO stuff, so when i installed them i added them as well :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Aha, CONTRIBUTING.md suggests using pipenv, which includes them.

I'm not so familiar with pipenv, but I see Travis CI runs pip install -e '.[d]', where the d is a so-called "extra" listed in setup.py:

    extras_require={
        "d": ["aiohttp>=3.3.2", "aiohttp-cors"],
        "colorama": ["colorama>=0.4.3"],
    },

So that will install them for you. And GitHub Actions uses tox which also installs with the extra.

Want to try running pip install -e '.[d]' instead?

Copy link
Collaborator

Choose a reason for hiding this comment

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

AFAIK this requirements file is for CI not developers of Black. Doing a Pipenv developement install should install Black with its HTTP web server dependencies.

black/Pipfile

Line 21 in 4070527

black = {editable = true, extras = ["d"], path = "."}

You get pinned dependencies too so doing a Pipenv development install is recommended

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oic. I use some...thing.. not actually sure. I simply realized it wasn't exhaustive, not that it was defined elsewhere. Should i remove these two lines?

Copy link
Contributor

Choose a reason for hiding this comment

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

Another popular thing to do is to move tests dependencies from test_requirements.txt into a tests extra, for example:

But that's for another PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I think remove those two lines to avoid duplication and keep things lean 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed :)

@hugovk
Copy link
Contributor

hugovk commented Oct 23, 2020

For the Primer failures:

The CI is running Black on a few other projects to see if the formatting changes. Sometimes we don't want that to happen. In this case, some changes are expected.

I checked a log, 5 projects now fail and they all look like hex number changes, as expected.

To fix: flip expect_formatting_changes": false, to true in src/black_primer/primer.json for attrs, hypothesis, Pillow, pyramid and virtualenv.

@C0DK
Copy link
Contributor Author

C0DK commented Oct 23, 2020

@hugovk Thank you :) Should be fixed now.

Regarding my refactoring, show i revert those? What is the general consesus? I can always revert it and then put it in another PR

@ichard26
Copy link
Collaborator

I completely forgot but please also add an entry in the change log, thank you!



def is_scientific_notation(text: str) -> bool:
"""Checks if the supplied string is a number with scientific notation"""
Copy link

Choose a reason for hiding this comment

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

This code matches 0xdeadbeef; it's an incorrect abstraction that depends on is_hex and other test having been applied earlier.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Valid. Should i move all the checkers into the original function, while still moving the formatting into sub functions? Would that be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved these now :)

Copy link
Collaborator

@cooperlees cooperlees left a comment

Choose a reason for hiding this comment

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

LGTM - But want other core devs completely happy too.

@C0DK
Copy link
Contributor Author

C0DK commented Oct 28, 2020

LGTM - But want other core devs completely happy too.

I'll update the changelog in less than 12 hours.

I hope i can get a hacktoberfest-approved label on this by the end of the month :p if you are desperately against hacktoberfest, then i understand :) I enjoy it as a reminder to give back to the community, meanwhile sticker-hunting is a desperate hobby of mine.

@zsol
Copy link
Collaborator

zsol commented Oct 28, 2020

LGTM. To make me completely happy, I'd also revert the refactoring that creates three tiny functions that @JelleZijlstra pointed out.

@C0DK
Copy link
Contributor Author

C0DK commented Oct 29, 2020

LGTM. To make me completely happy, I'd also revert the refactoring that creates three tiny functions that @JelleZijlstra pointed out.

Whichh are these? I did revert the smallest of functions, but kept the formatting functions, as the numeric formatting already was in it's own function. This keeps it on the same level of abstraction, IMO.

src/black/__init__.py Outdated Show resolved Hide resolved
@zsol
Copy link
Collaborator

zsol commented Oct 29, 2020

I did revert the smallest of functions, but kept the formatting functions, as the numeric formatting already was in it's own function. This keeps it on the same level of abstraction, IMO.

You're right, the formatting functions are fine. I was referring to the functions that only called startswith("0x") and such. All good now.

Co-authored-by: Zsolt Dollenstein <zsol.zsol@gmail.com>
@C0DK
Copy link
Contributor Author

C0DK commented Oct 30, 2020

I've mentioned it before, but didn't get any response: Will you add a hacktoberfest label? :P It's fine if you guys don't want to encourage it - I totally respect that. I'm just a bit pressed if i want to partake this year otherwise haha.

@hugovk

This comment has been minimized.

@cooperlees
Copy link
Collaborator

I've mentioned it before, but didn't get any response: Will you add a hacktoberfest label? :P It's fine if you guys don't want to encourage it - I totally respect that. I'm just a bit pressed if i want to partake this year otherwise haha.

Is this label all you need?

@C0DK
Copy link
Contributor Author

C0DK commented Oct 30, 2020

Is this label all you need?

hacktoberfest-accepted - to be really annoying ;)

@cooperlees cooperlees merged commit 7d032fa into psf:master Nov 13, 2020
@C0DK C0DK deleted the issue-1692 branch November 14, 2020 10:56
ambv added a commit that referenced this pull request Apr 25, 2021
noxan pushed a commit to noxan/black that referenced this pull request Jun 6, 2021
* Made hex lower case

* Refactored numeric formatting section

* Redid some refactoring and removed bloat

* Removed additions from test_requirements.txt

* Primer now expects expected changes

* Undid some refactoring

* added to changelog

* Update src/black/__init__.py

Co-authored-by: Zsolt Dollenstein <zsol.zsol@gmail.com>

Co-authored-by: Zsolt Dollenstein <zsol.zsol@gmail.com>
Co-authored-by: Cooper Lees <me@cooperlees.com>
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.

None yet

7 participants