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

Switch to black #1540

Merged
merged 8 commits into from
May 22, 2020
Merged

Switch to black #1540

merged 8 commits into from
May 22, 2020

Conversation

pquentin
Copy link
Member

@pquentin pquentin commented May 22, 2020

Trying again after #768 in 2018. Why now? black is much more popular, and Trio supports the same versions of Python as black. (It does not support PyPy, though.)

The benefits:

  • we're using a tool more contributors are familiar with
  • all editors have useful black integrations, unlike yapf
  • we won't have to be worried about yapf migration from lib2to3 which is deprecated
  • the output is less likely to change between versions
  • the output is generally more useful
  • we stay at 80 characters, so @njsmith can keep his stone age configuration :)

The downsides:

  • all pull request will get conflicts!
  • git blame will be less useful

Copy link
Member

@oremanj oremanj left a comment

Choose a reason for hiding this comment

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

Thank you for doing this! I pointed out a few places where we could tweak things to make the new formatting look better. Please feel free to commit this with or without those changes, and without further review from me -- I'd like to avoid having it stay open for too long, since it's such an annoying change to merge with.


Very occasionally, yapf will generate really ugly and unreadable
Copy link
Member

Choose a reason for hiding this comment

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

black supports this too with # fmt: off and # fmt: on comments; maybe worth mentioning?

platform.python_implementation() == "CPython" and sys.version_info <
(3, 6, 2)

