Search code examples
python-3.6flake8pre-commitpre-commit.com

pre-commit: run flake8 with python 3.6.8


I installed Pyton 3.6.8 on my system.

python3 --version   //-> Python 3.6.8
python3.6 --version //-> Python 3.6.8

My pre-commit-config.yaml is:

repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
-   repo: https://gitlab.com/pycqa/flake8
rev: 3.7.9
hooks:
- id: flake8
  language_version: python3.6

I installed the pre-commit hook for my project. Every time when I want to commit some changes to git, the pre-commit is running with the flake8 error:

TYP005 NamedTuple does not support defaults in 3.6.0

This is true for Python 3.6.0, because this feature is introduced and allowed with Python 3.6.1+. https://docs.python.org/3.6/library/typing.html#typing.NamedTuple

How can I configure flake8 to run with Python 3.6.8?

EDIT When I run flake8 file.rb, I don`t get the error message TYP005.

python3 -m pip install flake
flake --version //-> 3.7.9 (the same version as in the pre-commit script file)

Solution

  • disclaimer: I'm the author of two of the tools in question (pre-commit, flake8-typing-imports) and the maintainer of the other (flake8)


    the TYP005 code comes from flake8-typing-imports

    there are two options for indicating your minimum supported version to flake8-typing-imports, the first is a command line argument / flake8 setting:

    --min-python-version 3.6.1
    

    or in your flake8 configuration

    [flake8]
    min_python_version = 3.6.1
    

    if you're distributing a library, you can indicate the minimum supported version using the python_requires metadata -- this is specified in setup.cfg

    [options]
    python_requires >= 3.6.1
    

    an aside, I believe there's some information missing from your question, without additional_dependencies in your pre-commit configuration, flake8 will be installed in isolation and won't have access to plugins such as flake8-typing-imports -- my guess is you've actually got a configuration similar to:

    
    -   repo: https://gitlab.com/pycqa/flake8
        rev: 3.7.9
        hooks:
        -   id: flake8
            additional_dependencies: [flake8-typing-imports==1.9.0]
    

    when speaking of command line arguments above, you could specify them as args here (though I personally prefer the configuration file approaches)

        -   id: flake8
            args: [--min-python-version, '3.6.1']
            additional_dependencies: [flake8-typing-imports==1.9.0]