Hidden tips to write flake8 and black compatible code #1466
Replies: 5 comments 9 replies
-
How to break long statements in python, including if statements I will begin by this tricky error when writing this bit of code :
which makes Flaky blurp: setup.py:61:5: E125 continuation line with same indent as next logical line You can also try as an alternative, and let's simply the variable names for clarity:
Flaky, again, blurps. Let's try the following, since according to PEP8, long lines should be placed in parentheses. When using parentheses, the lines can be broken up without using backslashes. You should also try to put the line break before boolean operators.
Flaky blurps (at least, they don't vomit) Solution to make Flaky not blurping is:
|
Beta Was this translation helpful? Give feedback.
-
@sebastien-lenard The preferred method for line continuation is, as you say, to use parenthesis. Your first example would be, my_variable_has_a_so_long_and_incredible_name = 1
no_mine_is_much_longer_than_yours_as_you_can_see = 2
c = 0
if (
my_variable_has_a_so_long_and_incredible_name == 1
and no_mine_is_much_longer_than_yours_as_you_can_see == 2
):
c = 1 Your second example should just be on a single line, a = 1
b = 2
c = 0
if a == 1 and b == 2:
c = 1 This is how black would reformat it and should satisfy flake8. |
Beta Was this translation helpful? Give feedback.
-
How to write tests in docstrings which respect the 79 character limit imposed by flake8 When you write your unit tests in your docstrings of your methods, sometimes, the return expected make more than 79 characters. For instance this test:
But actually, it is the value returned that pytest will consider and not the string written. So you can shorten your line, with the same result, this way:
|
Beta Was this translation helpful? Give feedback.
-
This isn't quite right. It really is the printed string that pytest is looking at. In your example the second case passes because we set the NORMALIZE_WHITESPACE option (this is set in config.cfg). With this option turned on, pytest matches one whitespace character with one or more whitespace characters. Even with this option on, the following test would (possibly suprisingly) fail, >>> g.at_node["flow__upstream_node_order"]
array([4, 8, 7, 9, 16, 17, 23, 10, 5, 14, 13, 15, 21, 22, 27,
26, 20,
19, 25, 18, 11, 24, 29, 30, 12, 6, 28, 32, 33, 34, 35, 36, 31,
0, 1, 2, 3]) Notice the lack of a space before the first 4 (i.e. In setup.cfg we set flake8's maximum line length to 88 characters, not the default 79. Maybe you are using a different linter? |
Beta Was this translation helpful? Give feedback.
-
Reformatting of np.arrays written over 2 lines into arrays written over tens of lines within test_xxx.py files using black It occurs that my array on two lines:
is formatted by black into an array of 29 lines ... not sure that makes the code more readable ...
Result: my initial test files was at +/- 500 lines passed to a size of 3,000 lines ! |
Beta Was this translation helpful? Give feedback.
-
Hi, this discussion to share tips that are not so obvious to write flake8 and black compatible code for landlab.
Well to write black compatible code, it's easy since you just have to run black on your modified files. It automatically format files. More on black current and future style format: https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html
flake8 is a more capricious person. Again you run them on your modified files and they offer you a splendid report of several lines. You can have severe rants with Flaky, because Flaky don't take in charge code modification, probably because in some cases, modification of ambiguously written codes (especially with indents) can alter what you initially want your code to do. More on Flaky's big words: https://flake8.pycqa.org/en/latest/user/error-codes.html
Beta Was this translation helpful? Give feedback.
All reactions