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

[9512] Initial configuration for black, take two #1380

Merged
merged 14 commits into from Sep 13, 2020
Merged

[9512] Initial configuration for black, take two #1380

merged 14 commits into from Sep 13, 2020

Conversation

twm
Copy link
Contributor

@twm twm commented Sep 12, 2020

This is a rebase of, and closes #1134. I removed the documentation changes that proved so contentious, but added a few necessary to reconcile with The Black Code Style.

Contributor Checklist:

@twm twm marked this pull request as ready for review September 12, 2020 07:00
Copy link
Contributor

@rodrigc rodrigc left a comment

Choose a reason for hiding this comment

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

The old pycodestyle target would fail in CI if someone submitted a PR,
but it did not pass pycodestyle changes.

If someone submits a PR, and it is not formatted with black,
will there be a CI failure, so that the user will know that they have to reformat their patch with black,
with tox -e black?

tox.ini Outdated

[testenv:black]
skip_install=True
basepython=python3.6
Copy link
Contributor

Choose a reason for hiding this comment

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

In this PR, the basepython for running black is Python 3.6.
However, Python 3.5 support has not yet been dropped from Twisted.

Do you need to specify --target to black, to prevent style changes incompatible with Python 3.5 from going in?

I'm not in favor of pinning the tox environment for black to a specific Python version,
because a Twisted contributor may have a different version of Python installed on their system,
and tox -e black will not work for them.

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 believe that the target-version in pyproject.toml ensures that 3.5-compatible syntax is used.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you are correct. That's good enough then.

tox.ini Outdated
@@ -151,6 +148,25 @@ deps=https://github.com/twisted/pydoctor/archive/3f9c64829dfa040b334c9ae27c332c7
[testenv:manifest-checker]
skip_install = true

[testenv:lint]
skip_install=True
basepython=python3.6
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not in favor of pinning the lint target to Python 3.6,
because tox -e lint will not work for contributors who do not have Python 3.6 installed.

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 don't really understand the motivation for this change myself. Perhaps the goal was to prevent linting tools from running under Python 2.7 if Tox was installed with that Python version? I'll remove it.

tox.ini Outdated
lint: diff-cover==0.9.12
lint: pycodestyle

black: black>=19.3b0
Copy link
Contributor

Choose a reason for hiding this comment

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

Further down in the deps section, you have: black==20.8b1.
Might be good to keep this consistent

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I'll clean this up.

Copy link
Contributor

@rodrigc rodrigc left a comment

Choose a reason for hiding this comment

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

Reference to pycodestyle should be removed from README.rst.

Might want to add reference to tox -e black there.

@rodrigc
Copy link
Contributor

rodrigc commented Sep 12, 2020

If a user files a PR, and their PR does not match black's coding style,
how will they, or a code reviewer know that they need to run tox -e black to fix things up?

@twm
Copy link
Contributor Author

twm commented Sep 12, 2020

@rodrigc Thank you for the review! I believe that I have addressed all your comments.

Changes

I consolidated the Tox configuration to have a factor-based style throughout. This seemed to be most similar to what was already there. I'd contrast the factor-based style with the explicit [testenv:foo] style used in Klein. I did lift the [default] section technique for variables from Klein, though.

I removed some detritus for a pyflakes Tox environment. There is also some stuff for a wheel factor (not the same as the wheels factor!), but I'm not sure enough about that to remove it, and that's really out of scope for this PR, anyway.

I added a GitHub Actions workflow that runs the linting tools. I like GHA for this because it is lower-latency than Travis, though the log UI is more difficult to use. I also filed #9953 to do more of this.

Merge Procedure

There is a bit of instability in Black's output on one file that'll need to be worked around when running it (filed upstream here), so the procedure on merge will be:

tox -e black-reformat -- --fast src/twisted/words/test/test_irc.py
tox -e black-reformat -- --fast src/twisted/words/test/test_irc.py
tox -e black-reformat

Running the tool twice with --fast is the workaround suggested in the Black ticket I linked. I have verified that it works for this file.

Overall:

  1. Run Black (as above)
  2. Commit the result; push to trunk. No review is required because it is the output of a tool.
  3. Create a PR that adds the above commit to .git-blame-ignore-revs and makes the lint GHA build required.


To automatically format code according to the coding standards::

$ tox -e black-reformat
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice, this gives clear instructions on how to reformat the code.

@@ -76,18 +76,19 @@ Indentation is 4 spaces per indent.
Tabs are not allowed.
It is preferred that every block appears on a new line, so that control structure indentation is always visible.

Lines are flowed at 79 columns.
Lines are flowed at 88 columns per `The Black Code Style <https://github.com/psf/black/blob/master/docs/the_black_code_style.md>`_.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is OK. In future, we may want to remove all mention of number of columns, etc.
from this document, and just say that Twisted follows the style guidelines as followed by Black, and just give a pointer Black's guidelines. In the end, very few people will read this anyways.

As long as they are told during review time to run tox -e black-reformat to make their
PR acceptable, that is probably good enough.

@rodrigc
Copy link
Contributor

rodrigc commented Sep 13, 2020

Might want to add the required invocation of black to the wiki here: https://twistedmatrix.com/trac/wiki/TwistedDevelopment#GettingYourPatchAccepted


runs-on: ubuntu-20.04
env:
TOXENV: "lint,black,manifest-checker,twine"
Copy link
Contributor

Choose a reason for hiding this comment

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

Adding this GitHub action which invokes black is good.

@@ -148,20 +165,10 @@ commands =
[testenv:apidocs]
deps=https://github.com/twisted/pydoctor/archive/3f9c64829dfa040b334c9ae27c332c7078356e79.zip

[testenv:manifest-checker]
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor

@rodrigc rodrigc left a comment

Choose a reason for hiding this comment

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

I think this looks ok.

  1. You have a GitHub Action which invokes black, so we can get feedback from CI
  2. tox -e black runs the check, and tox -e black-reformat can be run by a contributor to reformat their code

So I would say merge this, and let's get this going.

In follow-up items, I would say:

  1. Make a follow-up PR which reformats the tree. I can quickly review/approve
  2. Make a follow-up PR which adds a value to .git-blame-ignore-revs. I can quickly review/approve
  3. Send a e-mail to the mailing list, informing that coding style has changed to black, and give pointers to what a user should do to make their existing PR's acceptable to the new coding style.
  4. The docs regarding coding style should be cut to the bare minimum, and just have pointers to the Black guidelines. Also, just mention in the docs that tox -e black-reformat must be run to make any contribution acceptable.
  5. Take a pass through the wiki, and add references to black as appropriate, such as: https://twistedmatrix.com/trac/wiki/TwistedDevelopment#GettingYourPatchAccepted
  6. Do we need to add Black to the Contributor Checklist: that is displayed for ever PR?

@twm twm merged commit c5fbd88 into trunk Sep 13, 2020
@twm twm deleted the 9512-black-v2 branch September 13, 2020 05:06
@twm twm mentioned this pull request Sep 13, 2020
5 tasks
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

3 participants