flake8
-
rbtools/commands/diff.py (Diff revision 1) Show all issues -
-
-
-
-
-
-
-
-
Review Request #11829 — Created Oct. 3, 2021 and updated
Information | |
---|---|
sandysaji | |
RBTools | |
master | |
Reviewers | |
rbtools | |
Add an option to the
rbt diff
command to
apply basic colorization to the output.
Therbt diff --color-diff
command highlights
removed lines in red and added lines in green.This review request includes the following:
--color-diff
option to rbt diff
command.test_diff.py
that contains test cases for rbt diff
commandSummary |
---|
Description | From | Last Updated |
---|---|---|
E251 unexpected spaces around keyword / parameter equals |
![]() |
|
E251 unexpected spaces around keyword / parameter equals |
![]() |
|
E128 continuation line under-indented for visual indent |
![]() |
|
E128 continuation line under-indented for visual indent |
![]() |
|
E128 continuation line under-indented for visual indent |
![]() |
|
E128 continuation line under-indented for visual indent |
![]() |
|
E501 line too long (94 > 79 characters) |
![]() |
|
E124 closing bracket does not match visual indentation |
![]() |
|
E501 line too long (94 > 79 characters) |
![]() |
|
E501 line too long (84 > 79 characters) |
![]() |
|
E225 missing whitespace around operator |
![]() |
|
W291 trailing whitespace |
![]() |
|
E501 line too long (84 > 79 characters) |
![]() |
|
E251 unexpected spaces around keyword / parameter equals |
![]() |
|
E251 unexpected spaces around keyword / parameter equals |
![]() |
|
W291 trailing whitespace |
![]() |
|
W291 trailing whitespace |
![]() |
|
It might be nicer to do elif six.PY2 and then an else instead of having nested if else statements |
![]() |
|
F841 local variable 'diff' is assigned to but never used |
![]() |
|
E303 too many blank lines (2) |
![]() |
|
W292 no newline at end of file |
![]() |
|
Instead of just having this be a boolean, I think we should follow git's lead and have it be a … |
|
|
F841 local variable 'diff' is assigned to but never used |
![]() |
|
E225 missing whitespace around operator |
![]() |
|
E501 line too long (80 > 79 characters) |
![]() |
|
E225 missing whitespace around operator |
![]() |
|
F841 local variable 'diff' is assigned to but never used |
![]() |
|
W291 trailing whitespace |
![]() |
|
F841 local variable 'diff' is assigned to but never used |
![]() |
|
F841 local variable 'diff' is assigned to but never used |
![]() |
|
W503 line break before binary operator |
![]() |
|
W503 line break before binary operator |
![]() |
|
E999 SyntaxError: invalid syntax |
![]() |
|
Perhaps we should call this "display options" to distinguish it from Command.diff_options? |
|
|
Add a blank line between these two. |
|
|
This needs to tell us what the type is. |
|
|
Add a blank line between these two. |
|
|
Please put this blank line back. |
|
|
Indentation is a little bit funky here and things don't quite line up with the parens. I think it might … |
|
|
All test docstrings should start with the word "Testing" for consistency in output. |
|
|
Please add a blank line between these two |
|
|
E999 SyntaxError: invalid syntax |
![]() |
|
Please add a blank line between these two |
|
|
There's an extra space at the end of this docstring. |
|
|
Let's use single quotes instead of double here. |
|
|
This should be (list of unicode, optional) |
|
|
I don't think this adds useful info--the first line of explanation is sufficient. |
|
|
DIFF is incorrectly capitalized here. |
|
|
Please add a blank line between these two. |
|
|
This needs a docstring. It probably should also be prefixed with _ |
|
|
Please add a blank line between these two. |
|
|
F401 'pygments.highlight' imported but unused |
![]() |
|
F401 'rbtools.commands.CommandError' imported but unused |
![]() |
|
W293 blank line contains whitespace |
![]() |
|
These should be grouped together with full-package imports coming before from imports. Please also alphabetize them (formatters before lexers). import … |
|
|
Let's give this method an imperative name. Perhaps colorize_diff? |
|
|
Codebase docs right now still link to python2, so the type here should be listed as unicode rather than str. … |
|
|
Please dedent this 4 spaces (align with line above it) |
|
|
Please add a blank line between these two. |
|
|
Instead of using the character, put the entire conditional in parens: if (options.color_diff == self.COLOR_ALWAYS or (options.color_diff == self.COLOR_AUTO and … |
|
|
Standard-library imports should go in their own section just after the __future__ import. Order should be full-packages first, then from … |
|
|
This can be a lot simpler: self.assertIn(lines_removed_color_pattern, sample_diff_highlighted) self.assertIn(lines_added_color_pattern, sample_diff_highlighted) Might be nice to shorten some of the variable names … |
|
|
This blank line can go away |
|
|
six isn't part of the standard library, so this should go in the next import group. |
|
|
This description could be more accurate. |
|
|
This should end with a period. |
|
|
Align the sys on the same column as options above it. |
|
|
Test -> Tests |
|
|
Why the .encode() calls? We should be able to operate entirely in unicode-land. |
|
|
F401 'pygments.highlight' imported but unused |
![]() |
|
These are both considered third-party modules, so should be in the same import group (no blank line here). |
|
|
This should also have an added_in='3.0' parameter, to allow the documentation to reflect when this option was first made available. |
|
|
This should have a trailing period. |
|
|
I feel this should go somewhere lower than main(). It should also probably be a private function. |
|
|
Looks like this wraps a bit early. The "or" can easily fit on the previous line. |
|
|
This can be one statement. |
|
|
These should ideally use keyword arguments. |
|
|
DiffLexer() is a good default, but something you can do now that this change is basically complete is support pydiffx's … |
|
|
This can easily be one statement. |
|
|
Unit tests should focus on testing one condition at a time. So instead of looping through a set of possible … |
|
|
Looks like this is wrapping prematurely. More can fit on the first line. |
|
|
Test docstrings should be in the form of: Testing <thing>[ with <conditions>]. So, Testing rbt diff with --color-diff=.... |
|
|
Small nit: For multi-line strings like this, put the trailing ) on its own line (aligned with the start of … |
|
|
It's a small nit, but we use %-formatting instead of .format() in our code. |
|
|
We try to avoid the multi-line with form. Let's use nested with blocks instead. |
|
|
As above, Testing <thing>[ with <conditions>]. In this case, <thing> would be Diff.colorize_diff |
|
|
Unwanted blank line here. |
|
|
+= is faster than .extends in Python. Let's move to that. |
|
|
This is more generally useful, and we don't want to maintain a bunch of copies of it. Let's move it … |
|
|
W292 no newline at end of file |
![]() |
|
W292 no newline at end of file |
![]() |
|
No reason to create a variable and then return it. Let's just return highlight(...) |
|
|
Why is the extend() version commented out? That's generally a better choice than +=. |
|
rbtools/commands/diff.py (Diff revision 1) |
---|
Commits: |
|
||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 2 (+66 -20) |
Commits: |
|
||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 3 (+74 -22) |
Commits: |
|
||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 4 (+77 -25) |
Overall looks good :)
rbtools/commands/diff.py (Diff revision 4) |
---|
It might be nicer to do
elif six.PY2
and then anelse
instead of having nested if else statements
Commits: |
|
||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 5 (+169 -29) |
rbtools/commands/tests/test_diff.py (Diff revision 5) |
---|
F841 local variable 'diff' is assigned to but never used
Commits: |
|
||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 6 (+169 -33) |
rbtools/commands/tests/test_diff.py (Diff revision 6) |
---|
F841 local variable 'diff' is assigned to but never used
rbtools/commands/diff.py (Diff revision 6) |
---|
Instead of just having this be a boolean, I think we should follow git's lead and have it be a choice between "never", "always", and "auto" (with auto being the default). When using auto, we should check
sys.stdout.isatty()
to determine whether to use color.
Commits: |
|
||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 7 (+209 -49) |
rbtools/commands/tests/test_diff.py (Diff revision 7) |
---|
F841 local variable 'diff' is assigned to but never used
Commits: |
|
||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 8 (+213 -51) |
rbtools/commands/tests/test_diff.py (Diff revision 8) |
---|
F841 local variable 'diff' is assigned to but never used
Commits: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 9 (+214 -52) |
rbtools/commands/tests/test_diff.py (Diff revision 9) |
---|
F841 local variable 'diff' is assigned to but never used
New test cases and new screenshots. Minor updates to the
diff_colorization
function.
Description: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Testing Done: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Commits: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Diff: |
Revision 10 (+340 -74) |
rbt_diff.png: |
| ||||
---|---|---|---|---|---|
rbt_diff_--color.png: |
|
Added Files: |
---|
New test cases and new screenshots. Minor updates to the diff_colorization function.
Fixed flake8 errors.
Commits: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 11 (+346 -78) |
This is mostly a bunch of small trivial issues with formatting. I do have one question about string types in the comment on line 100/139.
What is left to do for this change? Should the
[WIP]
tag be removed?
rbtools/commands/diff.py (Diff revision 11) |
---|
Perhaps we should call this "display options" to distinguish it from
Command.diff_options
?
rbtools/commands/diff.py (Diff revision 11) |
---|
Indentation is a little bit funky here and things don't quite line up with the parens. I think it might be a little easier to read if we swapped the order:
if (self.options.color_diff == self.COLOR_ALWAYS or (self.options.color_diff == self.COLOR_AUTO and sys.stdout.isatty())):
Note how we align with the parens for both cases.
I also have a question here: On Python 2, we can be pretty freewheeling with string types, but things are stricter on Python 3. Does Pygments return
str
orbytes
?
rbtools/commands/tests/test_diff.py (Diff revision 11) |
---|
All test docstrings should start with the word "Testing" for consistency in output.
rbtools/commands/tests/test_diff.py (Diff revision 11) |
---|
There's an extra space at the end of this docstring.
rbtools/commands/tests/test_diff.py (Diff revision 11) |
---|
Let's use single quotes instead of double here.
rbtools/commands/tests/test_diff.py (Diff revision 11) |
---|
I don't think this adds useful info--the first line of explanation is sufficient.
rbtools/commands/tests/test_diff.py (Diff revision 11) |
---|
This needs a docstring. It probably should also be prefixed with
_
Minor updates to code which addresses feedbacks provided on the review request.
Commits: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 12 (+380 -96) |
rbtools/commands/tests/test_diff.py (Diff revision 12) |
---|
F401 'pygments.highlight' imported but unused
rbtools/commands/tests/test_diff.py (Diff revision 12) |
---|
F401 'rbtools.commands.CommandError' imported but unused
Fixed issues rasied by Review Bot. Minor updates to code which addresses feedbacks provided on the review request.
Commits: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 13 (+378 -100) |
rbtools/commands/diff.py (Diff revision 13) |
---|
These should be grouped together with full-package imports coming before
from
imports. Please also alphabetize them (formatters before lexers).import six from pygments import highlight ...
rbtools/commands/diff.py (Diff revision 13) |
---|
Let's give this method an imperative name. Perhaps
colorize_diff
?
rbtools/commands/diff.py (Diff revision 13) |
---|
Codebase docs right now still link to python2, so the type here should be listed as
unicode
rather thanstr
. This line should also just be a type, not list any kind of variable name.
rbtools/commands/diff.py (Diff revision 13) |
---|
Instead of using the character, put the entire conditional in parens:
if (options.color_diff == self.COLOR_ALWAYS or (options.color_diff == self.COLOR_AUTO and sys.stdout.isatty())):
rbtools/commands/tests/test_diff.py (Diff revision 13) |
---|
Standard-library imports should go in their own section just after the
__future__
import. Order should be full-packages first, then from imports, and everything alphabetized. This also needs to usesix
in order to get the right StringIO implementation on both python 2 and 3 (which goes in its own import group as a "third party module"):from __future__ import unicode_literals import sys from contextlib import contextmanager from six.moves import cStringIO as StringIO from rbtools.commands.diff import Diff from rbtools.utils.testbase import RBTestBase
rbtools/commands/tests/test_diff.py (Diff revision 13) |
---|
This can be a lot simpler:
self.assertIn(lines_removed_color_pattern, sample_diff_highlighted) self.assertIn(lines_added_color_pattern, sample_diff_highlighted)
Might be nice to shorten some of the variable names too. Perhaps just
highlighted
,added_color
, andremoved_color
?
Ordered import statments, updated function name and test cases, and addressed feedback on review request.
Commits: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 14 (+400 -124) |
rbtools/commands/diff.py (Diff revision 14) |
---|
six
isn't part of the standard library, so this should go in the next import group.
rbtools/commands/tests/test_diff.py (Diff revision 14) |
---|
Why the
.encode()
calls? We should be able to operate entirely in unicode-land.
Minor code changes to address feedback on review request.
Commits: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 15 (+406 -128) |
Removed
.encode()
and updatedself.assertIn
.
Commits: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 16 (+410 -130) |
rbtools/commands/tests/test_diff.py (Diff revision 16) |
---|
F401 'pygments.highlight' imported but unused
Removed unused imports.
Commits: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 17 (+409 -131) |
rbtools/commands/diff.py (Diff revision 17) |
---|
These are both considered third-party modules, so should be in the same import group (no blank line here).
rbtools/commands/diff.py (Diff revision 17) |
---|
This should also have an
added_in='3.0'
parameter, to allow the documentation to reflect when this option was first made available.
rbtools/commands/diff.py (Diff revision 17) |
---|
I feel this should go somewhere lower than
main()
. It should also probably be a private function.
rbtools/commands/diff.py (Diff revision 17) |
---|
Looks like this wraps a bit early. The "or" can easily fit on the previous line.
rbtools/commands/diff.py (Diff revision 17) |
---|
DiffLexer()
is a good default, but something you can do now that this change is basically complete is support pydiffx'spydiffx.integrations.pygments_lexer.DiffXLexer
whendiff.startswith(b'#diffx')
.Unit tests would be necessary to make sure that's handled right.
rbtools/commands/tests/test_diff.py (Diff revision 17) |
---|
Unit tests should focus on testing one condition at a time. So instead of looping through a set of possible options and checking results, you should have a unit test for each possible option, and document that in the docstring. When there's a regression, it becomes easier to spot the extent of the fallout.
You can have a private convenience function that does the bulk of the test and call it from each conditional test function.
rbtools/commands/tests/test_diff.py (Diff revision 17) |
---|
Looks like this is wrapping prematurely. More can fit on the first line.
rbtools/commands/tests/test_diff.py (Diff revision 17) |
---|
Test docstrings should be in the form of:
Testing <thing>[ with <conditions>]
.So,
Testing rbt diff with --color-diff=...
.
rbtools/commands/tests/test_diff.py (Diff revision 17) |
---|
Small nit: For multi-line strings like this, put the trailing
)
on its own line (aligned with the start of the variable name defining the string).You'll do this below as well.
rbtools/commands/tests/test_diff.py (Diff revision 17) |
---|
It's a small nit, but we use
%
-formatting instead of.format()
in our code.
rbtools/commands/tests/test_diff.py (Diff revision 17) |
---|
We try to avoid the multi-line
with
form. Let's use nestedwith
blocks instead.
rbtools/commands/tests/test_diff.py (Diff revision 17) |
---|
As above,
Testing <thing>[ with <conditions>]
. In this case,<thing>
would beDiff.colorize_diff
rbtools/commands/tests/test_diff.py (Diff revision 17) |
---|
+=
is faster than.extends
in Python. Let's move to that.
rbtools/commands/tests/test_diff.py (Diff revision 17) |
---|
This is more generally useful, and we don't want to maintain a bunch of copies of it. Let's move it to
rbtools.testing.testcase.TestCase
.
updated colorize_diff to private method, unit tests to focus on single test cases, and
_capture_sys_output
. Addressed feedbacks on review request.
Commits: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 18 (+464 -196) |
Updated colorize_diff to private method, unit tests to focus on single test cases, and _capture_sys_output. Addressed feedbacks on review request.
Commits: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 19 (+463 -195) |
rbtools/commands/diff.py (Diff revision 19) |
---|
No reason to create a variable and then return it. Let's just
return highlight(...)
rbtools/commands/tests/test_diff.py (Diff revision 19) |
---|
Why is the
extend()
version commented out? That's generally a better choice than+=
.
Added support for pydiffx and created test cases. Addressed issues logged.
Commits: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Diff: |
Revision 20 (+566 -204) |
Description: |
|
---|