Reputation: 588
This post's answer suggests PEP 8: E128 requires spaces on the lines following the first one when they're all wrapped inside parentheses. However, with an if
statement this doesn't seem to be the case. You can see that the first three lines do not have a warning and the following do because they do have a space:
Am I missing something? I am using Pycharm Community edition if that helps.
Code used:
def main(user_input):
if (";" not in user_input
and "DROP " not in user_input
and "SELECT " not in user_input
and "FROM " not in user_input
and "DELETE " not in user_input
and '"' not in user_input
and ";" not in user_input
and "=" not in user_input
and ">" not in user_input
and "<" not in user_input):
pass
else:
exit()
EDIT: as there is a bit of confusion, this should be the correct indentation: https://i.sstatic.net/SOpip.png and this is the one Pycharm thinks is the correct one https://i.sstatic.net/z5oUr.png. Unless I'm mistaken (could be the case!).
Upvotes: 3
Views: 396
Reputation: 530872
PEP-8 notes that if (
provides a natural 4-space indentation which allows a continuation to begin after the parenthesis:
if (";" not in user_input
and "DROP " not in user_input
...
Your checker apparently does not care if you write
if (";" not in user_input
and "DROP " not in user_input
...
instead, but, if you do use that indentation, you have to continue to use the same indentation. Your FROM
line is the first line to deviate from that indentation.
This is common enough that the PEP even provides an anchor to link to this paragraph:
When the conditional part of an
if
-statement is long enough to require that it be written across multiple lines, it's worth noting that the combination of a two character keyword (i.e.if
), plus a single space, plus an opening parenthesis creates a natural 4-space indent for the subsequent lines of the multiline conditional. This can produce a visual conflict with the indented suite of code nested inside theif
-statement, which would also naturally be indented to 4 spaces. This PEP takes no explicit position on how (or whether) to further visually distinguish such conditional lines from the nested suite inside the if-statement. Acceptable options in this situation include, but are not limited to:# No extra indentation. if (this_is_one_thing and that_is_another_thing): do_something() # Add a comment, which will provide some distinction in editors # supporting syntax highlighting. if (this_is_one_thing and that_is_another_thing): # Since both conditions are true, we can frobnicate. do_something() # Add some extra indentation on the conditional continuation line. if (this_is_one_thing and that_is_another_thing): do_something()
(Also see the discussion of whether to break before or after binary operators below.)
Upvotes: 2
Reputation: 2432
The line with "FROM" is indented 1 space more than the line before. That's what's triggering the warning.
Upvotes: 0