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

Blacken the codebase #8903

Closed
wants to merge 238 commits into from
Closed

Conversation

pradyunsg
Copy link
Member

@pradyunsg pradyunsg commented Sep 23, 2020

Follow up to #8803

Closes #8543

Note to self: how to update this if it stalls
$ git rebase -i master
# (pick the first 3 commits)
$ nox -s lint
$ for file in $(git ls-files --modified); do
..    echo -e "\033[32m$file\033[0m"
..    git add $file
..    git commit -m "Blacken $file"
.. done
$ git cherry-pick 9443e6f

@pradyunsg pradyunsg added skip news Does not need a NEWS file entry (eg: trivial changes) type: maintenance Related to Development and Maintenance Processes labels Sep 23, 2020
@pfmoore
Copy link
Member

pfmoore commented Sep 23, 2020

Ah, this is the one that's probably got things in it I'm actually going to hate. I'm not even going to look at it, as it'll probably annoy me too much. I thought #8902 included this, which is why I was "pleasantly surprised" it was mostly OK.

I'm not going to argue against us adopting black, that's a huge debate that I don't have the energy or interest in starting again, so I'll push my head firmly into the sand here.

@pradyunsg pradyunsg mentioned this pull request Sep 23, 2020
@pradyunsg pradyunsg marked this pull request as ready for review September 23, 2020 15:19
@pradyunsg
Copy link
Member Author

Confirmed that the concerns that caused issues w/ trailing commas in #8803, are indeed resolved in this one.

noxfile.py Outdated Show resolved Hide resolved
@pradyunsg pradyunsg removed the skip news Does not need a NEWS file entry (eg: trivial changes) label Sep 23, 2020
@pradyunsg
Copy link
Member Author

Uhh... @pypa-bot hasn't reported the NEWS fragment stuff. Oh no.

@pradyunsg pradyunsg added the skip news Does not need a NEWS file entry (eg: trivial changes) label Sep 23, 2020
@uranusjr
Copy link
Member

Can we do this a module at a time instead? Black is known to not always make the best format decision (sometimes vastly incorrect IMO), and I don’t want the lost context get buried in such a hugh changeset when it does make mistakes.

@pradyunsg
Copy link
Member Author

Can we do this a module at a time instead?

Do you want me to make separate commits for each file?

@pradyunsg pradyunsg changed the title Linter updates with black Blacken the codebase Sep 23, 2020
"docs/" + kind,
"docs/build/" + kind,
"-c=docs/html", # see note above
f"-d=docs/build/doctrees/{kind}",
Copy link
Member

Choose a reason for hiding this comment

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

Is it me or this feels like a hack?

Copy link
Member Author

Choose a reason for hiding this comment

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

Using f-strings feels like a hack? I was going to do this anyway, and black forcing my move to be sooner is OK with me. 🤷

Copy link
Member

Choose a reason for hiding this comment

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

not f-strings, putting two pieces into one item

Copy link
Member

Choose a reason for hiding this comment

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

Agreed, I feel that we shouldn't try to fight the tool like this. It's up to black to have a way to allow us to express our intent here, which is to have two arguments, "-c" and "docs/html" which are separate but related. That's the semantics of the command line we'd normally write in the shell, and we should be able to reflect that readably in code.

IMO we just add #fmt: off to signal that black doesn't do what we want (see here)

Copy link
Member

Choose a reason for hiding this comment

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

Yes, this is exactly what I meant

@pradyunsg
Copy link
Member Author

Okay, GitHub does not like it when there's more than 250 commits. I'mma squash all the tests/data/ commits into a single one.

@pfmoore
Copy link
Member

pfmoore commented Sep 23, 2020

Do you want me to make separate commits for each file?

Personally, my "unit of review" is the PR, not the commit, so this would not help for me. I'd rather see multiple PRs. I've opted out of getting involved in reviewing the black formatting changes, though, so my opinion isn't relevant.

Copy link
Member Author

@pradyunsg pradyunsg left a comment

Choose a reason for hiding this comment

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

black is not perfect, but I still think it'll be worth it for us to adopt it, because it's consistent and gives a good output in nearly all cases. In many of the cases, black's "ugly" output is a good indicator of you're doing too much and that's IMO a good thing.

Unsuprisingly, I like it. It is also extremely frustrating when it makes the wrong choices though.

not canonical_package_name or
not link
)
can_not_cache = not self.cache_dir or not canonical_package_name or not link
Copy link
Member Author

@pradyunsg pradyunsg Sep 23, 2020

Choose a reason for hiding this comment

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

>.<

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like psf/black#449, so yay!

sdist_dependencies_allowed = (
options.format_control != binary_only and
not options.ignore_dependencies
options.format_control != binary_only and not options.ignore_dependencies
Copy link
Member Author

@pradyunsg pradyunsg Sep 23, 2020

Choose a reason for hiding this comment

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

>.<

Copy link
Member

Choose a reason for hiding this comment

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

There's no way a robot would guess the readability semantics you were after...

Copy link
Member Author

Choose a reason for hiding this comment

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

Which is fine? TBH the point is for it to do a good-enough job. :)

Copy link
Member

Choose a reason for hiding this comment

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

Depends on the definition of fine

Copy link
Member

Choose a reason for hiding this comment

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

I'm starting to become a strong supporter of #fmt: off 🙁

Copy link
Member Author

Choose a reason for hiding this comment

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

Again, that's fine -- black isn't going to be perfect everywhere. The point is that it's good enough almost all the time and the consistency is very useful.

To me, # fmt: off is an indication that: I'm sure that this is better than what black did for everyone that'll read the code. This is a great place to use it.

