简体   繁体   English

如何为flake8静态确定源文件是否支持python 3?

[英]How to statically determine whether a source file supports python 3, for flake8?

How can I statically determine whether a Python source file supports Python 3? 如何静态确定Python源文件是否支持Python 3?

The question may be impossible to answer as stated there, so I'll be more specific: 如此处所述,该问题可能无法回答,因此我将更加具体:

My application for this is to pick a version of flake8 to run that won't give spurious SyntaxErrors. 我的应用程序是选择要运行的flake8版本,该版本不会产生虚假的SyntaxErrors。 For that reason, I'm looking for (at least) some heuristic that will tell me whether I should run a Python 3 copy of flake8, or a Python 2 copy. 因此,我正在寻找(至少)某种启发式的方法,该方法将告诉我是应该运行flake8的Python 3副本,还是应该运行Python 2副本。

I'm using flake8 from my editor, which shows me lint errors as I type, and I'm most concerned with the fact that sometimes I lose naming errors (such as F821), as a side effect of pyflakes/flake8 thinking that something is a syntax error that's in fact correct syntax: when it encounters a syntax error it (understandably) appears to give up with things like naming errors. 我正在使用我的编辑器中的flake8,它会显示我输入时出现的棉绒错误,而我最担心的是有时我会丢失命名错误(例如F821),这是pyflakes / flake8的副作用是一个语法错误,实际上是正确的语法:当遇到语法错误时,(似乎可以理解)它似乎放弃了命名错误之类的东西。

This is nigh impossible. 这几乎是不可能的。 There are way too many codepaths to test. 有太多的代码路径需要测试。

Moreover, code can be written to run on both Python 2 and 3, and flake8 doesn't always like the tricks used to make this possible, unless the project specifically tests with flake8 and has marked such sites to be excluded. 此外,代码可以写入到两个 Python 2和3运行,flake8并不总是像用来使这成为可能,除非项目特别是与flake8测试,并具有显着此类网站要排除的技巧。 So you could either have false positives (errors in both the Python 2 and Python 3 versions of flake8) or the code will simply work on Python 2 and 3 without any warnings. 因此,您可能会产生误报 (在flake8的Python 2和Python 3版本中均出现错误),或者代码将仅在Python 2和3上运行而不会发出任何警告。

You could use tox to manage version support for a given project instead; 您可以使用tox来管理给定项目的版本支持; have tox figure out what flake8 command to use (which may be multiple): 让tox找出要使用的flake8命令(可能是多个):

[tox]
envlist = py27,py35,flake8-27,flake8-35

# ...
[testenv:flake8-27]
basepython=python2.7
deps=flake8
commands=
flake8 projectdir

[testenv:flake8-35]
basepython=python3.5
deps=flake8
commands=
flake8 projectdir

and use tox -e flake8-27 or tox -e flake8-35 . 并使用tox -e flake8-27tox -e flake8-35

If you only care about SyntaxError (which you specifically mention in your question), you can simply try to compile the file with Python 2 and 3: 如果您只关心SyntaxError (在问题中特别提到),则可以尝试使用Python 2和3编译文件:

python -m compileall
python3 -m compileall

If either of these commands fails, you at least know that the code does not work with that Python version. 如果这些命令中的任何一个失败,那么您至少知道该代码不适用于该Python版本。 The reverse is of course not true: If the code compiles in a specific version of Python, that doesn't guarantee you that it will work correctly in that version of Python. 相反,当然不是这样:如果代码是在特定版本的Python中编译的,则不能保证您可以在该版本的Python中正常工作。 It just tells you there are no SyntaxError s. 它只是告诉您没有SyntaxError

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

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