57

I'm using a Flake8 git hook in my project and I want to relax the line length limit, but only for one project. Given that it looks like there's no clear API for that, how do I modify this hook to do that? Alternatively, is there a git-config setting or an environment variable that would let me set that? Here's the hook source code:

#!/usr/bin/env python
import os
import sys
import logging
import os.path as P

from flake8.main import git


if __name__ == '__main__':
    ret = git.hook(
        strict=git.config_for('strict'),
        lazy=git.config_for('lazy'),
    )
    if ret:
        sys.exit(ret)
d33tah
  • 10,999
  • 13
  • 68
  • 158

2 Answers2

123

Use the setup.cfg file in each project. This file is read by various Python-related tools, including pep8 (see pep8's documentation) and flake8.

The documentation for setup.cfg with flake8 is in the Configuring Flake8 chapter. In short, you want a setup.cfg file with this content (obviously, adjust the 99 to your needs):

[flake8]
max-line-length = 99

If you don't want to create a setup.cfg file, you can also specify this as an argument to the command:

flake8 --max-line-length 99
jeremysprofile
  • 10,028
  • 4
  • 33
  • 53
Matthieu Moy
  • 15,151
  • 5
  • 38
  • 65
  • 1
    Where does this config go when using `tox` and a `pyproject.toml`? It can go in the command line invocation in the `tox.ini` but is it possible to put it separately in the `tox.ini`? And if your `tox` config is in the `pyproject.toml` does the `flake8` config have to go in the `tox.ini` text inside the `pyproject.toml` or can the `pyproject.toml` have it's own section that `flake8` will read when invoked by `tox`? – NeilG Oct 01 '22 at 06:22
0

Supplement post for @Matthieu Moy's answer.

If you use pyproject.toml, install pyproject-flake8 package which enables you to write your flake8 settings:

[tool.flake8]
max-line-length = 88

If you use tox.ini, add the following line to your config file:

[flake8]
max-line-length = 88

Be aware that multiple configuration files will override each other. In my case, the tox config overrides the poetry config.

Péter Szilvási
  • 362
  • 4
  • 17