如何配置flake8和pylint豁免shebang行的行过长检查?
Got it, let's tackle this problem step by step—you want to keep line-length checks enabled for both flake8 and pylint, but specifically exempt the shebang line (#!/...) only if it's the first line of the file. Here's how to set this up for each tool, with configurations that work seamlessly with Vim's ALE plugin:
For flake8
Flake8 flags line-length violations with the E501 error code. We have two approaches depending on how precise you need to be:
1. Approximate Solution (No Plugins Needed)
This method ignores E501 only on the first line of every file. Since shebangs almost always live on line 1, this works for 99% of use cases, and avoids the need for extra plugins.
Add this to your flake8 config file (use .flake8, setup.cfg, or tox.ini—whichever you already use):
[flake8] max-line-length = 5 # Replace with your actual desired maximum line length per-file-ignores = *:E501@1
*matches all files in your projectE501@1tells flake8 to skip the line-length check only on line 1
2. Precise Solution (Plugin Required)
If you need to strictly exempt only shebang lines (and still flag line-length violations on non-shebang first lines), install the flake8-no-shebang-line-length plugin:
pip install flake8-no-shebang-line-length
Once installed, the plugin automatically skips E501 checks for any line starting with #!/ (regardless of line number, though shebangs should always be first). No extra config is needed—your existing flake8 line-length rules will stay intact for all other lines.
For pylint
Pylint uses the C0301 code for line-length violations. It supports regex-based rule disabling, which lets us target shebang lines precisely without affecting other lines.
Configure via .pylintrc
Add these sections to your .pylintrc file:
[messages control] max-line-length = 5 # Replace with your desired max length [regexp-disable] # Disable line-length check only for lines starting with #! (shebang) C0301 = ^#!
Configure via pyproject.toml (Modern Alternative)
If you prefer using pyproject.toml for project configs:
[tool.pylint.messages_control] max-line-length = 5 [tool.pylint.regexp-disable] C0301 = "^#!"
The ^#! regex matches exactly lines starting with the shebang sequence, so only those lines are exempt from C0301 checks. Non-shebang first lines that exceed the length limit will still trigger an error, just as you want.
ALE Integration Note
You don't need to modify your Vim/ALE config at all—ALE automatically reads the standard config files for flake8 and pylint. As long as your tool configs are set up correctly, ALE will apply the rules and only flag line-length violations where you want them.
Testing your example: With max-line-length set to 5, the shebang line #!/run/stuff won't trigger an error, but print(x) will throw the expected line-length warning/error.
内容的提问来源于stack exchange,提问作者LangeHaare

