From 927de9f3b6600f6645804f71adb78ef53b352739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Thu, 2 Jul 2020 08:56:30 +0200 Subject: [PATCH 1/7] cfg: add isort conf to setup.cfg --- setup.cfg | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/setup.cfg b/setup.cfg index 706af9c14df..c1209ca7fa0 100644 --- a/setup.cfg +++ b/setup.cfg @@ -12,3 +12,16 @@ exclude = doc,benchmarks,*/api.py,*/__init__.py,*/__config__.py,yt/visualization max-line-length=999 ignore = E111,E121,E122,E123,E124,E125,E126,E127,E128,E129,E131,E201,E202,E211,E221,E222,E227,E228,E241,E301,E203,E225,E226,E231,E251,E261,E262,E265,E266,E302,E303,E305,E306,E402,E502,E701,E703,E722,E741,E731,W291,W292,W293,W391,W503,W504,W605 jobs=8 + +# To be kept consistent with "Import Formatting" section in CONTRIBUTING.rst +[tool:isort] +multi_line_output=3 +include_trailing_comma=True +force_grid_wrap=0 +combine_as_imports=True +line_length=88 +# isort can't be applied to yt/__init__.py because it creates circular imports +skip = venv, doc, benchmarks, yt/__init__.py, yt/extern +known_third_party = IPython, nose, numpy, sympy, matplotlib, unyt, git, yaml, dateutil, requests, coverage, pytest +known_first_party = yt +sections = FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER From f4e31db30c771eecb12d8c50476df38b510055dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Sat, 16 May 2020 12:00:09 +0200 Subject: [PATCH 2/7] sty: add isort:skip to noqa instruction in yt.testing --- yt/testing.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/yt/testing.py b/yt/testing.py index 001f38b3918..2b64b1fec45 100644 --- a/yt/testing.py +++ b/yt/testing.py @@ -14,12 +14,12 @@ # we import this in a weird way from numpy.testing to avoid triggering # flake8 errors from the unused imports. These test functions are imported # elsewhere in yt from here so we want them to be imported here. -from numpy.testing import assert_array_equal, assert_almost_equal # NOQA -from numpy.testing import assert_approx_equal, assert_array_almost_equal # NOQA -from numpy.testing import assert_equal, assert_array_less # NOQA -from numpy.testing import assert_string_equal # NOQA -from numpy.testing import assert_array_almost_equal_nulp # NOQA -from numpy.testing import assert_allclose, assert_raises # NOQA +from numpy.testing import assert_array_equal, assert_almost_equal # NOQA isort:skip +from numpy.testing import assert_approx_equal, assert_array_almost_equal # NOQA isort:skip +from numpy.testing import assert_equal, assert_array_less # NOQA isort:skip +from numpy.testing import assert_string_equal # NOQA isort:skip +from numpy.testing import assert_array_almost_equal_nulp # NOQA isort:skip +from numpy.testing import assert_allclose, assert_raises # NOQA isort:skip from numpy.random import RandomState from yt.convenience import load from yt.units.yt_array import YTArray, YTQuantity From 837c35822bac96f2fab5993f479b920217f7827b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Sat, 16 May 2020 14:34:33 +0200 Subject: [PATCH 3/7] sty: group imports in yt/exthook.py . This is the only change required to make the (isort + black) combo commutative --- yt/exthook.py | 2 +- yt/funcs.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/yt/exthook.py b/yt/exthook.py index 1d2f4ccb43d..f4ea3dfd586 100644 --- a/yt/exthook.py +++ b/yt/exthook.py @@ -20,8 +20,8 @@ :license: BSD, see LICENSE for more details. """ # This source code was originally in flask/exthook.py -import sys import os +import sys class ExtensionImporter: diff --git a/yt/funcs.py b/yt/funcs.py index c9f8780e239..3b34ed947e7 100644 --- a/yt/funcs.py +++ b/yt/funcs.py @@ -15,7 +15,6 @@ import numpy as np import itertools import base64 -import numpy import matplotlib import getpass import glob From ccc44bdcb14485afb7c608facba0ce9e31d4be16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Sat, 16 May 2020 12:44:23 +0200 Subject: [PATCH 4/7] sty: fix a mistake in funcs.py where numpy was imported under two namespaces (np and numpy), which is not handled correctly by isort. --- yt/funcs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yt/funcs.py b/yt/funcs.py index 3b34ed947e7..94f7430fb29 100644 --- a/yt/funcs.py +++ b/yt/funcs.py @@ -651,7 +651,7 @@ def get_yt_version(): def get_version_stack(): version_info = {} version_info['yt'] = get_yt_version() - version_info['numpy'] = numpy.version.version + version_info['numpy'] = np.version.version version_info['matplotlib'] = matplotlib.__version__ return version_info From d22890bd4a24fbccad2ca502fb299c5fe546a29d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Thu, 2 Jul 2020 08:56:04 +0200 Subject: [PATCH 5/7] doc: add an isort section to CONTRIBUTING.rst --- CONTRIBUTING.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index f77e890b01b..1b148aea4f1 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -747,6 +747,27 @@ error and warning list blacklist a large number of the full list of rules that are checked by ``flake8`` by default. +Import Formatting +----------------- + +We use ``isort`` to enforce PEP-8 guidelines for import ordering. +By decreasing priority order: +FUTURE > STDLIB > THIRD PARTY > FIRST PARTY > EXPLICITLY LOCAL + +``isort`` can be installed via ``pip`` + +.. code-block:: bash + + $ pip install isort + +To validate import order, run ``isort`` recursively at the top level + +.. code-block:: bash + + $ isort -rc . --check-only + +If any error is detected, rerun this without the ``--check-only`` flag to fix them. + Source code style guide ----------------------- From e1bbca1bf855af78fbb3d42de165b92ed638487f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Sat, 16 May 2020 16:13:06 +0200 Subject: [PATCH 6/7] doc: add isort check to PR checklist --- .github/PULL_REQUEST_TEMPLATE.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index be463bb8b3e..8e75d176a68 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -20,7 +20,8 @@ detail. Why is this change required? What problem does it solve?--> -- [ ] Code passes flake8 checker +- [ ] pass `flake8 yt/` +- [ ] pass `isort -rc . --check-only` - [ ] New features are documented, with docstrings and narrative docs - [ ] Adds a test for any bugs fixed. Adds tests for new features. From 3bbad9dfaf3b24febb5d6910cb98efbe1d6f5e0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Thu, 2 Jul 2020 19:49:47 +0200 Subject: [PATCH 7/7] ci: add isort to lint_requirements.txt and run it in check-only mode on travis --- .travis.yml | 4 ++++ tests/lint_requirements.txt | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e7cfad98563..7997c752a73 100644 --- a/.travis.yml +++ b/.travis.yml @@ -82,6 +82,10 @@ jobs: python: 3.6 script: flake8 yt/ + - stage: Lint + python: 3.6 + script: isort --check-only -rc yt/ + - stage: tests name: "Python: 3.6 Minimal Dependency Unit Tests" python: 3.6 diff --git a/tests/lint_requirements.txt b/tests/lint_requirements.txt index 19e903dbf4d..e3d69bb855e 100644 --- a/tests/lint_requirements.txt +++ b/tests/lint_requirements.txt @@ -1,4 +1,5 @@ flake8==3.6.0 mccabe==0.6.1 pycodestyle==2.4.0 -pyflakes==2.0.0 \ No newline at end of file +pyflakes==2.0.0 +isort==4.3