From 919297555f17efedcdcc7cc936204a1b86b804d3 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 28 Feb 2023 10:58:22 -0500 Subject: ensure single import per line This adds the very small plugin flake8-import-single which will prevent us from having an import with more than one symbol on a line. Flake8 by itself prevents this pattern with E401: import collections, os, sys However does not do anything with this: from sqlalchemy import Column, text Both statements have the same issues generating merge artifacts as well as presenting a manual decision to be made. While zimports generally cleans up such imports at the top level, we don't enforce zimports / pre-commit use. the plugin finds the same issue for imports that are inside of test methods. We shouldn't usually have imports in test methods so most of them here are moved to be top level. The version is pinned at 0.1.5; the project seems to have no activity since 2019, however there are three 0.1.6dev releases on pypi which stopped in September 2019, they seem to be experiments with packaging. The source for 0.1.5 is extremely simple and only reveals one method to flake8 (the run() method). Change-Id: Icea894e43bad9c0b5d4feb5f49c6c666d6ea6aa1 --- .pre-commit-config.yaml | 1 + alembic/testing/env.py | 3 ++- tests/test_autogen_render.py | 4 +--- tests/test_op.py | 9 +++------ tests/test_postgresql.py | 3 ++- tests/test_script_production.py | 4 +++- tox.ini | 1 + 7 files changed, 13 insertions(+), 12 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 304db47..c4397b9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -19,6 +19,7 @@ repos: - id: flake8 additional_dependencies: - flake8-import-order + - flake8-import-single==0.1.5 - flake8-builtins - flake8-docstrings - flake8-rst-docstrings diff --git a/alembic/testing/env.py b/alembic/testing/env.py index 3d42f1c..79a4980 100644 --- a/alembic/testing/env.py +++ b/alembic/testing/env.py @@ -7,6 +7,8 @@ from sqlalchemy.testing import config from sqlalchemy.testing import provision from . import util as testing_util +from .. import command +from .. import script from .. import util from ..script import Script from ..script import ScriptDirectory @@ -20,7 +22,6 @@ def _get_staging_directory(): def staging_env(create=True, template="generic", sourceless=False): - from alembic import command, script cfg = _testing_config() if create: diff --git a/tests/test_autogen_render.py b/tests/test_autogen_render.py index f138df5..ada2a12 100644 --- a/tests/test_autogen_render.py +++ b/tests/test_autogen_render.py @@ -25,6 +25,7 @@ from sqlalchemy import text from sqlalchemy import types from sqlalchemy import Unicode from sqlalchemy import UniqueConstraint +from sqlalchemy import VARCHAR from sqlalchemy.engine.default import DefaultDialect from sqlalchemy.sql import and_ from sqlalchemy.sql import column @@ -1792,7 +1793,6 @@ class AutogenRenderTest(TestBase): ) def test_render_variant(self): - from sqlalchemy import VARCHAR, CHAR self.autogen_context.opts["user_module_prefix"] = None @@ -1824,8 +1824,6 @@ class AutogenRenderTest(TestBase): ) def test_repr_user_type_user_prefix_present(self): - from sqlalchemy.types import UserDefinedType - class MyType(UserDefinedType): def get_col_spec(self): return "MYTYPE" diff --git a/tests/test_op.py b/tests/test_op.py index 5682abb..c483c4a 100644 --- a/tests/test_op.py +++ b/tests/test_op.py @@ -14,10 +14,13 @@ from sqlalchemy import Table from sqlalchemy import UniqueConstraint from sqlalchemy.sql import column from sqlalchemy.sql import func +from sqlalchemy.sql import table from sqlalchemy.sql import text from sqlalchemy.sql.schema import quoted_name from alembic import op +from alembic.operations import MigrateOperation +from alembic.operations import Operations from alembic.operations import ops from alembic.operations import schemaobj from alembic.testing import assert_raises_message @@ -1023,8 +1026,6 @@ class OpTest(TestBase): def test_inline_literal(self): context = op_fixture() - from sqlalchemy.sql import table, column - from sqlalchemy import String, Integer account = table( "account", column("name", String), column("id", Integer) @@ -1144,8 +1145,6 @@ class OpTest(TestBase): class SQLModeOpTest(TestBase): def test_auto_literals(self): context = op_fixture(as_sql=True, literal_binds=True) - from sqlalchemy.sql import table, column - from sqlalchemy import String, Integer account = table( "account", column("name", String), column("id", Integer) @@ -1179,8 +1178,6 @@ class SQLModeOpTest(TestBase): class CustomOpTest(TestBase): def test_custom_op(self): - from alembic.operations import Operations, MigrateOperation - @Operations.register_operation("create_sequence") class CreateSequenceOp(MigrateOperation): """Create a SEQUENCE.""" diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index 6a67e0b..23d6a8c 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -19,9 +19,11 @@ from sqlalchemy import types from sqlalchemy import UniqueConstraint from sqlalchemy.dialects.postgresql import ARRAY from sqlalchemy.dialects.postgresql import BYTEA +from sqlalchemy.dialects.postgresql import ExcludeConstraint from sqlalchemy.dialects.postgresql import HSTORE from sqlalchemy.dialects.postgresql import JSON from sqlalchemy.dialects.postgresql import JSONB +from sqlalchemy.dialects.postgresql import TSRANGE from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.sql import column from sqlalchemy.sql import false @@ -1248,7 +1250,6 @@ class PGUniqueIndexAutogenerateTest(AutogenFixtureTest, TestBase): @config.requirements.btree_gist def test_exclude_const_unchanged(self): - from sqlalchemy.dialects.postgresql import TSRANGE, ExcludeConstraint m1 = MetaData() m2 = MetaData() diff --git a/tests/test_script_production.py b/tests/test_script_production.py index bedf545..5c4cd9e 100644 --- a/tests/test_script_production.py +++ b/tests/test_script_production.py @@ -5,7 +5,10 @@ from unittest.mock import patch from dateutil import tz import sqlalchemy as sa +from sqlalchemy import Column from sqlalchemy import inspect +from sqlalchemy import MetaData +from sqlalchemy import Table from alembic import autogenerate from alembic import command @@ -717,7 +720,6 @@ class ImportsTest(TestBase): ) def test_imports_in_script(self): - from sqlalchemy import MetaData, Table, Column from sqlalchemy.dialects.mysql import VARCHAR type_ = VARCHAR(20, charset="utf8", national=True) diff --git a/tox.ini b/tox.ini index 8b744d7..08cc4f4 100644 --- a/tox.ini +++ b/tox.ini @@ -90,6 +90,7 @@ basepython = python3 deps= flake8 flake8-import-order + flake8-import-single==0.1.5 flake8-builtins flake8-docstrings flake8-rst-docstrings -- cgit v1.2.1