Comment on lines 41 to +43
help="Use the order in the given requirements file and its "
"comments when generating output. This option can be "
"used multiple times.")
"comments when generating output. This option can be "
"used multiple times.",
Copy link
Member Author

Choose a reason for hiding this comment

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

Oh come on.

Copy link
Member

Choose a reason for hiding this comment

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

My favorite is when it does ('str 1' 'str2' 'str 3') and nobody cares

Copy link
Member Author

Choose a reason for hiding this comment

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

It no longer does that. :)

psf/black#26

Copy link
Contributor

Choose a reason for hiding this comment

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

You can use https://github.com/keisheiled/flake8-implicit-str-concat to find stuff like 'str 1' 'str2' 'str 3'

(None currently found on this branch)

Copy link
Member

Choose a reason for hiding this comment

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

@hugovk implicit str concat is not bad if formatted properly

Copy link
Contributor

Choose a reason for hiding this comment

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

@webknjaz flake8-implicit-str-concat only prohibits implicit string concatenation on one line

Comment on lines +60 to +61
help="If in a virtualenv that has global access, do not output "
"globally-installed packages.",
Copy link
Member Author

Choose a reason for hiding this comment

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

Yea, okay, this needs to be fixed throughout the codebase actually.

Copy link
Member

Choose a reason for hiding this comment

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

If by "fixed" you mean "make black not do that" then I agree 🙂

Copy link
Member Author

Choose a reason for hiding this comment

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

It needs us to parenthesize all these help assignments, to make them consistent throughout, which is an overall positive change IMO. :)

Copy link
Member

Choose a reason for hiding this comment

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

I personally prefer this form but I can see how it could be confusing w/o parentheses.

Comment on lines +719 to +725
parts.extend(
[
user_option_part,
" or ",
permissions_part.lower(),
]
)
Copy link
Member Author

Choose a reason for hiding this comment

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

I really wouldn't write it like this, but I guess an extra variable doesn't hurt.

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like psf/black#925, so it'll get fixed down the line?

Comment on lines +470 to +474
if dist_path in {
p
for p in {sysconfig.get_path("stdlib"), sysconfig.get_path("platstdlib")}
if p
}:
Copy link
Member Author

Choose a reason for hiding this comment

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

We should simplify this by having a variable for the RHS:

paths = {p for p in [sysconfig.get_path("stdlib"), sysconfig.get_path("platstdlib")] if p}

Copy link
Member

Choose a reason for hiding this comment

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

Actually that whole construct is just opaque IMO. Needs rewriting.

Comment on lines +155 to +157
self._discovered_dependencies = defaultdict(
list
) # type: DiscoveredDependencies
Copy link
Member Author

Choose a reason for hiding this comment

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

Ugh.

Copy link
Member

Choose a reason for hiding this comment

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

I blame Python 2 (type comments) and over-long variable names for that.

Comment on lines -446 to +463
msg = msg + "\n\n" + \
"To fix this you could try to:\n" + \
"1. loosen the range of package versions you've specified\n" + \
"2. remove package versions to allow pip attempt to solve " + \
"the dependency conflict\n"
msg = (
msg
+ "\n\n"
+ "To fix this you could try to:\n"
+ "1. loosen the range of package versions you've specified\n"
+ "2. remove package versions to allow pip attempt to solve "
+ "the dependency conflict\n"
)
Copy link
Member Author

Choose a reason for hiding this comment

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

Nice!

Copy link
Member

Choose a reason for hiding this comment

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

I'd probably do implicit concat + str.format

Copy link
Member Author

Choose a reason for hiding this comment

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

Or += ? :P

Copy link
Member

Choose a reason for hiding this comment

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

No, because runtime concat would be happening multiple times which is slower + a bunch of implicitly concatenated strings parsed as one literal and then formatted just once...

Comment on lines +52 to +54
if isinstance(dist, pkg_resources.DistInfoDistribution) and dist.has_metadata(
metadata_name
):
Copy link
Member Author

Choose a reason for hiding this comment

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

Uh what?

Comment on lines +393 to +395
newpath = initial_slashes + urllib_request.url2pathname(path).replace(
"\\", "/"
).lstrip("/")
Copy link
Member Author

Choose a reason for hiding this comment

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

Uhm... really?

@pradyunsg
Copy link
Member Author

Personally, my "unit of review" is the PR, not the commit, so this would not help for me. I'd rather see multiple PRs.

Ah, smart. I think I can do this incrementally too. :)

I've opted out of getting involved in reviewing the black formatting changes, though, so my opinion isn't relevant.

:)

@pradyunsg pradyunsg marked this pull request as draft September 23, 2020 18:32
@pradyunsg
Copy link
Member Author

Making this PR a draft again, and I'll make a bunch of smaller PRs for adding black in. :)

@BrownTruck
Copy link
Contributor

Hello!

I am an automated bot and I have noticed that this pull request is not currently able to be merged. If you are able to either merge the master branch into this pull request or rebase this pull request against master then it will be eligible for code review and hopefully merging!

@BrownTruck BrownTruck added the needs rebase or merge PR has conflicts with current master label Feb 4, 2021
@pradyunsg
Copy link
Member Author

pradyunsg commented Mar 5, 2021

Closing since #8543 tracks this.

@pradyunsg pradyunsg closed this Mar 5, 2021
@pradyunsg pradyunsg deleted the linter-updates-with-black branch March 5, 2021 16:44
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs rebase or merge PR has conflicts with current master skip news Does not need a NEWS file entry (eg: trivial changes) type: maintenance Related to Development and Maintenance Processes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adopting black (and linter updates!)
8 participants