From 9df2f1ff13ee37c312b5f04fcb77f4339f92b015 Mon Sep 17 00:00:00 2001 From: Aaron Gokaslan Date: Fri, 13 Aug 2021 12:37:05 -0400 Subject: [PATCH] maint(precommit): Apply isort (#3195) * Apply isort * Tweak isort config * Add env.py as a known_first_party * Add one missing known first party * Make config compat with older isort versions * Add another comment * Revert pyproject setting --- .pre-commit-config.yaml | 5 +++++ docs/benchmark.py | 6 +++--- docs/conf.py | 4 ++-- noxfile.py | 1 - pybind11/__init__.py | 5 ++--- pybind11/__main__.py | 2 +- pybind11/_version.pyi | 2 +- pybind11/commands.py | 1 - pybind11/setup_helpers.py | 8 ++++---- pybind11/setup_helpers.pyi | 9 ++++----- pyproject.toml | 6 ++++++ setup.py | 2 +- tests/extra_setuptools/test_setuphelper.py | 2 +- tests/test_buffers.py | 5 ++--- tests/test_builtin_casters.py | 3 +-- tests/test_call_policies.py | 3 +-- tests/test_callbacks.py | 8 +++++--- tests/test_chrono.py | 3 ++- tests/test_class.py | 3 +-- tests/test_cmake_build/test.py | 1 + tests/test_copy_move.py | 1 + tests/test_custom_type_casters.py | 1 + tests/test_eigen.py | 1 + tests/test_enum.py | 1 + tests/test_eval.py | 1 - tests/test_exceptions.py | 3 +-- tests/test_factory_constructors.py | 6 +++--- tests/test_iostream.py | 4 ++-- tests/test_kwargs_and_defaults.py | 1 - tests/test_local_bindings.py | 1 - tests/test_methods_and_attributes.py | 3 +-- tests/test_modules.py | 8 +++++--- tests/test_multiple_inheritance.py | 1 - tests/test_numpy_array.py | 1 - tests/test_numpy_dtypes.py | 1 - tests/test_numpy_vectorize.py | 1 + tests/test_opaque_types.py | 3 ++- tests/test_operator_overloading.py | 3 ++- tests/test_pickling.py | 1 - tests/test_pytypes.py | 7 ++++--- tests/test_sequences_and_iterators.py | 3 ++- tests/test_stl.py | 3 +-- tests/test_stl_binders.py | 1 - tools/libsize.py | 3 ++- tools/make_changelog.py | 2 -- 45 files changed, 73 insertions(+), 67 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ee61258d3..a5900df16 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,6 +35,11 @@ repos: hooks: - id: pyupgrade +- repo: https://github.com/PyCQA/isort + rev: 5.9.3 + hooks: + - id: isort + # Black, the code formatter, natively supports pre-commit - repo: https://github.com/psf/black rev: 21.7b0 diff --git a/docs/benchmark.py b/docs/benchmark.py index 33d78fb4e..369470c81 100644 --- a/docs/benchmark.py +++ b/docs/benchmark.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- -import random -import os -import time import datetime as dt +import os +import random +import time nfns = 4 # Functions per class nargs = 4 # Arguments per function diff --git a/docs/conf.py b/docs/conf.py index 6ac054c6b..458a86886 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -13,12 +13,12 @@ # All configuration values have a default; values that are commented out # serve to show the default. -import sys import os +import re import shlex import subprocess +import sys from pathlib import Path -import re DIR = Path(__file__).parent.resolve() diff --git a/noxfile.py b/noxfile.py index f0f668015..55b1d180b 100644 --- a/noxfile.py +++ b/noxfile.py @@ -1,6 +1,5 @@ import nox - nox.options.sessions = ["lint", "tests", "tests_packaging"] diff --git a/pybind11/__init__.py b/pybind11/__init__.py index ad6542089..64e999ba0 100644 --- a/pybind11/__init__.py +++ b/pybind11/__init__.py @@ -1,8 +1,7 @@ # -*- coding: utf-8 -*- -from ._version import version_info, __version__ -from .commands import get_include, get_cmake_dir - +from ._version import __version__, version_info +from .commands import get_cmake_dir, get_include __all__ = ( "version_info", diff --git a/pybind11/__main__.py b/pybind11/__main__.py index 020988c67..3235747be 100644 --- a/pybind11/__main__.py +++ b/pybind11/__main__.py @@ -5,7 +5,7 @@ import argparse import sys import sysconfig -from .commands import get_include, get_cmake_dir +from .commands import get_cmake_dir, get_include def print_includes(): diff --git a/pybind11/_version.pyi b/pybind11/_version.pyi index 970184c77..d45e5dc90 100644 --- a/pybind11/_version.pyi +++ b/pybind11/_version.pyi @@ -1,4 +1,4 @@ -from typing import Union, Tuple +from typing import Tuple, Union def _to_int(s: str) -> Union[int, str]: ... diff --git a/pybind11/commands.py b/pybind11/commands.py index 34dbaf8a8..11f81d2d6 100644 --- a/pybind11/commands.py +++ b/pybind11/commands.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- import os - DIR = os.path.abspath(os.path.dirname(__file__)) diff --git a/pybind11/setup_helpers.py b/pybind11/setup_helpers.py index 0888ab487..4ff1a0cb3 100644 --- a/pybind11/setup_helpers.py +++ b/pybind11/setup_helpers.py @@ -41,23 +41,23 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import contextlib import os +import platform import shutil import sys +import sysconfig import tempfile import threading -import platform import warnings -import sysconfig try: - from setuptools.command.build_ext import build_ext as _build_ext from setuptools import Extension as _Extension + from setuptools.command.build_ext import build_ext as _build_ext except ImportError: from distutils.command.build_ext import build_ext as _build_ext from distutils.extension import Extension as _Extension -import distutils.errors import distutils.ccompiler +import distutils.errors WIN = sys.platform.startswith("win32") and "mingw" not in sysconfig.get_platform() PY2 = sys.version_info[0] < 3 diff --git a/pybind11/setup_helpers.pyi b/pybind11/setup_helpers.pyi index 8b16c0a13..074744eb8 100644 --- a/pybind11/setup_helpers.pyi +++ b/pybind11/setup_helpers.pyi @@ -1,13 +1,12 @@ # IMPORTANT: Should stay in sync with setup_helpers.py (mostly checked by CI / # pre-commit). -from typing import Any, Callable, Dict, Iterator, List, Optional, Type, TypeVar, Union -from types import TracebackType - +import contextlib +import distutils.ccompiler from distutils.command.build_ext import build_ext as _build_ext # type: ignore from distutils.extension import Extension as _Extension -import distutils.ccompiler -import contextlib +from types import TracebackType +from typing import Any, Callable, Dict, Iterator, List, Optional, Type, TypeVar, Union WIN: bool PY2: bool diff --git a/pyproject.toml b/pyproject.toml index f86470eef..7d7a1c821 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,6 +15,12 @@ ignore = [ "noxfile.py", ] +[tool.isort] +# Needs the compiled .so modules and env.py from tests +known_first_party = "env,pybind11_cross_module_tests,pybind11_tests," +# For black compatibility +profile = "black" + [tool.mypy] files = "pybind11" python_version = "2.7" diff --git a/setup.py b/setup.py index 7aa151c01..a2326287d 100644 --- a/setup.py +++ b/setup.py @@ -4,6 +4,7 @@ # Setup script for PyPI; use CMakeFile.txt to build extension modules import contextlib +import io import os import re import shutil @@ -11,7 +12,6 @@ import string import subprocess import sys import tempfile -import io import setuptools.command.sdist diff --git a/tests/extra_setuptools/test_setuphelper.py b/tests/extra_setuptools/test_setuphelper.py index fb2d34208..c24f50af8 100644 --- a/tests/extra_setuptools/test_setuphelper.py +++ b/tests/extra_setuptools/test_setuphelper.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import os -import sys import subprocess +import sys from textwrap import dedent import pytest diff --git a/tests/test_buffers.py b/tests/test_buffers.py index 50845758b..e3df7e04f 100644 --- a/tests/test_buffers.py +++ b/tests/test_buffers.py @@ -1,14 +1,13 @@ # -*- coding: utf-8 -*- +import ctypes import io import struct -import ctypes import pytest import env # noqa: F401 - -from pybind11_tests import buffers as m from pybind11_tests import ConstructorStats +from pybind11_tests import buffers as m np = pytest.importorskip("numpy") diff --git a/tests/test_builtin_casters.py b/tests/test_builtin_casters.py index 1b9dbb2b4..09b4b08c5 100644 --- a/tests/test_builtin_casters.py +++ b/tests/test_builtin_casters.py @@ -2,9 +2,8 @@ import pytest import env # noqa: F401 - +from pybind11_tests import IncType, UserType from pybind11_tests import builtin_casters as m -from pybind11_tests import UserType, IncType def test_simple_string(): diff --git a/tests/test_call_policies.py b/tests/test_call_policies.py index af017e9d0..3599cf81a 100644 --- a/tests/test_call_policies.py +++ b/tests/test_call_policies.py @@ -2,9 +2,8 @@ import pytest import env # noqa: F401 - -from pybind11_tests import call_policies as m from pybind11_tests import ConstructorStats +from pybind11_tests import call_policies as m @pytest.mark.xfail("env.PYPY", reason="sometimes comes out 1 off on PyPy", strict=False) diff --git a/tests/test_callbacks.py b/tests/test_callbacks.py index 5bc4d1773..9dc272a2d 100644 --- a/tests/test_callbacks.py +++ b/tests/test_callbacks.py @@ -1,9 +1,11 @@ # -*- coding: utf-8 -*- -import pytest -from pybind11_tests import callbacks as m -from threading import Thread import time +from threading import Thread + +import pytest + import env # NOQA: F401 +from pybind11_tests import callbacks as m def test_callbacks(): diff --git a/tests/test_chrono.py b/tests/test_chrono.py index 12312c720..fdd73d690 100644 --- a/tests/test_chrono.py +++ b/tests/test_chrono.py @@ -1,9 +1,10 @@ # -*- coding: utf-8 -*- -from pybind11_tests import chrono as m import datetime + import pytest import env # noqa: F401 +from pybind11_tests import chrono as m def test_chrono_system_clock(): diff --git a/tests/test_class.py b/tests/test_class.py index 1ff60fe21..85d453199 100644 --- a/tests/test_class.py +++ b/tests/test_class.py @@ -2,9 +2,8 @@ import pytest import env # noqa: F401 - +from pybind11_tests import ConstructorStats, UserType from pybind11_tests import class_ as m -from pybind11_tests import UserType, ConstructorStats def test_repr(): diff --git a/tests/test_cmake_build/test.py b/tests/test_cmake_build/test.py index 87ed5135f..d1a290dcc 100644 --- a/tests/test_cmake_build/test.py +++ b/tests/test_cmake_build/test.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import sys + import test_cmake_build assert test_cmake_build.add(1, 2) == 3 diff --git a/tests/test_copy_move.py b/tests/test_copy_move.py index 1d9895220..eb1efddd5 100644 --- a/tests/test_copy_move.py +++ b/tests/test_copy_move.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import pytest + from pybind11_tests import copy_move_policies as m diff --git a/tests/test_custom_type_casters.py b/tests/test_custom_type_casters.py index bb74d54eb..a10646ff4 100644 --- a/tests/test_custom_type_casters.py +++ b/tests/test_custom_type_casters.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import pytest + from pybind11_tests import custom_type_casters as m diff --git a/tests/test_eigen.py b/tests/test_eigen.py index a131dc15d..e53826cbb 100644 --- a/tests/test_eigen.py +++ b/tests/test_eigen.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import pytest + from pybind11_tests import ConstructorStats np = pytest.importorskip("numpy") diff --git a/tests/test_enum.py b/tests/test_enum.py index e9732fa74..62f9426ee 100644 --- a/tests/test_enum.py +++ b/tests/test_enum.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import pytest + from pybind11_tests import enums as m diff --git a/tests/test_eval.py b/tests/test_eval.py index 601e526f4..1bbd991bc 100644 --- a/tests/test_eval.py +++ b/tests/test_eval.py @@ -4,7 +4,6 @@ import os import pytest import env # noqa: F401 - from pybind11_tests import eval_ as m diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 966ae07fc..d1edc39f0 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -4,9 +4,8 @@ import sys import pytest import env # noqa: F401 - -from pybind11_tests import exceptions as m import pybind11_cross_module_tests as cm +from pybind11_tests import exceptions as m def test_std_exception(msg): diff --git a/tests/test_factory_constructors.py b/tests/test_factory_constructors.py index 8c6ca173a..8bc026985 100644 --- a/tests/test_factory_constructors.py +++ b/tests/test_factory_constructors.py @@ -1,12 +1,12 @@ # -*- coding: utf-8 -*- -import pytest import re -import env # noqa: F401 +import pytest +import env # noqa: F401 +from pybind11_tests import ConstructorStats from pybind11_tests import factory_constructors as m from pybind11_tests.factory_constructors import tag -from pybind11_tests import ConstructorStats def test_init_factory_basic(): diff --git a/tests/test_iostream.py b/tests/test_iostream.py index e2b74d01c..7f18ca65c 100644 --- a/tests/test_iostream.py +++ b/tests/test_iostream.py @@ -1,9 +1,9 @@ # -*- coding: utf-8 -*- -from pybind11_tests import iostream as m import sys - from contextlib import contextmanager +from pybind11_tests import iostream as m + try: # Python 3 from io import StringIO diff --git a/tests/test_kwargs_and_defaults.py b/tests/test_kwargs_and_defaults.py index 12fe705b4..ddc387eeb 100644 --- a/tests/test_kwargs_and_defaults.py +++ b/tests/test_kwargs_and_defaults.py @@ -2,7 +2,6 @@ import pytest import env # noqa: F401 - from pybind11_tests import kwargs_and_defaults as m diff --git a/tests/test_local_bindings.py b/tests/test_local_bindings.py index a38564b6e..52b1b6335 100644 --- a/tests/test_local_bindings.py +++ b/tests/test_local_bindings.py @@ -2,7 +2,6 @@ import pytest import env # noqa: F401 - from pybind11_tests import local_bindings as m diff --git a/tests/test_methods_and_attributes.py b/tests/test_methods_and_attributes.py index 750ec02f8..21909654a 100644 --- a/tests/test_methods_and_attributes.py +++ b/tests/test_methods_and_attributes.py @@ -2,9 +2,8 @@ import pytest import env # noqa: F401 - -from pybind11_tests import methods_and_attributes as m from pybind11_tests import ConstructorStats +from pybind11_tests import methods_and_attributes as m def test_methods_and_attributes(): diff --git a/tests/test_modules.py b/tests/test_modules.py index 3390031af..49e1ea5e3 100644 --- a/tests/test_modules.py +++ b/tests/test_modules.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- +from pybind11_tests import ConstructorStats from pybind11_tests import modules as m from pybind11_tests.modules import subsubmodule as ms -from pybind11_tests import ConstructorStats def test_nested_modules(): @@ -54,18 +54,20 @@ def test_reference_internal(): def test_importing(): - from pybind11_tests.modules import OD from collections import OrderedDict + from pybind11_tests.modules import OD + assert OD is OrderedDict assert str(OD([(1, "a"), (2, "b")])) == "OrderedDict([(1, 'a'), (2, 'b')])" def test_pydoc(): """Pydoc needs to be able to provide help() for everything inside a pybind11 module""" - import pybind11_tests import pydoc + import pybind11_tests + assert pybind11_tests.__name__ == "pybind11_tests" assert pybind11_tests.__doc__ == "pybind11 test module" assert pydoc.text.docmodule(pybind11_tests) diff --git a/tests/test_multiple_inheritance.py b/tests/test_multiple_inheritance.py index e6a7f9762..a02c31300 100644 --- a/tests/test_multiple_inheritance.py +++ b/tests/test_multiple_inheritance.py @@ -2,7 +2,6 @@ import pytest import env # noqa: F401 - from pybind11_tests import ConstructorStats from pybind11_tests import multiple_inheritance as m diff --git a/tests/test_numpy_array.py b/tests/test_numpy_array.py index 548c84bab..69ba9d495 100644 --- a/tests/test_numpy_array.py +++ b/tests/test_numpy_array.py @@ -2,7 +2,6 @@ import pytest import env # noqa: F401 - from pybind11_tests import numpy_array as m np = pytest.importorskip("numpy") diff --git a/tests/test_numpy_dtypes.py b/tests/test_numpy_dtypes.py index 0a5881e49..6ea064d5b 100644 --- a/tests/test_numpy_dtypes.py +++ b/tests/test_numpy_dtypes.py @@ -4,7 +4,6 @@ import re import pytest import env # noqa: F401 - from pybind11_tests import numpy_dtypes as m np = pytest.importorskip("numpy") diff --git a/tests/test_numpy_vectorize.py b/tests/test_numpy_vectorize.py index 4e6b2d198..de5c9a607 100644 --- a/tests/test_numpy_vectorize.py +++ b/tests/test_numpy_vectorize.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import pytest + from pybind11_tests import numpy_vectorize as m np = pytest.importorskip("numpy") diff --git a/tests/test_opaque_types.py b/tests/test_opaque_types.py index 773794634..5495cb6b4 100644 --- a/tests/test_opaque_types.py +++ b/tests/test_opaque_types.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- import pytest -from pybind11_tests import opaque_types as m + from pybind11_tests import ConstructorStats, UserType +from pybind11_tests import opaque_types as m def test_string_list(): diff --git a/tests/test_operator_overloading.py b/tests/test_operator_overloading.py index 5dbfb32c8..b7137d159 100644 --- a/tests/test_operator_overloading.py +++ b/tests/test_operator_overloading.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- import pytest -from pybind11_tests import operators as m + from pybind11_tests import ConstructorStats +from pybind11_tests import operators as m def test_operator_overloading(): diff --git a/tests/test_pickling.py b/tests/test_pickling.py index 303213068..e39463d20 100644 --- a/tests/test_pickling.py +++ b/tests/test_pickling.py @@ -2,7 +2,6 @@ import pytest import env # noqa: F401 - from pybind11_tests import pickling as m try: diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 66d6d30a0..8a11b1872 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -1,12 +1,13 @@ # -*- coding: utf-8 -*- from __future__ import division -import pytest + import sys -import env # noqa: F401 +import pytest -from pybind11_tests import pytypes as m +import env # noqa: F401 from pybind11_tests import debug_enabled +from pybind11_tests import pytypes as m def test_int(doc): diff --git a/tests/test_sequences_and_iterators.py b/tests/test_sequences_and_iterators.py index 70fb697f9..a868c542c 100644 --- a/tests/test_sequences_and_iterators.py +++ b/tests/test_sequences_and_iterators.py @@ -1,7 +1,8 @@ # -*- coding: utf-8 -*- import pytest -from pybind11_tests import sequences_and_iterators as m + from pybind11_tests import ConstructorStats +from pybind11_tests import sequences_and_iterators as m def isclose(a, b, rel_tol=1e-05, abs_tol=0.0): diff --git a/tests/test_stl.py b/tests/test_stl.py index 6f0845904..3f63d6c3a 100644 --- a/tests/test_stl.py +++ b/tests/test_stl.py @@ -1,9 +1,8 @@ # -*- coding: utf-8 -*- import pytest +from pybind11_tests import ConstructorStats, UserType from pybind11_tests import stl as m -from pybind11_tests import UserType -from pybind11_tests import ConstructorStats def test_vector(doc): diff --git a/tests/test_stl_binders.py b/tests/test_stl_binders.py index 84132a2b4..475a9ec40 100644 --- a/tests/test_stl_binders.py +++ b/tests/test_stl_binders.py @@ -2,7 +2,6 @@ import pytest import env # noqa: F401 - from pybind11_tests import stl_binders as m diff --git a/tools/libsize.py b/tools/libsize.py index 589c317f0..1551477e6 100644 --- a/tools/libsize.py +++ b/tools/libsize.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- -from __future__ import print_function, division +from __future__ import division, print_function + import os import sys diff --git a/tools/make_changelog.py b/tools/make_changelog.py index 4e8fbf5b0..629c284d3 100755 --- a/tools/make_changelog.py +++ b/tools/make_changelog.py @@ -4,11 +4,9 @@ import re import ghapi.all - from rich import print from rich.syntax import Syntax - ENTRY = re.compile( r""" Suggested \s changelog \s entry: