我使用Tox来运行单元测试,使用flake8命令检查代码格式错误.每次我在PyCharm中编码,我运行tox然后意识到我有一堆烦人的格式错误,我必须回来并手动修复.我希望PyCharm自动格式化代码(根据flake8 google,每次我停止输入后自动保存,对我来说.
我的tox testenv看起来像这样:
[testenv:flake8]
commands=flake8 <my_code_directory>
deps =
flake8==2.4.1
flake8-import-order==0.11
pep8-naming==0.4.1
[flake8]
max-line-length = 120
import-order-style = google
Run Code Online (Sandbox Code Playgroud)
这可能吗?我是否必须在某处下载特定的插件?如果没有flake8,那么PEP-8呢?
我使用 ALE 来管理我的 linting(使用 flake8)和代码格式(使用 black)。flake8 和 black 之间一个恼人的不兼容性是,当一行超过 80 个字符时,flake8 会出错,而 black 只能纠正超过 88 个字符的行。
所以我想将flake8的行长更改为88个字符,或者将black的行长更改为80个字符。
请注意,我没有独立于 ALE 安装 flake8,但我确实独立于 ALE 安装了 black。
我flake8与一堆插件一起使用(,,flake8-docstrings)。我已将它们全部预安装到.flake8-isortflake8-blackvenv
我的仓库要检查pre-commit:
pyproject.toml(配置black和isort)setup.cfg(配置flake8和pydocstyle)\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 foo\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pyproject.toml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 setup.cfg\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 (the package)\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 bar\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pyproject.toml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 setup.cfg\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 (the package)\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 venv\nRun Code Online (Sandbox Code Playgroud)\n我想为这两个包调用flake8via 。pre-commit
这是我目前的做法:
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 foo\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pyproject.toml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 setup.cfg\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 (the package)\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 bar\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 pyproject.toml\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 setup.cfg\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 (the package)\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 venv\n …Run Code Online (Sandbox Code Playgroud) 当使用多个检查或格式化 python 文件的工具时,有没有办法一次性设置行长度?
目前我有:
.flake8文件:
max-line-length = 120
Run Code Online (Sandbox Code Playgroud)
.isort.cfg文件:
line-length = 120
Run Code Online (Sandbox Code Playgroud)
.black文件:
line-length = 120
Run Code Online (Sandbox Code Playgroud)
.pylintrc文件:
max-line-length = 120
Run Code Online (Sandbox Code Playgroud) 有没有办法使用pep8与cython文件?
例如,pep8不适用于运营商.
getline(& line)
Run Code Online (Sandbox Code Playgroud)
产生错误:
E225 missing whitespace around operator
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试修复它并运行它:
getline( & line)
Run Code Online (Sandbox Code Playgroud)
产生错误:
E201 whitespace after '('
Run Code Online (Sandbox Code Playgroud) 我已在 Ubuntu 18.04 上使用的 VM 上正确安装了上述所有模块。在特定脚本或文件夹上运行它们中的任何一个时,它们都会正确识别样式错误并将其输出到控制台中。例如:
(venv) .../src$ python3.6 -m flake8
./free_prediction.py:8:1: E303 too many blank lines (5)
./free_prediction.py:8:28: E231 missing whitespace after ','
./free_prediction.py:10:5: E225 missing whitespace around operator
./free_prediction.py:12:3: E225 missing whitespace around operator
./free_prediction.py:15:13: E225 missing whitespace around operator
Run Code Online (Sandbox Code Playgroud)
但是,代码中仍然存在相同的样式错误。如何使它们自动更正?
我想忽略静态代码分析中的特定行。
对于 Flake8,我会使用语法# noqa: F401.
对于 pylint,我会使用语法# pylint: disable=unused-import。
当我正在开发代码生成框架时,我希望代码能够支持这两种 linter。有没有办法结合这两个指令,以便正确检测到它们?
https://www.aeracode.org/2018/02/19/python-async-simplified/
如果您调用非阻塞同步函数,则不会破坏您的一天,如下所示:
def get_chat_id(name):
return "chat-%s" % name
async def main():
result = get_chat_id("django")
Run Code Online (Sandbox Code Playgroud)
但是,如果您调用阻塞函数(例如 Django ORM),则异步函数内的代码看起来是相同的,但现在它是危险的代码,可能会阻塞整个事件循环,因为它不等待:
def get_chat_id(name):
return Chat.objects.get(name=name).id
async def main():
result = get_chat_id("django")
Run Code Online (Sandbox Code Playgroud)
您可以看到,如果程序员没有超级了解调用它的所有内容,那么很容易让一个非阻塞函数“意外地”变成阻塞。这就是为什么我建议您在没有安全执行或事先不知道它是非阻塞标准库函数(例如
os.path.join.
所以我正在寻找一种方法来自动捕获此错误的实例。Python 是否有任何 linter 会将异步函数内的同步函数调用报告为违规?
我可以配置 Pylint 或 Flake8 来执行此操作吗?
我不一定介意它是否也捕获了上面的第一种情况(这是无害的)。
更新:
在某种程度上,我意识到这是一个愚蠢的问题,正如米哈伊尔的回答所指出的那样。我们需要的是 linter 应该检测到的“危险同步函数”的定义。
因此,出于这个问题的目的,我给出以下定义:
“危险同步函数”是执行 IO 操作的函数。例如,这些操作必须由 gevent 进行猴子修补,或者必须包装在async函数中,以便事件循环可以进行上下文切换。
(我欢迎对这个定义的任何改进)
我在 python 中有一个问题。我原来的正则表达式是:
f"regex(metrics_api_failure\.prod\.[\w_]+\.{method_name}\.\d+\.\d+\.[\w_]+\.[\w_]+\.sum\.60)"
Run Code Online (Sandbox Code Playgroud)
(method_name是一个局部变量),我收到了 lint 警告:
"[FLAKE8 W605] invalid escape sequence '\.'Arc(W605)"
Run Code Online (Sandbox Code Playgroud)
这看起来建议我使用r作为正则表达式前缀。但如果我这样做:
r"regex(metrics_api_failure\.prod\.[\w_]+\.{method_name}\.\d+\.\d+\.[\w_]+\.[\w_]+\.sum\.60)"
Run Code Online (Sandbox Code Playgroud)
成为{method_name}字符串类型而不是传入的变量。
有谁知道如何解决这个困境?
我flake8在Visual Studio Code中使用,使用Python 3.6变量注释编写一些代码.到目前为止它没有任何问题,但我遇到了一个奇怪的警告.
这很好用:
style: str = """
width: 100%;
...
"""
# Doing sth with `style`
Run Code Online (Sandbox Code Playgroud)
这个也是:
img_style: str = """
width: 100%;
...
"""
# Doing sth with `img_style`
Run Code Online (Sandbox Code Playgroud)
但是,这不会产生以下警告:
iframe_style: str = """
width: 100%;
...
"""
# Doing sth with `iframe_style`
Run Code Online (Sandbox Code Playgroud)
嗯,从技术上讲它确实很好; 代码运行.但不知何故flake8对此并不满意.多行字符串和后面的代码始终相同.
当我省略"f"(i_rame_style)时,我也没有收到警告!所以我想由于某种原因,flake8想到了if foo: bar()这里!?
我在这里错过了什么?这是一个错误flake8吗?
flake8 ×10
python ×5
pylint ×4
pep8 ×2
pre-commit ×2
python-3.x ×2
autopep8 ×1
cython ×1
isort ×1
lint ×1
mypy ×1
plugins ×1
pycharm ×1
pycodestyle ×1
pylintrc ×1
python-3.6 ×1
regex ×1
vim ×1