diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8d9a956e..8c22faaa 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -19,6 +19,13 @@ repos: - id: pyupgrade args: [--py39-plus] + # Automatically sort python imports + - repo: https://github.com/pycqa/isort + rev: 5.10.1 + hooks: + - id: isort + args: [--profile, black] + # Autoformat: Python code - repo: https://github.com/psf/black rev: 22.1.0 @@ -53,9 +60,10 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.1.0 hooks: - - id: trailing-whitespace - - id: end-of-file-fixer - id: check-added-large-files + - id: end-of-file-fixer + - id: requirements-txt-fixer + - id: trailing-whitespace # Lint: Dockerfile - repo: https://github.com/hadolint/hadolint.git @@ -108,12 +116,13 @@ repos: - id: nbstripout # nbQA provides tools from the Python ecosystem like - # pyupgrade, black, and flake8, adjusted for notebooks. + # pyupgrade, isort, black, and flake8, adjusted for notebooks. - repo: https://github.com/nbQA-dev/nbQA rev: 1.3.1 hooks: - id: nbqa-pyupgrade args: [--py39-plus] + - id: nbqa-isort - id: nbqa-black args: [--target-version=py39] - id: nbqa-flake8 diff --git a/base-notebook/jupyter_server_config.py b/base-notebook/jupyter_server_config.py index c5f70c6f..ef0380bd 100644 --- a/base-notebook/jupyter_server_config.py +++ b/base-notebook/jupyter_server_config.py @@ -1,11 +1,11 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. # mypy: ignore-errors -from jupyter_core.paths import jupyter_data_dir -import subprocess import os import stat +import subprocess +from jupyter_core.paths import jupyter_data_dir c = get_config() # noqa: F821 c.ServerApp.ip = "0.0.0.0" diff --git a/tagging/create_manifests.py b/tagging/create_manifests.py index 5704801c..c784bdf5 100755 --- a/tagging/create_manifests.py +++ b/tagging/create_manifests.py @@ -5,13 +5,14 @@ import argparse import datetime import logging import os + from docker.models.containers import Container + from .docker_runner import DockerRunner from .get_taggers_and_manifests import get_taggers_and_manifests from .git_helper import GitHelper from .manifests import ManifestHeader, ManifestInterface - LOGGER = logging.getLogger(__name__) diff --git a/tagging/docker_runner.py b/tagging/docker_runner.py index 08a93867..9d71f27b 100644 --- a/tagging/docker_runner.py +++ b/tagging/docker_runner.py @@ -1,11 +1,11 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. -from typing import Optional +import logging from types import TracebackType +from typing import Optional + import docker from docker.models.containers import Container -import logging - LOGGER = logging.getLogger(__name__) diff --git a/tagging/get_taggers_and_manifests.py b/tagging/get_taggers_and_manifests.py index d8730a32..2a882c1b 100644 --- a/tagging/get_taggers_and_manifests.py +++ b/tagging/get_taggers_and_manifests.py @@ -1,6 +1,7 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. from typing import Optional + from .images_hierarchy import ALL_IMAGES from .manifests import ManifestInterface from .taggers import TaggerInterface diff --git a/tagging/images_hierarchy.py b/tagging/images_hierarchy.py index 690ca121..9a68f97e 100644 --- a/tagging/images_hierarchy.py +++ b/tagging/images_hierarchy.py @@ -2,29 +2,30 @@ # Distributed under the terms of the Modified BSD License. from dataclasses import dataclass, field from typing import Optional + +from .manifests import ( + AptPackagesManifest, + CondaEnvironmentManifest, + JuliaPackagesManifest, + ManifestInterface, + RPackagesManifest, + SparkInfoManifest, +) from .taggers import ( - TaggerInterface, - SHATagger, DateTagger, - UbuntuVersionTagger, - PythonVersionTagger, - JupyterNotebookVersionTagger, - JupyterLabVersionTagger, - JupyterHubVersionTagger, - RVersionTagger, - TensorflowVersionTagger, - JuliaVersionTagger, - SparkVersionTagger, HadoopVersionTagger, JavaVersionTagger, -) -from .manifests import ( - ManifestInterface, - CondaEnvironmentManifest, - AptPackagesManifest, - RPackagesManifest, - JuliaPackagesManifest, - SparkInfoManifest, + JuliaVersionTagger, + JupyterHubVersionTagger, + JupyterLabVersionTagger, + JupyterNotebookVersionTagger, + PythonVersionTagger, + RVersionTagger, + SHATagger, + SparkVersionTagger, + TaggerInterface, + TensorflowVersionTagger, + UbuntuVersionTagger, ) diff --git a/tagging/manifests.py b/tagging/manifests.py index c0bceaf6..e7a51924 100644 --- a/tagging/manifests.py +++ b/tagging/manifests.py @@ -1,7 +1,8 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. -from plumbum.cmd import docker from docker.models.containers import Container +from plumbum.cmd import docker + from .docker_runner import DockerRunner from .git_helper import GitHelper diff --git a/tagging/tag_image.py b/tagging/tag_image.py index 364be059..c1783f52 100755 --- a/tagging/tag_image.py +++ b/tagging/tag_image.py @@ -3,12 +3,13 @@ # Distributed under the terms of the Modified BSD License. import argparse import logging + from plumbum.cmd import docker + from .docker_runner import DockerRunner from .get_taggers_and_manifests import get_taggers_and_manifests from .github_set_env import github_set_env - LOGGER = logging.getLogger(__name__) diff --git a/tagging/taggers.py b/tagging/taggers.py index 09f017ac..a39334f5 100644 --- a/tagging/taggers.py +++ b/tagging/taggers.py @@ -1,9 +1,11 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. from datetime import datetime + from docker.models.containers import Container -from .git_helper import GitHelper + from .docker_runner import DockerRunner +from .git_helper import GitHelper def _get_program_version(container: Container, program: str) -> str: diff --git a/tests/all-spark-notebook/test_spark_notebooks.py b/tests/all-spark-notebook/test_spark_notebooks.py index 9d2475b3..ee64dfb2 100644 --- a/tests/all-spark-notebook/test_spark_notebooks.py +++ b/tests/all-spark-notebook/test_spark_notebooks.py @@ -2,10 +2,9 @@ # Distributed under the terms of the Modified BSD License. import logging - -import pytest # type: ignore from pathlib import Path +import pytest # type: ignore from conftest import TrackedContainer LOGGER = logging.getLogger(__name__) diff --git a/tests/base-notebook/test_container_options.py b/tests/base-notebook/test_container_options.py index de1261b3..03405694 100644 --- a/tests/base-notebook/test_container_options.py +++ b/tests/base-notebook/test_container_options.py @@ -1,12 +1,11 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. +import logging import pathlib import time -import logging import pytest # type: ignore import requests - from conftest import TrackedContainer, find_free_port LOGGER = logging.getLogger(__name__) diff --git a/tests/base-notebook/test_outdated.py b/tests/base-notebook/test_outdated.py index ac13da56..26416545 100644 --- a/tests/base-notebook/test_outdated.py +++ b/tests/base-notebook/test_outdated.py @@ -5,7 +5,6 @@ import logging import pytest # type: ignore from conftest import TrackedContainer - from package_helper import CondaPackageHelper LOGGER = logging.getLogger(__name__) diff --git a/tests/base-notebook/test_package_managers.py b/tests/base-notebook/test_package_managers.py index 9f36185a..d613bf3c 100644 --- a/tests/base-notebook/test_package_managers.py +++ b/tests/base-notebook/test_package_managers.py @@ -2,8 +2,8 @@ # Distributed under the terms of the Modified BSD License. import logging -import pytest # type: ignore +import pytest # type: ignore from conftest import TrackedContainer LOGGER = logging.getLogger(__name__) diff --git a/tests/base-notebook/test_packages.py b/tests/base-notebook/test_packages.py index ddee10ce..ea1e9bb4 100644 --- a/tests/base-notebook/test_packages.py +++ b/tests/base-notebook/test_packages.py @@ -38,11 +38,10 @@ Example: """ import logging +from typing import Callable, Iterable import pytest # type: ignore from conftest import TrackedContainer -from typing import Callable, Iterable - from package_helper import CondaPackageHelper LOGGER = logging.getLogger(__name__) diff --git a/tests/base-notebook/test_python.py b/tests/base-notebook/test_python.py index 27ca9a03..75db24ff 100644 --- a/tests/base-notebook/test_python.py +++ b/tests/base-notebook/test_python.py @@ -2,9 +2,8 @@ # Distributed under the terms of the Modified BSD License. import logging -from packaging import version # type: ignore - from conftest import TrackedContainer +from packaging import version # type: ignore LOGGER = logging.getLogger(__name__) diff --git a/tests/base-notebook/test_start_container.py b/tests/base-notebook/test_start_container.py index 0322db82..fffdf68f 100644 --- a/tests/base-notebook/test_start_container.py +++ b/tests/base-notebook/test_start_container.py @@ -2,11 +2,11 @@ # Distributed under the terms of the Modified BSD License. import logging +import time from typing import Optional + import pytest # type: ignore import requests -import time - from conftest import TrackedContainer, find_free_port LOGGER = logging.getLogger(__name__) diff --git a/tests/conftest.py b/tests/conftest.py index 18d7435f..fd4e4704 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,19 +1,17 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. -from contextlib import closing -import os import logging +import os import socket +from contextlib import closing from typing import Any, Optional import docker -from docker.models.containers import Container import pytest # type: ignore import requests - -from urllib3.util.retry import Retry +from docker.models.containers import Container from requests.adapters import HTTPAdapter - +from urllib3.util.retry import Retry LOGGER = logging.getLogger(__name__) diff --git a/tests/minimal-notebook/data/notebook_math.ipynb b/tests/minimal-notebook/data/notebook_math.ipynb index 951f751e..5b028b1d 100644 --- a/tests/minimal-notebook/data/notebook_math.ipynb +++ b/tests/minimal-notebook/data/notebook_math.ipynb @@ -27,7 +27,7 @@ }, "outputs": [], "source": [ - "from sympy import init_printing, symbols, sin, diff, integrate" + "from sympy import diff, init_printing, integrate, sin, symbols" ] }, { diff --git a/tests/minimal-notebook/test_nbconvert.py b/tests/minimal-notebook/test_nbconvert.py index 0763fb4a..3c70794b 100644 --- a/tests/minimal-notebook/test_nbconvert.py +++ b/tests/minimal-notebook/test_nbconvert.py @@ -2,10 +2,9 @@ # Distributed under the terms of the Modified BSD License. import logging - -import pytest # type: ignore from pathlib import Path +import pytest # type: ignore from conftest import TrackedContainer LOGGER = logging.getLogger(__name__) diff --git a/tests/package_helper.py b/tests/package_helper.py index 358f3951..88738ba5 100644 --- a/tests/package_helper.py +++ b/tests/package_helper.py @@ -22,17 +22,16 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +import json +import logging import re from collections import defaultdict from itertools import chain -import logging -import json from typing import Any, Optional -from docker.models.containers import Container - -from tabulate import tabulate from conftest import TrackedContainer +from docker.models.containers import Container +from tabulate import tabulate LOGGER = logging.getLogger(__name__) diff --git a/tests/pyspark-notebook/test_spark.py b/tests/pyspark-notebook/test_spark.py index dcb9fe6b..416fb099 100644 --- a/tests/pyspark-notebook/test_spark.py +++ b/tests/pyspark-notebook/test_spark.py @@ -4,7 +4,6 @@ import logging from conftest import TrackedContainer - LOGGER = logging.getLogger(__name__) diff --git a/tests/run_tests.py b/tests/run_tests.py index 664ebcc2..a30a0391 100755 --- a/tests/run_tests.py +++ b/tests/run_tests.py @@ -5,10 +5,8 @@ import argparse import logging import plumbum -from plumbum.cmd import pytest - from images_hierarchy import get_test_dirs - +from plumbum.cmd import pytest LOGGER = logging.getLogger(__name__) diff --git a/tests/scipy-notebook/data/cython/setup.py b/tests/scipy-notebook/data/cython/setup.py index de123d62..2ad2140c 100644 --- a/tests/scipy-notebook/data/cython/setup.py +++ b/tests/scipy-notebook/data/cython/setup.py @@ -1,4 +1,6 @@ -from setuptools import setup -from Cython.Build import cythonize +# These lines are not sorted by isort on purpose +# see: https://stackoverflow.com/a/53356077/4881441 +from setuptools import setup # isort:skip +from Cython.Build import cythonize # isort:skip setup(ext_modules=cythonize("helloworld.pyx")) diff --git a/tests/scipy-notebook/data/matplotlib/matplotlib_1.py b/tests/scipy-notebook/data/matplotlib/matplotlib_1.py index 11b10dd3..e1a0add6 100644 --- a/tests/scipy-notebook/data/matplotlib/matplotlib_1.py +++ b/tests/scipy-notebook/data/matplotlib/matplotlib_1.py @@ -1,11 +1,12 @@ # Matplotlib: Create a simple plot example. # Refs: https://matplotlib.org/stable/gallery/lines_bars_and_markers/simple_plot.html +import os + # Optional test with [Matplotlib Jupyter Integration](https://github.com/matplotlib/ipympl) # %matplotlib widget import matplotlib.pyplot as plt import numpy as np -import os # Data for plotting t = np.arange(0.0, 2.0, 0.01) diff --git a/tests/scipy-notebook/data/matplotlib/matplotlib_fonts_1.py b/tests/scipy-notebook/data/matplotlib/matplotlib_fonts_1.py index afc50fb3..951a3d87 100644 --- a/tests/scipy-notebook/data/matplotlib/matplotlib_fonts_1.py +++ b/tests/scipy-notebook/data/matplotlib/matplotlib_fonts_1.py @@ -1,7 +1,8 @@ # Matplotlib: Test tex fonts +import os + import matplotlib import matplotlib.pyplot as plt -import os matplotlib.rcParams["pgf.texsystem"] = "pdflatex" matplotlib.rcParams.update( diff --git a/tests/scipy-notebook/test_extensions.py b/tests/scipy-notebook/test_extensions.py index 3f457cad..ca2a5f4e 100644 --- a/tests/scipy-notebook/test_extensions.py +++ b/tests/scipy-notebook/test_extensions.py @@ -3,7 +3,6 @@ import logging import pytest # type: ignore - from conftest import TrackedContainer LOGGER = logging.getLogger(__name__) diff --git a/tests/scipy-notebook/test_matplotlib.py b/tests/scipy-notebook/test_matplotlib.py index bf5aacbc..725f8a9e 100644 --- a/tests/scipy-notebook/test_matplotlib.py +++ b/tests/scipy-notebook/test_matplotlib.py @@ -2,10 +2,9 @@ # Distributed under the terms of the Modified BSD License. import logging - -import pytest # type: ignore from pathlib import Path +import pytest # type: ignore from conftest import TrackedContainer LOGGER = logging.getLogger(__name__) diff --git a/tests/scipy-notebook/units/unit_pandas.py b/tests/scipy-notebook/units/unit_pandas.py index 5036f823..fc998225 100644 --- a/tests/scipy-notebook/units/unit_pandas.py +++ b/tests/scipy-notebook/units/unit_pandas.py @@ -4,6 +4,5 @@ import numpy as np import pandas as pd - np.random.seed(0) print(pd.Series(np.random.randint(0, 7, size=10)).sum()) diff --git a/tests/tensorflow-notebook/units/unit_tensorflow.py b/tests/tensorflow-notebook/units/unit_tensorflow.py index 0f5911ca..96446a5d 100644 --- a/tests/tensorflow-notebook/units/unit_tensorflow.py +++ b/tests/tensorflow-notebook/units/unit_tensorflow.py @@ -2,6 +2,5 @@ # Distributed under the terms of the Modified BSD License. import tensorflow as tf - print(tf.constant("Hello, TensorFlow")) print(tf.reduce_sum(tf.random.normal([1000, 1000])))