Search code examples
pythonpylintpre-commit.com

Run pylint for both python 2.7 and 3.7, in pre-commit hook on Docker image


I am trying to use CircleCI to run a pre-commit hook that runs pylint for both Python 2.7 and 3.7.

.circleci/config.yml runs pre-commit for both Python 2 and Python 3:

jobs:
  lint-py2:
    docker:
      - image: python:2.7.14
    steps:
      {snip}
      - run: pre-commit run --all-files
      {snip}

  lint-py3:
    docker:
      - image: python:3.7.3
    steps:
      {snip}
      - run: pre-commit run --all-files
      {snip}

pre-commit, among other things, runs pylint:

-   repo: https://github.com/pre-commit/mirrors-pylint
    rev: v2.3.1  # Which version here?
    hooks:
    -   id: pylint

The problem here is that there is no version of pylint that is compatible with both Python 2.7 and 3.7: Python 2.7 requires pylint 1.x and Python 3.7 requires pylint 2.x.

How can I make Circle CI run both linting jobs using different versions of pylint?

I am considering several options:

  • Add pylint twice in the pre-commit configuration (with different aliases) and disable one or the other in the job definition
    • It seems that pre-commit tries to install dependencies before looking at the SKIP variable, so the Python 2.7 run tries to install pylint 2 anyway, and errors with ERROR: Could not find a version that satisfies the requirement pylint==2.3.1 (from pre-commit-dummy-package==0.0.0)
  • Use a Docker image that has both Python versions and set the python version at the hook level
    • This requires building my own Docker image
  • Skip pylint in one of the linting jobs
  • Drop 2.7 or 3.7 support

Solution

  • The easiest option is probably to install both python2 and python3 though it is possible to use multiple configuration files to accomplish what you want:

    Another option would be to only run one of them during CI by utilizing the --config option

    With this you would have your default .pre-commit-config.yaml and a special .pre-commit-config-py27.yaml which includes the python2.7 pylint instead of the python3 pylint

    In CI you'd invoke pre-commit run --config .pre-commit-config-py27.yaml --all-files --show-diff-on-failure for python2.7 and the normal pre-commit run --all-files --show-diff-on-failure for the non-py27 run