【问题标题】:Python AutoPep8 formatting not working with max line length parameterPython AutoPep8 格式不适用于最大行长参数
【发布时间】:2020-08-08 10:41:55
【问题描述】:

我注意到一件奇怪的事情,当我们设置时,VSCode 中的 autopep8 自动格式化不起作用

    "python.formatting.autopep8Args": [
        "--line-length 119"
    ],

但如果此设置处于行长 79 的默认模式下,则效果很好。 autopep8 是否存在一些问题,只能在不超过 79 行的情况下工作,或者我在 VSCode 中犯了任何错误。我需要的主要功能是当我的 python 程序行太长时,它应该能够将它分成多行。我不想继续使用 79 个字符的方法。我首选的方法是 119 个字符。目前,我必须手动缩进大行。除了 pep8 之外,是否还有其他格式支持 119 个字符和自动缩进超过 119 个字符的行。

我正在附加我的 settings.json 文件数据

{
    "window.zoomLevel": 1,
    "python.dataScience.sendSelectionToInteractiveWindow": true,
    "diffEditor.ignoreTrimWhitespace": false,
    "editor.fontSize": 16,
    "python.formatting.provider": "autopep8",
    "editor.formatOnPaste": true,
    "editor.formatOnSave": true,
    "editor.formatOnType": true,
    "python.autoComplete.addBrackets": true,
    "python.formatting.autopep8Args": [
        "--line-length 119"
    ],
    // "python.linting.flake8Args": [
    //     "--max-line-length=120"
    // ],
    "files.autoSaveDelay": 10000,
    "editor.defaultFormatter": "ms-python.python",
    "files.autoSave": "afterDelay",
    "files.trimTrailingWhitespace": true,
    "files.trimFinalNewlines": true,
    "editor.quickSuggestions": true,
    "editor.codeActionsOnSave": null,
}

【问题讨论】:

  • 是不是应该是--max-line-length=119
  • Pep8 说 --max-line-length=79 但 Django 说 --max-line-length=119。两者都与 Python 相关联。即使在 GitHub 代码审查中也是 119。我只想要支持具有 119 个字符的 python 并自动格式化我的文档而不是我这样做,因为它使用 autopep8 格式化,最大行长 = 79

标签: python visual-studio-code flake8 autopep8


【解决方案1】:

实验对我有用

"python.formatting.autopep8Args": ["--max-line-length", "120", "--experimental"]

check out this link for proper format specifier settings

【讨论】:

  • --experimental 对我来说不是必需的。
  • 在 Mac 上使用 autopep8 1.5.7 --experimental 选项对于根据最大长度进行格式化至关重要,否则行将保持在提供的最大长度之上。
  • @k_rus:你知道这是在哪个版本中针对 Mac 进行了更正吗?(即不需要“实验性”)
  • @MrT77 对不起,我不知道。它不适用于 1.5.7 版。我现在可以看到 1.6.0 可以通过 homebrew 获得,但我还没有测试过。
  • 我在 MacOs 中使用 setup.cfg,所以我在那里定义了experimental=True 和 max-line-length=120 但是当它大于 100 时我仍然收到 line too long 的警告。 .对此有任何想法吗? (我使用的是 autopep8 1.6.0)
【解决方案2】:

这应该可行 -

"python.formatting.provider": "autopep8",
"python.formatting.autopep8Args": [
    "--max-line-length",
    "120",
    "--experimental"
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-28
    • 2021-07-26
    • 2018-08-26
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 2017-08-25
    相关资源
    最近更新 更多