当我 运行 flake8 时,如何自动格式化包含长字符串的 Python 文件?

How can I auto-format a Python file that contains long strings to pass when I run flake8?

我有很多 json 样式数据,我正在存储这些数据用于我正在处理的一些单元测试 api 模拟。数据以Python字典的形式存储在.py文件中,我没有写所以创建时无法控制行长。

我现在正在进行代码审查,这个文件没有通过 flake8,因为有些行超过了 1000 个字符。我试过 autopep8pep8black,我试过在 VS Code 中更改换行,我试过在线搜索答案,但一无所获。

有没有一种方法可以自动格式化这种文件,因为手动操作会花费很长时间并且非常乏味。谢谢

编辑

我的 settings.json

中有这个配置
{
  "python.pythonPath": "C:\Users\andre_2d8wovo\.virtualenvs\asana_data-LIIUPrbn\Scripts\python.exe",
  "editor.detectIndentation": false,
  "editor.formatOnSave": true,
  "editor.insertSpaces": true,
  "editor.tabSize": 4,
  "python.formatting.autopep8Args": ["--max-line-length=80"],
  "python.formatting.provider": "autopep8",
  "python.linting.enabled": true,
  "python.linting.lintOnSave": true,
  "python.linting.pycodestyleArgs": ["--max-line-length=80"],
  "python.linting.pycodestyleEnabled": true,
  "python.linting.pylintEnabled": false
}

如果您希望该行很长,您可以使用注释告诉 flake8 通过在行尾写入 # noqa: E501 来忽略该行很长的事实。

Black 目前不拆分字符串,在他们的 github 上有一个 open issue