From dc5ca461a99ec078ccb44eace22205a6eb2ec80e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sat, 1 Feb 2020 17:12:46 +0200 Subject: [PATCH] Run mypy through a pyenv/virtualenv enabler wrapper script (#30922) --- .pre-commit-config-all.yaml | 59 -------------------------------- .pre-commit-config.yaml | 20 +++++++---- azure-pipelines-ci.yml | 8 ++--- requirements_test_pre_commit.txt | 2 +- script/gen_requirements_all.py | 2 +- script/run-in-env.sh | 18 ++++++++++ tox.ini | 2 +- 7 files changed, 38 insertions(+), 73 deletions(-) delete mode 100644 .pre-commit-config-all.yaml create mode 100755 script/run-in-env.sh diff --git a/.pre-commit-config-all.yaml b/.pre-commit-config-all.yaml deleted file mode 100644 index a6b882e617b..00000000000 --- a/.pre-commit-config-all.yaml +++ /dev/null @@ -1,59 +0,0 @@ -# This configuration includes the full set of hooks we use. In -# addition to the defaults (see .pre-commit-config.yaml), this -# includes hooks that require our development and test dependencies -# installed and the virtualenv containing them active by the time -# pre-commit runs to produce correct results. -# -# If this is not a problem for your workflow, using this config is -# recommended, install it with -# pre-commit install --config .pre-commit-config-all.yaml -# Otherwise, see the default .pre-commit-config.yaml for a lighter one. - -repos: -- repo: https://github.com/psf/black - rev: 19.10b0 - hooks: - - id: black - args: - - --safe - - --quiet - files: ^((homeassistant|script|tests)/.+)?[^/]+\.py$ -- repo: https://github.com/PyCQA/flake8 - rev: 3.7.9 - hooks: - - id: flake8 - additional_dependencies: - - flake8-docstrings==1.5.0 - - pydocstyle==5.0.2 - files: ^(homeassistant|script|tests)/.+\.py$ -- repo: https://github.com/PyCQA/bandit - rev: 1.6.2 - hooks: - - id: bandit - args: - - --quiet - - --format=custom - - --configfile=tests/bandit.yaml - files: ^(homeassistant|script|tests)/.+\.py$ -- repo: https://github.com/pre-commit/mirrors-isort - rev: v4.3.21 - hooks: - - id: isort -- repo: https://github.com/pre-commit/pre-commit-hooks - rev: v2.4.0 - hooks: - - id: check-json -# Using a local "system" mypy instead of the mypy hook, because its -# results depend on what is installed. And the mypy hook runs in a -# virtualenv of its own, meaning we'd need to install and maintain -# another set of our dependencies there... no. Use the "system" one -# and reuse the environment that is set up anyway already instead. -- repo: local - hooks: - - id: mypy - name: mypy - entry: mypy - language: system - types: [python] - require_serial: true - files: ^homeassistant/.+\.py$ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 05601865691..a340aa7ae67 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,10 +1,3 @@ -# This configuration includes the default, minimal set of hooks to be -# run on all commits. It requires no specific setup and one can just -# start using pre-commit with it. -# -# See .pre-commit-config-all.yaml for a more complete one that comes -# with a better coverage at the cost of some specific setup needed. - repos: - repo: https://github.com/psf/black rev: 19.10b0 @@ -48,3 +41,16 @@ repos: rev: v2.4.0 hooks: - id: check-json +- repo: local + hooks: + # Run mypy through our wrapper script in order to get the possible + # pyenv and/or virtualenv activated; it may not have been e.g. if + # committing from a GUI tool that was not launched from an activated + # shell. + - id: mypy + name: mypy + entry: script/run-in-env.sh mypy + language: script + types: [python] + require_serial: true + files: ^homeassistant/.+\.py$ diff --git a/azure-pipelines-ci.yml b/azure-pipelines-ci.yml index 0474bf77489..4c6a353d775 100644 --- a/azure-pipelines-ci.yml +++ b/azure-pipelines-ci.yml @@ -43,7 +43,7 @@ stages: . venv/bin/activate pip install -r requirements_test.txt -c homeassistant/package_constraints.txt - pre-commit install-hooks --config .pre-commit-config-all.yaml + pre-commit install-hooks - script: | . venv/bin/activate pre-commit run codespell --all-files @@ -98,7 +98,7 @@ stages: . venv/bin/activate pip install -r requirements_test.txt -c homeassistant/package_constraints.txt - pre-commit install-hooks --config .pre-commit-config-all.yaml + pre-commit install-hooks - script: | . venv/bin/activate pre-commit run black --all-files --show-diff-on-failure @@ -194,8 +194,8 @@ stages: . venv/bin/activate pip install -e . -r requirements_test.txt -c homeassistant/package_constraints.txt - pre-commit install-hooks --config .pre-commit-config-all.yaml + pre-commit install-hooks - script: | . venv/bin/activate - pre-commit run --config .pre-commit-config-all.yaml mypy --all-files + pre-commit run mypy --all-files displayName: 'Run mypy' diff --git a/requirements_test_pre_commit.txt b/requirements_test_pre_commit.txt index 8af2cbb6123..ef10641608d 100644 --- a/requirements_test_pre_commit.txt +++ b/requirements_test_pre_commit.txt @@ -1,4 +1,4 @@ -# Automatically generated from .pre-commit-config-all.yaml by gen_requirements_all.py, do not edit +# Automatically generated from .pre-commit-config.yaml by gen_requirements_all.py, do not edit bandit==1.6.2 black==19.10b0 diff --git a/script/gen_requirements_all.py b/script/gen_requirements_all.py index 3b30bf04363..1bf9031a536 100755 --- a/script/gen_requirements_all.py +++ b/script/gen_requirements_all.py @@ -253,7 +253,7 @@ def requirements_test_output(reqs): def requirements_pre_commit_output(): """Generate output for pre-commit dependencies.""" - source = ".pre-commit-config-all.yaml" + source = ".pre-commit-config.yaml" pre_commit_conf = load_yaml(source) reqs = [] for repo in (x for x in pre_commit_conf["repos"] if x.get("rev")): diff --git a/script/run-in-env.sh b/script/run-in-env.sh new file mode 100755 index 00000000000..586f59d717a --- /dev/null +++ b/script/run-in-env.sh @@ -0,0 +1,18 @@ +#!/bin/sh -eu + +# Activate pyenv and virtualenv if present, then run the specified command + +# pyenv, pyenv-virtualenv +if [ -s .python-version ]; then + PYENV_VERSION=$(head -n 1 .python-version) + export PYENV_VERSION +fi + +# other common virtualenvs +for venv in venv .venv .; do + if [ -f $venv/bin/activate ]; then + . $venv/bin/activate + fi +done + +exec "$@" diff --git a/tox.ini b/tox.ini index 7060a46b764..5527db738a6 100644 --- a/tox.ini +++ b/tox.ini @@ -45,4 +45,4 @@ deps = -r{toxinidir}/requirements_test.txt -c{toxinidir}/homeassistant/package_constraints.txt commands = - pre-commit run --config .pre-commit-config-all.yaml mypy {posargs: --all-files} + pre-commit run mypy {posargs: --all-files}