简体   繁体   English

更好的替代 flake8 的“E902 TokenError: EOF in multi-line statement”

[英]better alternative to flake8's "E902 TokenError: EOF in multi-line statement"

I do understand the E902 error, however I do think it's a bit "messy".我确实理解 E902 错误,但我确实认为它有点“混乱”。 This is for 2 reasons:这有两个原因:

  • It feels to me that it should be F902 (a failure rather than an error).我觉得应该是 F902(失败而不是错误)。 After all, as I see it in Flake's world, errors are things that are wrong in the style guide (eg having 3 newlines in a row), whereas Failures are things that will not make the program run (eg referencing a variable before declaration).毕竟,正如我在 Flake 的世界中看到的那样,错误是样式指南中的错误(例如连续有 3 个换行符),而失败是不会使程序运行的东西(例如在声明之前引用变量) . In other words, when I save a python file in my IDE, I should be happy to ignore any errors (as long as I'm just hacking away), but want to be alerted of anything that is obviously a mistake (and not just me not caring about things).换句话说,当我在 IDE 中保存一个 python 文件时,我应该很乐意忽略任何错误(只要我只是在黑客攻击),但希望收到任何明显错误的警报(而不仅仅是我不在乎事情)。 Obviously writing not valid python is even worse than using a variable before declaring it (which in some cases, will actually run).显然,编写无效的python比在声明变量之前使用它(在某些情况下,实际上会运行)更糟糕
  • More importantly, I feel that it's very unhelpful that this error always points to line 1 character 1. This means it's up to me to start looking in the code (of files sometimes 1000s lines long) to the exact spot where I forgot to close a parenthesis, or run another tool (such as just running python on the file) to find the exact line.更重要的是,我觉得这个错误总是指向第 1 行字符 1 是非常无益的。这意味着由我开始查看代码(有时是 1000 行长的文件)到我忘记关闭的确切位置括号,或运行其他工具(例如仅在文件上运行python )以找到确切的行。 Also, this means that my editor (VIM) will not show any error for the line I'm working on;此外,这意味着我的编辑器 (VIM) 不会显示我正在处理的行的任何错误; I will only see the error if I scroll up all the way.如果我一直向上滚动,我只会看到错误。

I guess my questions are threefold:我想我的问题有三个:

  • Why is E902 an error instead of a failure (not super important, but if there's a rationale behind it' I'd be happy to learn it)为什么 E902 是错误而不是失败(不是非常重要,但如果背后有理由,我很乐意学习)
  • Is there a good reason that E902 error is always on line 1 col 1, or is this just that not enough resources have been spent (yet) to have it point to the right spot in the code). E902 错误总是出现在第 1 列第 1 行上是否有充分的理由,或者这仅仅是因为(尚未)花费足够的资源使其指向代码中的正确位置)。
  • Most importantly: is there an accepted / better way for this?最重要的是:有没有一种可以接受的/更好的方法? Perhaps a wrapper around flake8 that, in case of a E902 error uses some other way to find a more accurate spot (note that pyflakes, which is part of flake8, does find the correct line; probably not a good idea to run python since there does not seem to be a way to tell python to only parse the file but not actually run it.).也许是 flake8 的包装器,在出现 E902 错误的情况下,它会使用其他一些方法来找到更准确的位置(请注意,作为 flake8 的一部分的 pyflakes 确实找到了正确的行;运行python可能不是一个好主意,因为在那里)似乎不是告诉 python 只解析文件而不实际运行它的方法。)。 I'm sure I can create such a wrapper myself, but if other/smarter people already made one, I'd prefer to reuse their work.我确信我自己可以创建这样的包装器,但是如果其他/更聪明的人已经制作了一个,我更愿意重用他们的工作。

For completeness, the following code shows the issue:为了完整起见,以下代码显示了问题:

def x():
    pass


x(
a = 1
b = 2

Both pyflakes and python show an error in line 7 (which at least is very close to where the error is made), whereas flake8 will just show the error on 1:1 pyflakes 和 python 都在第 7 行显示错误(至少非常接近出错的地方),而 flake8 只会在 1:1 上显示错误

answering all three of the questions (usually separate questions should be separate questions on SO but...):回答所有三个问题(通常单独的问题应该是关于 SO 的单独问题,但是......):

  1. the E / F / W codes do not mean error / failure / warning -- the prefix is a coincidence. E/F/W 代码并不意味着错误/失败/警告——前缀是一个巧合。 F codes come from pyflakes, E and W codes come from pycodestyle (though there are two special E codes inherited from pycodestyle that remain -- E902 and E999 for OSError and SyntaxError -- I'd love to change this but the sheer number of people using flake8 makes breaking changes tricky) F 代码来自 pyflakes,E 和 W 代码来自 pycodestyle(尽管有两个从 pycodestyle 继承的特殊 E 代码仍然存在——用于 OSError 和 SyntaxError 的 E902 和 E999——我很想改变这一点,但人数众多使用 flake8 使重大更改变得棘手)

  2. it is col 1 line 1 because this error comes from the tokenizer and it reports the error out-of-bounds on the last line of the file.它是第 1 行第 1 行,因为此错误来自标记器,并且它在文件的最后一行报告越界错误。 the tokenizer is not very good about giving precise error locations分词器不太擅长给出精确的错误位置

  3. it is considered a bug, but nobody has volunteered to work on it yet, if I'm reading it correctly it should be a 2 line fix -- you can read more about the bug here: https://github.com/PyCQA/flake8/issues/740它被认为是一个错误,但还没有人自愿处理它,如果我正确阅读它应该是 2 行修复 - 您可以在此处阅读有关该错误的更多信息: https : //github.com/PyCQA /flake8/issues/740


disclaimer: I'm the current flake8 maintainer免责声明:我是当前的 flake8 维护者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM