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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch to code formatter Black #267
Conversation
- Add/remove trailing commas in some collections to ensure/prevent Black explodes the contents to a single item per line. - Wrap the `*_ATTRIBUTES` constants at the top of `pydot.py` in `# fmt: off/on` pragmas to prevent they get exploded. - Merge some string literals that Black will put on a single line anyway. - Two unrelated spelling and comment formatting corrections.
[Black, The Uncompromising Code Formatter][1] promises to save the developers of `pydot` time and mental energy by taking control over code formatting. With this commit, we will start to find out. With `black` installed, you can now run `black .` in the top `pydot` directory and any formatting issues in pydot source files should get corrected automatically: ~/Development/pydot $ black . reformatted dot_parser.py reformatted setup.py reformatted test/pydot_unittest.py reformatted pydot.py All done! 4 files reformatted. Check out the documentation for more on the [Black code style][2] and options for [integrating it with your editor or git pre-commit][3]. This commit contains: - New `pyproject.toml`: Configuration of `black`. - Changed `.py` files: Results of the first run of `black` 21.5b2. Line length is kept at 79 characters for now. This is my personal preference because it allows for reasonable font size when using a phone, side-by-side diffs on a small laptop screen or 3-way diffs on larger screens. For easier review of the formatting changes made by `black`, this commit uses the `--skip-string-normalization` flag to prevent that single quotes are converted to double quotes already. The next commit will drop that flag, so the results of that conversion are shown there. A later commit will take care of the CI integration. Thanks to Kostis Anagnostopoulos for first suggesting `black` and to Sebastian Kalinowski and Hrishikesh Terdalkar for giving their reviews as well. Alternatives that were rejected: - `yapf`: Not clearly better formatting than `black`. More configuration options, but the cost of reaching agreement on all those options might offset the benefits of using a code formatter. - `autopep8`: Focusses mainly on PEP 8 compliance, meaning that a lot of other formatting issues would still need to be decided on manually, reducing the benefits of using a code formatter. As discussed in [pydot#265][265] and [pydot#267][267]. [1]: https://pypi.org/project/black/ [2]: https://black.readthedocs.io/en/latest/the_black_code_style/current_style.html [3]: https://black.readthedocs.io/en/latest/integrations/index.html [265]: pydot#265 [267]: pydot#267
Let `black` take care of the quotes as well, meaning a switch from single quotes to double quotes (except for where it would result in more backslash escapes). Currently, `black` can only be configured to either enforce double quotes or skip this altogether, not to enforce single quotes. I had some doubts about this switch at first, but noticed that several other projects and their documentation examples have switched to using double quotes now. Holding on to a consistent use of single quotes could become more of a burden, while offering only little benefit. Changes from single quotes to double quotes that were made manually: - Examples in `README.md`. - `pydot.py`, the `*_ATTRIBUTES` constants at the top, because they are surrounded by `# fmt: off` and `# fmt: on` pragmas. Note this is all about quotes around Python strings in the pydot source code and not at all related to quotes that are part of a DOT string.
Usage of `extras_require` suggested by Kostis Anagnostopoulos: - pydot#265 (comment)
(haven't look at the PR code yet) As i've written before, double-quotes is like "sealing" the code, guaranteed that no human has touch it since black last run. I agree the line-length is a choice the developers should be allowed to make, because certain programms may need too frequently longer lines. For instance, assuming the next line is too long:
... then it becomes:
... and the last comma(,) prevents it from folding back, even if
I guess the same behavior may undo some of the -sh flexibility (if it ever existed in |
|
Yeah, this bothered me as well. There is an option
If we are talking about the same thing (-ish flexibility == different behavior in the 10% tolerance zone of the maximum line length as discussed in #265 (comment)) then as far as I can tell, that does not exist at all in But I think I see what you mean: The way most humans would understand "-ish flexibilty" is that there would be room for the human to decide whether to enter that tolerance zone or not, and then not be corrected by
Thanks 馃槃 And thank you for reviewing, btw, I really appreciate it!
A pleasant side effect of changing my mind on double quotes so late. 馃槄
Ok, I tried putting the So, as you suggest, I moved the job for Here are two examples using a configuration like that:
Both builds are marked as "Failed" by Travis CI. The change is in the new commit 9b3d2b7b and I will squash it with the old one de074105 at merge time using the commit message of the new one.
Yeah, I don't know what I can do about that. Dropping the Also, code changes filed by beginners are usually quite small, right, so then the diff from |
Totally agree. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, I think this is a very well prepared commit, and will (hopefully) put a rest to further formatting concerns/discussions.
@@ -12,14 +12,25 @@ | |||
import sys | |||
|
|||
from pyparsing import ( | |||
nestedExpr, Literal, CaselessLiteral, | |||
Word, OneOrMore, | |||
nestedExpr, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One change that according to me is not optimal, if we think about "better readability".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Black does this to minimize diffs, but when it results in a lot of lines it becomes disturbing. The alternative would be to wrap it in # fmt: off
and # fmt: on
pragmas, just like I did for the *_ATTRIBUTES
constants at the top of pydot.py
. However, I also wanted to keep the number of such exceptions low and thought the problem here (from 8 lines to 19 lines) was not big enough to warrant another exception. Just one of the things we have to accept as the price for switching to black
, was my conclusion.
|
||
assignment = (ID + equals + righthand_id).setName("assignment") | ||
stmt = (assignment | edge_stmt | attr_stmt | | ||
subgraph | graph_stmt | node_stmt).setName("stmt") | ||
stmt = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar concern, but these cases might be more limited than imports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Also not big enough to warrant an exception, though.
@@ -50,7 +50,7 @@ start with. Here are 3 common options: | |||
```python | |||
import pydot | |||
|
|||
graphs = pydot.graph_from_dot_file('example.dot') | |||
graphs = pydot.graph_from_dot_file("example.dot") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Double quote changes are okay, I don't have a strong opinion about quotes either way and if anything, uniformity in this regard would be better enforced by Black
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remind that uniformity in the quotes reduces commit diffs - nowdays there is no code outside of git.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uniformity in the quotes reduces commit diffs
As long as people do not touch existing quotes when they make code changes, it should not matter, right? Or you mean when someone makes a code change and at the same time decides to "clean up" the quoting style around that area (as a form of opportunistic refactoring) and then combines it all together in just a single commit? In that case, yes, it would lead to larger diffs and black
prevents that.
This adds a new job for running `black` to our Travis CI continuous integration configuration. See the previous commits for more details on Black. If `black` finds a problem with the formatting, its job will be marked as "Failed". A diff of the required changes can be found on Travis CI by clicking on job `black` and scrolling down the Job log. You may also run `black` on your local machine to let it make the corrections for you. The new job is added to the default stage (`test`), meaning it will run alongside the regular test suite jobs. `black` is kept separate from the test suite, because it only needs to run once, not on multiple Python versions and architectures. A failure reported by `black` will not stop the test suite jobs from running, but will result in the build as a whole to be marked "Failed" in the end, even if the other jobs all passed. Using a separate Travis CI "stage" (named `lint`) was attempted, but considered inadequate: - Running stage `lint` *after* stage `test` meant long waiting for what was actually the fastest job. - Running stage `lint` *before* stage `test` meant a minor formatting issue could prevent the test suite from running at all. - Defining stage `lint` as an `allowed_failure` meant its outcome would become irrelevant for the outcome of the build as a whole, i.e. the build would pass even if `lint` had failed.
9b3d2b7
to
479bdca
Compare
Ok, merged, thanks guys! |
Proposal for the introduction of the Black code formatter, which promises to save the developers of
pydot
time and mental energy by taking control over code formatting.See the earlier discussion in #265 and the individual commit messages in this PR pydot/pydot#267.
@ankostis @prmtl @hrishikeshrt Ok, I gave in on the double quotes... but not on the line lengths yet. 馃槃 Hope you can have a look and let me know what you think.