From 20dd7dc83a20ca63390a92f1866bfe67697dd14c Mon Sep 17 00:00:00 2001 From: crusaderky Date: Tue, 28 Jan 2020 19:41:51 +0000 Subject: [PATCH] Add isort to CI (#3721) * Add isort to CI * Run isort before black * Run isort before black * What's new * Run isort before black * DEMO: deliberate isort failure * Fix isort failure --- .github/PULL_REQUEST_TEMPLATE.md | 2 +- .pre-commit-config.yaml | 13 ++++++----- azure-pipelines.yml | 12 ++++++++++ ci/min_deps_check.py | 1 + ci/requirements/py36-min-all-deps.yml | 1 + ci/requirements/py36.yml | 1 + ci/requirements/py37-windows.yml | 1 + ci/requirements/py37.yml | 1 + doc/contributing.rst | 33 ++++++++++++--------------- doc/whats-new.rst | 2 ++ 10 files changed, 42 insertions(+), 25 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index d78dd38dd85..61d8671ab48 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -2,5 +2,5 @@ - [ ] Closes #xxxx - [ ] Tests added - - [ ] Passes `black . && mypy . && flake8` + - [ ] Passes `isort -rc . && black . && mypy . && && flake8` - [ ] Fully documented, including `whats-new.rst` for all changes and `api.rst` for new API diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ed62c1c256e..57a987faa20 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,11 @@ # https://pre-commit.com/ -# https://github.com/python/black#version-control-integration repos: + # isort should run before black as black sometimes tweaks the isort output + - repo: https://github.com/timothycrosley/isort + rev: 4.3.21-2 + hooks: + - id: isort + # https://github.com/python/black#version-control-integration - repo: https://github.com/python/black rev: stable hooks: @@ -14,7 +19,7 @@ repos: rev: v0.761 # Must match ci/requirements/*.yml hooks: - id: mypy - # run these occasionally, ref discussion https://github.com/pydata/xarray/pull/3194 + # run this occasionally, ref discussion https://github.com/pydata/xarray/pull/3194 # - repo: https://github.com/asottile/pyupgrade # rev: v1.22.1 # hooks: @@ -23,7 +28,3 @@ repos: # - "--py3-only" # # remove on f-strings in Py3.7 # - "--keep-percent-format" - # - repo: https://github.com/timothycrosley/isort - # rev: 4.3.21-2 - # hooks: - # - id: isort diff --git a/azure-pipelines.yml b/azure-pipelines.yml index d6ee76c7d3f..b09c4ec127c 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -84,6 +84,18 @@ jobs: mypy . displayName: mypy type checks +- job: isort + variables: + conda_env: py37 + pool: + vmImage: 'ubuntu-16.04' + steps: + - template: ci/azure/install.yml + - bash: | + source activate xarray-tests + isort -rc --check . + displayName: isort formatting checks + - job: MinimumVersionsPolicy pool: vmImage: 'ubuntu-16.04' diff --git a/ci/min_deps_check.py b/ci/min_deps_check.py index a5ba90679b7..527093cf5bc 100755 --- a/ci/min_deps_check.py +++ b/ci/min_deps_check.py @@ -15,6 +15,7 @@ "coveralls", "flake8", "hypothesis", + "isort", "mypy", "pip", "pytest", diff --git a/ci/requirements/py36-min-all-deps.yml b/ci/requirements/py36-min-all-deps.yml index dc77e232dea..d6e1f3082bb 100644 --- a/ci/requirements/py36-min-all-deps.yml +++ b/ci/requirements/py36-min-all-deps.yml @@ -23,6 +23,7 @@ dependencies: - hdf5=1.10 - hypothesis - iris=2.2 + - isort - lxml=4.4 # Optional dep of pydap - matplotlib=3.1 - mypy=0.761 # Must match .pre-commit-config.yaml diff --git a/ci/requirements/py36.yml b/ci/requirements/py36.yml index 7450fafbd86..929a36295a0 100644 --- a/ci/requirements/py36.yml +++ b/ci/requirements/py36.yml @@ -19,6 +19,7 @@ dependencies: - hdf5 - hypothesis - iris + - isort - lxml # optional dep of pydap - matplotlib - mypy=0.761 # Must match .pre-commit-config.yaml diff --git a/ci/requirements/py37-windows.yml b/ci/requirements/py37-windows.yml index d9e634c74ae..8755ea2cef6 100644 --- a/ci/requirements/py37-windows.yml +++ b/ci/requirements/py37-windows.yml @@ -19,6 +19,7 @@ dependencies: - hdf5 - hypothesis - iris + - isort - lxml # Optional dep of pydap - matplotlib - mypy=0.761 # Must match .pre-commit-config.yaml diff --git a/ci/requirements/py37.yml b/ci/requirements/py37.yml index 2f879e29f87..dd3267abe4c 100644 --- a/ci/requirements/py37.yml +++ b/ci/requirements/py37.yml @@ -19,6 +19,7 @@ dependencies: - hdf5 - hypothesis - iris + - isort - lxml # Optional dep of pydap - matplotlib - mypy=0.761 # Must match .pre-commit-config.yaml diff --git a/doc/contributing.rst b/doc/contributing.rst index 105133eccc4..610b1dd9b82 100644 --- a/doc/contributing.rst +++ b/doc/contributing.rst @@ -345,33 +345,31 @@ as possible to avoid mass breakages. Code Formatting ~~~~~~~~~~~~~~~ -Xarray uses `Black `_ and -`Flake8 `_ to ensure a consistent code -format throughout the project. ``black`` and ``flake8`` can be installed with +xarray uses several tools to ensure a consistent code format throughout the project: + +- `Black `_ for standardized code formatting +- `Flake8 `_ for general code quality +- `isort `_ for standardized order in imports. + See also `flake8-isort `_. +- `mypy `_ for static type checking on `type hints + `_ + ``pip``:: - pip install black flake8 + pip install black flake8 isort mypy and then run from the root of the Xarray repository:: - black . + isort -rc . + black -t py36 . flake8 + mypy . to auto-format your code. Additionally, many editors have plugins that will apply ``black`` as you edit files. -Other recommended but optional tools for checking code quality (not currently -enforced in CI): - -- `mypy `_ performs static type checking, which can - make it easier to catch bugs. Please run ``mypy xarray`` if you annotate any - code with `type hints `_. -- `isort `_ will highlight - incorrectly sorted imports. ``isort -y`` will automatically fix them. See - also `flake8-isort `_. - Optionally, you may wish to setup `pre-commit hooks `_ -to automatically run ``black`` and ``flake8`` when you make a git commit. This +to automatically run all the above tools every time you make a git commit. This can be done by installing ``pre-commit``:: pip install pre-commit @@ -380,8 +378,7 @@ and then running:: pre-commit install -from the root of the Xarray repository. Now ``black`` and ``flake8`` will be run -each time you commit changes. You can skip these checks with +from the root of the xarray repository. You can skip the pre-commit checks with ``git commit --no-verify``. .. note:: diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 4aad6ad3701..3ee804e32a5 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -157,6 +157,8 @@ Internal Changes - Replaced versioneer with setuptools-scm. Moved contents of setup.py to setup.cfg. Removed pytest-runner from setup.py, as per deprecation notice on the pytest-runner project. (:pull:`3714`) by `Guido Imperiale `_ +- Use of isort is now enforced by CI. + (:pull:`3721`) by `Guido Imperiale `_ v0.14.1 (19 Nov 2019)