buggy_wakeup_fd = platform.python_implementation() == "CPython" and sys.version_info < (
Copy link
Member

Choose a reason for hiding this comment

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

ugh, this is an ugly one. Doesn't really matter though. It might do a better job if you put the clauses in the opposite order, and surround with parens?

trio/_socket.py Outdated
@@ -338,10 +341,10 @@ def _make_simple_sock_method_wrapper(methname, wait_fn, maybe_avail=False):
async def wrapper(self, *args, **kwargs):
return await self._nonblocking_helper(fn, args, kwargs, wait_fn)

wrapper.__doc__ = (
"""Like :meth:`socket.socket.{}`, but async.
wrapper.__doc__ = """Like :meth:`socket.socket.{}`, but async.
Copy link
Member

Choose a reason for hiding this comment

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

Maybe make this into an f-string so it doesn't format so poorly?

@@ -53,11 +58,13 @@ async def wait_with_ul1():

def test_module_metadata_is_fixed_up():
import trio

assert trio.Cancelled.__module__ == "trio"
assert trio.open_nursery.__module__ == "trio"
assert trio.abc.Stream.__module__ == "trio.abc"
assert trio.lowlevel.wait_task_rescheduled.__module__ == "trio.lowlevel"
import trio.testing
Copy link
Member

Choose a reason for hiding this comment

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

With the extra blank lines, I think it looks better to put both imports next to each other.

@oremanj
Copy link
Member

oremanj commented May 22, 2020

Oh, and I believe you can fix the CI issues by only installing black on CPython. (It depends on typed-ast, which doesn't compile under PyPy.)

@codecov
Copy link

codecov bot commented May 22, 2020

Codecov Report

Merging #1540 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #1540   +/-   ##
=======================================
  Coverage   99.67%   99.67%           
=======================================
  Files         108      108           
  Lines       13356    13358    +2     
  Branches     1012     1012           
=======================================
+ Hits        13313    13315    +2     
  Misses         28       28           
  Partials       15       15           
Impacted Files Coverage Δ
trio/__init__.py 100.00% <ø> (ø)
trio/_core/_exceptions.py 100.00% <ø> (ø)
trio/_core/_traps.py 100.00% <ø> (ø)
trio/_core/_unbounded_queue.py 100.00% <ø> (ø)
...tests/test_multierror_scripts/apport_excepthook.py 100.00% <ø> (ø)
trio/_core/tests/test_parking_lot.py 100.00% <ø> (ø)
trio/_deprecated_ssl_reexports.py 100.00% <ø> (ø)
trio/_deprecated_subprocess_reexports.py 100.00% <ø> (ø)
trio/_highlevel_open_tcp_stream.py 96.96% <ø> (ø)
trio/_highlevel_open_unix_stream.py 91.30% <ø> (ø)
... and 73 more

@njsmith
Copy link
Member

njsmith commented May 22, 2020 via email

@pquentin
Copy link
Member Author

@njsmith I would like to apologize for using the term "stone age", in my defense I just reused your words from #768.

Should we use 88 characters, then? I guess sticking to black defaults is better when possible. And more generally, are you in favor of this or do you still have concerns?

@oremanj
Copy link
Member

oremanj commented May 22, 2020

I'm neutral on whether we switch to 88 or stay at 80. (I also use an approx width=88 editor setup.) But I think if we plan to reflow we should do it now, to consolidate all the disruptive changes in one place.

@pquentin
Copy link
Member Author

But I think if we plan to reflow we should do it now, to consolidate all the disruptive changes in one place.

Agreed. I'm happy to wait for consensus: it's easy to rebase this as the actual black changes are isolated in their own commit.

@njsmith
Copy link
Member

njsmith commented May 22, 2020

Oh, I wasn't bothered, just terse b/c I was replying from my phone while waiting for my laptop to upgrade to Ubuntu 20.04 :-).

I'm in favor of switching to black, and I guess mildly in favor of sticking to the defaults instead of fighting them.

pquentin added 6 commits May 22, 2020 13:03
Unfortunately, full black compliance is not possible, as a few lines are
too long. This is why I added the `fmt` pragmas.
It depends on typed-ast, which isn't supported on PyPy.
@pquentin
Copy link
Member Author

@njsmith @oremanj Can you please take another look? I now use the default 88 characters. I had to rebase, in order to avoid two reformatting commmits, sorry about that!

Copy link
Member

@oremanj oremanj left a comment

Choose a reason for hiding this comment

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

Looks good! Again, minor suggestions for cleanups and you can feel free to commit once you resolve them.

@@ -84,8 +81,7 @@ def monitor_kevent(self, ident, filter):
key = (ident, filter)
if key in self._registered:
raise _core.BusyResourceError(
"attempt to register multiple listeners for same "
"ident/filter pair"
"attempt to register multiple listeners for same " "ident/filter pair"
Copy link
Member

Choose a reason for hiding this comment

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

combine these into a single string (here and below) -- you might want to grep for ".*" ".*" to see if there are any I missed

buggy_wakeup_fd = (
platform.python_implementation() == "CPython" and sys.version_info <
(3, 6, 2)
sys.version_info < (3, 6, 2) and platform.python_implementation() == "CPython"
Copy link
Member

Choose a reason for hiding this comment

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

This can go back to the original order now that we have longer lines

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately, with the other order Black produces the original ugly code with each number on its own line.

trio/_socket.py Outdated
"Only available on platforms where :meth:`socket.socket.{}` "
"is available.".format(methname)
f"Only available on platforms where :meth:`socket.socket.{methname}`"
" is available."
Copy link
Member

Choose a reason for hiding this comment

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

Can this space move to the end of the previous line now that we have more room?

trio/_socket.py Outdated
@@ -497,8 +497,7 @@ async def _resolve_address(self, address, flags):
elif self._sock.family == _stdlib_socket.AF_INET6:
if not isinstance(address, tuple) or not 2 <= len(address) <= 4:
raise ValueError(
"address should be a (host, port, [flowinfo, [scopeid]]) "
"tuple"
"address should be a (host, port, [flowinfo, [scopeid]]) " "tuple"
Copy link
Member

Choose a reason for hiding this comment

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

Merge strings

trio/_sync.py Outdated
@@ -321,8 +317,7 @@ def release_on_behalf_of(self, borrower):
"""
if borrower not in self._borrowers:
raise RuntimeError(
"this borrower isn't holding any of this CapacityLimiter's "
"tokens"
"this borrower isn't holding any of this CapacityLimiter's " "tokens"
Copy link
Member

Choose a reason for hiding this comment

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

Merge strings

@pquentin pquentin merged commit 15facd9 into python-trio:master May 22, 2020
@pquentin pquentin deleted the black branch May 22, 2020 10:56
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.

3 participants