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 black for python codestyle #386
Conversation
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.
Looks great! Thanks!
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.
The configuration file has a special purpose. Most changes it it don't look enhancements to me.
conf.py
Outdated
@@ -18,14 +18,14 @@ | |||
# If extensions (or modules to document with autodoc) are in another directory, | |||
# add these directories to sys.path here. If the directory is relative to the | |||
# documentation root, use os.path.abspath to make it absolute, like shown here. | |||
#sys.path.insert(0, os.path.abspath('.')) | |||
# sys.path.insert(0, os.path.abspath('.')) |
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'm not sure this is an enhancement.
This is not a human-readable comment as above lines, but a commented out sample. Currently you need to press DEL only once for switching it on. With your changes you will need to press DEL twice, and it will be harder to distinguish it from the above comment.
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 agree.
conf.py
Outdated
@@ -111,61 +111,61 @@ | |||
html_sidebars = { | |||
# Defaults taken from http://www.sphinx-doc.org/en/stable/config.html#confval-html_sidebars | |||
# Removes the quick search block | |||
'**': ['localtoc.html', 'globaltoc.html', 'relations.html', 'customsourcelink.html'], | |||
'**': ['localtoc.html', 'globaltoc.html', 'relations.html', 'customsourcelink.html'] |
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.
Trailing comma makes the dict easier for modification.
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.
and hence is considered good practice.
conf.py
Outdated
u'Python Developer\'s Guide Documentation', | ||
u'Brett Cannon', | ||
'manual', | ||
) |
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.
Trailing comma makes the list easier for modification.
And I'm not sure that writing 5-tuple vertically adds readability (especially if add yet few 5-tuples) .
tools/rstlint.py
Outdated
|
||
|
||
def main(argv): | ||
usage = '''\ | ||
usage = ( |
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.
What is the purpose of this change?
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 presume to have a closing ')' that lines up with 'usage' and 'try', as in some C styles with '}'. But unneeded (...) around Python expressions is not typical style.
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.
Going to change this back to the original which I agree is cleaner.
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.
Multiline strings are one of the two places in the Python grammar where significant indentation is broken. Black tries to avoid unnecessary parentheses but is easily fooled with multiline strings that aren't aligned with the current indentation level. This is a technical limitation more than conscious design.
I think I'm with Serhiy here: the utility of code formatters like black is to handle cases where the formatting really doesn't matter, and there's money to be saved by getting your developers to spend less time arguing about trivialities in a code review. I don't think "the formatting doesn't really matter" holds in the case of documentation examples - you'll often want to format things in particular ways in order to highlight specific lines and constructs, and that formatting may not match what you might do in "real" code. |
conf.py
Outdated
@@ -18,14 +18,14 @@ | |||
# If extensions (or modules to document with autodoc) are in another directory, | |||
# add these directories to sys.path here. If the directory is relative to the | |||
# documentation root, use os.path.abspath to make it absolute, like shown here. | |||
#sys.path.insert(0, os.path.abspath('.')) | |||
# sys.path.insert(0, os.path.abspath('.')) |
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 agree.
conf.py
Outdated
@@ -111,61 +111,61 @@ | |||
html_sidebars = { | |||
# Defaults taken from http://www.sphinx-doc.org/en/stable/config.html#confval-html_sidebars | |||
# Removes the quick search block | |||
'**': ['localtoc.html', 'globaltoc.html', 'relations.html', 'customsourcelink.html'], | |||
'**': ['localtoc.html', 'globaltoc.html', 'relations.html', 'customsourcelink.html'] |
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.
and hence is considered good practice.
tools/rstlint.py
Outdated
@@ -221,8 +312,10 @@ def main(argv): | |||
else: | |||
for severity in sorted(count): | |||
number = count[severity] | |||
print('%d problem%s with severity %d found.' % | |||
(number, number > 1 and 's' or '', severity)) |
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.
This is a standard way to write this.
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.
We follow PEP 8's "operators go in the front" revision from 2016. All code everywhere goes against this style because even PEP 8 used to advise the other way round. pycodestyle even has a warning about this (W503). I don't feel string interpolation is special enough to break the rules.
tools/rstlint.py
Outdated
|
||
|
||
def main(argv): | ||
usage = '''\ | ||
usage = ( |
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 presume to have a closing ')' that lines up with 'usage' and 'try', as in some C styles with '}'. But unneeded (...) around Python expressions is not typical style.
Hi folks, Thanks for the review and the comments. I expected there to be opinions on these changes since it is style ;-) A few general comments:
Thanks all 😄 |
tools/rstlint.py
Outdated
(number, number > 1 and 's' or '', severity)) | ||
print( | ||
'%d problem%s with severity %d found.' | ||
% (number, number > 1 and 's' or '', severity) |
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 would rewrite number > 1 and 's' or ''
as 's' if number > 1 else ''
.
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 agree 👍
FWIW removing trailing commas from indented single-element collection literals is a bug: psf/black#274 I will have it fixed for the next release. |
Thanks folks for the review. I've gone back and made most of the suggested changes by @terryjreedy and @serhiy-storchaka. |
tools/rstlint.py
Outdated
@@ -138,6 +226,7 @@ def main(argv): | |||
-s sev only show problems with severity >= sev | |||
-i path ignore subdir or file path | |||
'''% argv[0] |
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.
It is worth to add a space before %
.
conf.py
Outdated
@@ -29,8 +29,8 @@ | |||
|
|||
# Add any Sphinx extension module names here, as strings. They can be extensions | |||
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones. | |||
extensions = ['sphinx.ext.intersphinx', 'sphinx.ext.todo'] | |||
intersphinx_mapping = {'python': ('https://docs.python.org/3', None)} | |||
extensions = ['sphinx.ext.intersphinx', 'sphinx.ext.todo',] |
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.
Trailing comma has a value when the list is aligned vertically (and the trailing comma is followed by a newline instead of ]
). You can easy comment out or copy-and-edit the last line by pressing just a few keys in many editors. But if the list is written in a single line, the trailing comma rather looks distractive to me.
@terryjreedy and @serhiy-storchaka I've made all recommended changes from your review with the exception of one place where we were in split agreement. If I don't hear any objection in a couple of days, I'm going to go ahead and merge. Thanks for the reviews:sunny: |
My comment was related actually to all newly added trailing commas. |
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.
Misc style bikeshedding.
conf.py
Outdated
u"Python Developer's Guide Documentation", | ||
[u'Brett Cannon'], | ||
1, | ||
), | ||
] |
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.
Any reason to keep the u
prefix around, especially since it's only used on some of the strings?
I would also put the square brackets and parentheses on the same line (i.e. [(
and )]
) and remove a level of indentation.
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.
Black doesn't hug multiple levels of brackets to make it trivial to scan logical nesting levels visually.
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.
The main issue is the fact that 3 lines are "wasted" for parentheses/brackets. The argument is also similar to the one I made below. I would either use:
man_pages = [('index', 'pythondevelopersguide',
u"Python Developer's Guide Documentation",
[u'Brett Cannon'], 1)]
This takes 1/3 of vertical space compared to the "expanded" version (but I would only use it in case of a single tuple) or
man_pages = [
('index', 'pythondevelopersguide',
u"Python Developer's Guide Documentation",
[u'Brett Cannon'], 1),
]
This takes roughly half of the vertical space, and I would use it if I want/need more horizontal space or have (or planning to add) more tuples.
It also depends on the context: if it's a module-level constant I can splurge a bit and waste more vertical space, if it's a variable or a function call in the middle of a 10-lines function, I would avoid making the function twice as long if possible. The fact that the style is context-dependent is also the reason why I generally avoid using tools that alter the formatting I used.
58f6470
to
347da02
Compare
* blacken python files * add badge for codestyle * make all dicts and lists consistent with a trailing comma * edits per @serhiy-storchaka and @terryjreedy review * Edit per @serhiy-storchaka review * Edit per @serihy-storchaka clarification * Remove unneeded unicode u
There are only a few Python files in the devguide repo. I've run them through the Black codestyle formatter for easier readability.