pre-commit-hookflake8

black and flake8 hooks conflicts


Black fail and reformat the following

foo1 = foo2[len(foo3):]

to

foo1 = foo2[len(foo3) :]

But Flake8 fails with

foo1 = foo2[len(foo3) :]

and wants

foo1 = foo2[len(foo3):]

Unless I am mistaken, in this case Flake8 is right.

How to make a pre-commit that doesn't fail with black and flake8, in this case ?


Solution

  • Thanks to the answer of Anthony Sottile we can say that:

    1- Black wants extra space for “complex expressions” as foo1 = foo2[len(foo3) :] and this behaviour may raise E203 whitespace before ':' warnings in style guide enforcement tools like Flake8. Since E203 is not PEP 8 compliant, we should tell Flake8 to ignore these warnings.

    2- Contrary to what I thought, the pyproject.toml file is not used for the configuration of flake8. We must absolutely and exclusively use .flake8, setup.cfg, or tox.ini.

    Finally, here is the configuration that I use (and which fix the issue encountered initially):

    $ more .pre-commit-config.yaml
    repos:
    
    -   repo: https://github.com/pre-commit/pre-commit-hooks
        rev: v4.4.0
        hooks: # general hooks
        -   id: check-added-large-files
        -   id: check-ast
        -   id: check-case-conflict
        -   id: check-docstring-first
        -   id: check-json
        -   id: check-merge-conflict
        -   id: check-toml
        -   id: check-xml
        -   id: check-yaml
        -   id: debug-statements
        -   id: end-of-file-fixer
        -   id: fix-encoding-pragma
        -   id: name-tests-test
        -   id: trailing-whitespace
    
    -   repo: https://github.com/psf/black
        rev: 23.1.0
        hooks: # code formatter
        -   id: black
            language: python
            args: ["--line-length=79"]
    
    -   repo: https://github.com/pycqa/isort
        rev: 5.12.0
        hooks: # imports sorting
        -   id: isort
            name: isort (python)
    
    -   repo: https://github.com/econchick/interrogate
        rev: 1.5.0
        hooks: # documentation checker
        -   id: interrogate
            args: [--fail-under=80, -vv]
    
    -   repo: https://github.com/PyCQA/flake8
        rev: 6.0.0
        hooks:
        -   id: flake8
            name: flake8 under python3
            language_version: python3
    
    $ more pyproject.toml
    [tool.black]
    line-length = 79
    include = '\.pyi?$'
    exclude = '''
    /(
        \.git
      | \.hg
      | \.mypy_cache
      | \.tox
      | \.venv
      | _build
      | buck-out
      | build
      | dist
    )/
    '''
    
    #[tool.flake8]
    #max-line-length = 79
    #exclude = [".git", "__pycache__", "dist"]
    #extend-ignore = ["E203", "E266", "E501", "W503", "F403", "F401"]
    #max-complexity = 10
    
    [tool.isort]
    atomic = true
    profile = "black"
    line_length = 79
    skip_gitignore = true
    
    $ more .flake8
    [flake8]
    ignore = E203, E266, E501, W503, F403, F401
    exclude =
        .git,
        __pycache__,
        docs/source/conf.py,
        build,
        dist
    max-complexity = 10
    max-line-length = 79