PEP 8:E128 有时需要空格,有时不需要

发布于 2025-01-11 13:14:21 字数 1093 浏览 0 评论 0原文

这篇帖子的答案建议 PEP 8: E128 当第一行后面的行全部用括号括起来时,需要在行上有空格。但是,对于 if 语句,情况似乎并非如此。您可以看到前三行没有警告,而以下行则有警告,因为它们确实有空格:

我错过了什么吗?如果有帮助的话,我正在使用 Pycharm 社区版。

使用的代码:

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()

编辑:由于存在一些混乱,这应该是正确的缩进: https:// i.sstatic.net/SOpip.png 这是 Pycharm 认为正确的一个 https://i.sstatic.net/z5oUr.png。除非我弄错了(可能是这种情况!)。

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!).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木緿 2025-01-18 13:14:21

PEP-8 注意到if ( 提供了一个自然的 4 个空格缩进,允许在括号之后开始延续:

if (";" not in user_input
    and "DROP " not in user_input
    ...

您的检查器显然没有请注意您是否

if (";" not in user_input
   and "DROP " not in user_input
   ...

改为写入,但是,如果您确实使用该缩进,则必须继续使用相同的缩进您的FROM行是。第一条偏离的线这种情况


很常见,PEP 甚至提供了一个链接到 本段

if 语句的条件部分足够长以要求
它被写成多行,值得注意的是
两个字符关键字的组合(即 if),加上一个空格,
加上左括号会创建一个自然的 4 个空格缩进
多行条件语句的后续行。这可以产生一个
与嵌套在其中的缩进代码套件存在视觉冲突
if 语句,自然也会缩进 4 个空格。这
PEP 对于如何(或是否)进一步视觉化没有明确立场
将此类条件行与内部的嵌套套件区分开来
if 语句。在这种情况下可接受的选择包括,但
不限于:

# 没有额外的缩进。
如果(this_is_one_thing 和
    那是另一件事):
    做某事()

# 添加注释,这将为编辑器提供一些区别
# 支持语法高亮。
如果(this_is_one_thing 和
    那是另一件事):
    # 由于两个条件都成立,我们可以进行 frobnicate。
    做某事()

# 在条件延续行上添加一些额外的缩进。
如果(这是一个东西
        那是另一件事):
    做某事()

(另请参阅关于是否在二进制之前或之后中断的讨论
下面的运算符。)

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 the
if-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.)

嗫嚅 2025-01-18 13:14:21

带有“FROM”的行比之前的行缩进1个空格。这就是触发警告的原因。

The line with "FROM" is indented 1 space more than the line before. That's what's triggering the warning.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文