From 321c5b55bd60acd384192dcaf3e297c8e3ffc628 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sun, 13 Mar 2022 19:51:00 -0400 Subject: [PATCH 01/11] remove unneeded gitignore lines - coverage-html: coverage>=6.2 writes a .gitignore file - mypy_cache: mypy>=0.770 writes a .gitignore file - pytest_cache: pytest>=3.8.1 writes a .gitignore file - venv: virtualenv>=20.0.21 writes a .gitignore file Committed via https://github.com/asottile/all-repos --- .gitignore | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.gitignore b/.gitignore index c447675..dc5942e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,4 @@ *.py[co] .coverage .tox -/.mypy_cache -/.pytest_cache -/venv* -coverage-html dist From 2062bd10cc691996e8255124b4bdce07777615a3 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 14 Mar 2022 14:26:36 -0400 Subject: [PATCH 02/11] remove _get_steps now that it's not needed --- reorder_python_imports.py | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/reorder_python_imports.py b/reorder_python_imports.py index 503804f..fc86f0e 100644 --- a/reorder_python_imports.py +++ b/reorder_python_imports.py @@ -4,7 +4,6 @@ import ast import collections import enum -import functools import io import os import sys @@ -368,21 +367,6 @@ def apply_import_sorting( return pre_import_code + new_imports + rest -def _get_steps( - imports_to_add: tuple[str, ...], - imports_to_remove: tuple[str, ...], - imports_to_replace: Iterable[ImportToReplace], - **sort_kwargs: Any, -) -> Generator[Step, None, None]: - yield combine_trailing_code_chunks - yield functools.partial(add_imports, to_add=imports_to_add) - yield separate_comma_imports - yield functools.partial(remove_imports, to_remove=imports_to_remove) - yield functools.partial(replace_imports, to_replace=imports_to_replace) - yield remove_duplicated_imports - yield functools.partial(apply_import_sorting, **sort_kwargs) - - def _most_common_line_ending(s: str) -> str: # initialize in case there's no line endings at all counts = collections.Counter({'\n': 0}) @@ -406,13 +390,14 @@ def fix_file_contents( contents = contents.replace('\r\n', '\n').replace('\r', '\n') partitioned = partition_source(contents) - for step in _get_steps( - imports_to_add, - imports_to_remove, - imports_to_replace, - **sort_kwargs, - ): - partitioned = step(partitioned) + partitioned = combine_trailing_code_chunks(partitioned) + partitioned = add_imports(partitioned, to_add=imports_to_add) + partitioned = separate_comma_imports(partitioned) + partitioned = remove_imports(partitioned, to_remove=imports_to_remove) + partitioned = replace_imports(partitioned, to_replace=imports_to_replace) + partitioned = remove_duplicated_imports(partitioned) + partitioned = apply_import_sorting(partitioned, **sort_kwargs) + return _partitions_to_src(partitioned).replace('\n', nl) From d2c6d5f63d6d42d60a62e5fe601172029842a077 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Mar 2022 00:41:52 +0000 Subject: [PATCH 03/11] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/pyupgrade: v2.31.0 → v2.31.1](https://github.com/asottile/pyupgrade/compare/v2.31.0...v2.31.1) - [github.com/pre-commit/mirrors-mypy: v0.931 → v0.940](https://github.com/pre-commit/mirrors-mypy/compare/v0.931...v0.940) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 521a49b..a810c76 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -25,7 +25,7 @@ repos: - id: reorder-python-imports args: [--py37-plus, --add-import, 'from __future__ import annotations'] - repo: https://github.com/asottile/pyupgrade - rev: v2.31.0 + rev: v2.31.1 hooks: - id: pyupgrade args: [--py37-plus] @@ -43,7 +43,7 @@ repos: hooks: - id: python-use-type-annotations - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.931 + rev: v0.940 hooks: - id: mypy additional_dependencies: [types-all] From 81b344c125052d40572f68d4d9abda962c755214 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 22 Mar 2022 00:54:55 +0000 Subject: [PATCH 04/11] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/mirrors-mypy: v0.940 → v0.941](https://github.com/pre-commit/mirrors-mypy/compare/v0.940...v0.941) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a810c76..b7c94d8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -43,7 +43,7 @@ repos: hooks: - id: python-use-type-annotations - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.940 + rev: v0.941 hooks: - id: mypy additional_dependencies: [types-all] From 89748705bed0cb7160a83f92c67260ef68ce7bc9 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Thu, 24 Mar 2022 14:48:57 -0400 Subject: [PATCH 05/11] Update default branch to main Committed via https://github.com/asottile/all-repos --- README.md | 6 +++--- azure-pipelines.yml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 12b1ca1..8b2331e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -[![Build Status](https://dev.azure.com/asottile/asottile/_apis/build/status/asottile.reorder_python_imports?branchName=master)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=28&branchName=master) -[![Azure DevOps coverage](https://img.shields.io/azure-devops/coverage/asottile/asottile/28/master.svg)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=28&branchName=master) -[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/asottile/reorder_python_imports/master.svg)](https://results.pre-commit.ci/latest/github/asottile/reorder_python_imports/master) +[![Build Status](https://dev.azure.com/asottile/asottile/_apis/build/status/asottile.reorder_python_imports?branchName=main)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=28&branchName=main) +[![Azure DevOps coverage](https://img.shields.io/azure-devops/coverage/asottile/asottile/28/main.svg)](https://dev.azure.com/asottile/asottile/_build/latest?definitionId=28&branchName=main) +[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/asottile/reorder_python_imports/main.svg)](https://results.pre-commit.ci/latest/github/asottile/reorder_python_imports/main) reorder_python_imports ====================== diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 131e765..ada0df8 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -1,6 +1,6 @@ trigger: branches: - include: [master, test-me-*] + include: [main, test-me-*] tags: include: ['*'] From 616ce88b8d84350011ddc20a214168ae0cbdc747 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 25 Mar 2022 14:05:05 -0400 Subject: [PATCH 06/11] reorder pre-commit config Committed via https://github.com/asottile/all-repos --- .pre-commit-config.yaml | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b7c94d8..6f1fafd 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,46 +4,40 @@ repos: hooks: - id: trailing-whitespace - id: end-of-file-fixer - - id: check-docstring-first - id: check-yaml - id: debug-statements - id: double-quote-string-fixer - id: name-tests-test - id: requirements-txt-fixer -- repo: https://github.com/PyCQA/flake8 - rev: 4.0.1 - hooks: - - id: flake8 - additional_dependencies: ['flake8-typing-imports==1.12.0'] -- repo: https://github.com/pre-commit/mirrors-autopep8 - rev: v1.6.0 +- repo: https://github.com/asottile/setup-cfg-fmt + rev: v1.20.0 hooks: - - id: autopep8 + - id: setup-cfg-fmt - repo: https://github.com/asottile/reorder_python_imports rev: v3.0.1 hooks: - id: reorder-python-imports args: [--py37-plus, --add-import, 'from __future__ import annotations'] -- repo: https://github.com/asottile/pyupgrade - rev: v2.31.1 - hooks: - - id: pyupgrade - args: [--py37-plus] - repo: https://github.com/asottile/add-trailing-comma rev: v2.2.1 hooks: - id: add-trailing-comma args: [--py36-plus] -- repo: https://github.com/asottile/setup-cfg-fmt - rev: v1.20.0 +- repo: https://github.com/asottile/pyupgrade + rev: v2.31.1 hooks: - - id: setup-cfg-fmt -- repo: https://github.com/pre-commit/pygrep-hooks - rev: v1.9.0 + - id: pyupgrade + args: [--py37-plus] +- repo: https://github.com/pre-commit/mirrors-autopep8 + rev: v1.6.0 hooks: - - id: python-use-type-annotations + - id: autopep8 +- repo: https://github.com/PyCQA/flake8 + rev: 4.0.1 + hooks: + - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.941 + rev: v0.942 hooks: - id: mypy additional_dependencies: [types-all] From a1b7a0c845f4cc66e3258dc48d477c2c508a41dc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 Apr 2022 21:35:07 +0000 Subject: [PATCH 07/11] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/setup-cfg-fmt: v1.20.0 → v1.20.1](https://github.com/asottile/setup-cfg-fmt/compare/v1.20.0...v1.20.1) - [github.com/asottile/add-trailing-comma: v2.2.1 → v2.2.2](https://github.com/asottile/add-trailing-comma/compare/v2.2.1...v2.2.2) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6f1fafd..c7edbe9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,7 +10,7 @@ repos: - id: name-tests-test - id: requirements-txt-fixer - repo: https://github.com/asottile/setup-cfg-fmt - rev: v1.20.0 + rev: v1.20.1 hooks: - id: setup-cfg-fmt - repo: https://github.com/asottile/reorder_python_imports @@ -19,7 +19,7 @@ repos: - id: reorder-python-imports args: [--py37-plus, --add-import, 'from __future__ import annotations'] - repo: https://github.com/asottile/add-trailing-comma - rev: v2.2.1 + rev: v2.2.2 hooks: - id: add-trailing-comma args: [--py36-plus] From 2f22927c8ed90c7184622ac47810c61f68ed2615 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 11 Apr 2022 22:55:25 +0000 Subject: [PATCH 08/11] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/pre-commit-hooks: v4.1.0 → v4.2.0](https://github.com/pre-commit/pre-commit-hooks/compare/v4.1.0...v4.2.0) - [github.com/asottile/pyupgrade: v2.31.1 → v2.32.0](https://github.com/asottile/pyupgrade/compare/v2.31.1...v2.32.0) --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c7edbe9..823ef3e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.1.0 + rev: v4.2.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer @@ -24,7 +24,7 @@ repos: - id: add-trailing-comma args: [--py36-plus] - repo: https://github.com/asottile/pyupgrade - rev: v2.31.1 + rev: v2.32.0 hooks: - id: pyupgrade args: [--py37-plus] From 59f320b06c2a03fa7f7c3397889d172129154c17 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 18 Apr 2022 21:25:51 +0000 Subject: [PATCH 09/11] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/asottile/add-trailing-comma: v2.2.2 → v2.2.3](https://github.com/asottile/add-trailing-comma/compare/v2.2.2...v2.2.3) --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 823ef3e..fcff0fc 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -19,7 +19,7 @@ repos: - id: reorder-python-imports args: [--py37-plus, --add-import, 'from __future__ import annotations'] - repo: https://github.com/asottile/add-trailing-comma - rev: v2.2.2 + rev: v2.2.3 hooks: - id: add-trailing-comma args: [--py36-plus] From c43c7d95fe2e962df411360a088f3349dced0246 Mon Sep 17 00:00:00 2001 From: Aneesh Agrawal Date: Fri, 22 Apr 2022 10:49:49 -0400 Subject: [PATCH 10/11] Teach --py3-plus to rewrite direct six module imports Impetus for this is to automatically rewrite `from six.moves.urllib import parse` to `from urllib import parse`. --- reorder_python_imports.py | 14 ++++++++ tests/reorder_python_imports_test.py | 48 ++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/reorder_python_imports.py b/reorder_python_imports.py index fc86f0e..445b31a 100644 --- a/reorder_python_imports.py +++ b/reorder_python_imports.py @@ -244,6 +244,20 @@ def _inner() -> Generator[CodePartition, None, None]: new_src = import_obj.to_text() yield partition._replace(src=new_src) break + # from a.b.c import d => from c import d + elif ( + (mod_parts + [symbol] == orig_mod) and + not attr and + len(new_mod) > 1 and + (asname or symbol == new_mod[-1]) + ): + import_obj.ast_obj.module = '.'.join(new_mod[:-1]) + import_obj.ast_obj.names = [ + ast.alias(name=new_mod[-1], asname=asname), + ] + new_src = import_obj.to_text() + yield partition._replace(src=new_src) + break # from x.y import z => import z elif ( not attr and diff --git a/tests/reorder_python_imports_test.py b/tests/reorder_python_imports_test.py index 9fd49c8..a476762 100644 --- a/tests/reorder_python_imports_test.py +++ b/tests/reorder_python_imports_test.py @@ -524,6 +524,54 @@ def test_replace_module_imported_asname(): assert ret == 'import queue as Queue\n' +def test_replace_module_imported_with_nested_replacement(): + ret = fix_file_contents( + 'from six.moves.urllib import parse\n', + imports_to_replace=[ + (['six', 'moves', 'urllib', 'parse'], ['urllib', 'parse'], ''), + ], + ) + assert ret == 'from urllib import parse\n' + + +def test_replace_module_imported_with_nested_replacement_asname(): + ret = fix_file_contents( + 'from six.moves.urllib import parse as urllib_parse\n', + imports_to_replace=[ + (['six', 'moves', 'urllib', 'parse'], ['urllib', 'parse'], ''), + ], + ) + assert ret == 'from urllib import parse as urllib_parse\n' + + +def test_replace_module_imported_with_nested_renamed_replacement_asname(): + ret = fix_file_contents( + 'from six.moves.urllib import parse as urllib_parse\n', + imports_to_replace=[ + (['six', 'moves', 'urllib', 'parse'], ['urllib', 'parse2'], ''), + ], + ) + assert ret == 'from urllib import parse2 as urllib_parse\n' + + +def test_replace_module_skips_attr_specific_rules(): + ret = fix_file_contents( + 'from libone import util\n', + imports_to_replace=[ + (['libone', 'util'], ['libtwo', 'util'], 'is_valid'), + ], + ) + assert ret == 'from libone import util\n' + + +def test_replace_module_skips_nonmatching_rules(): + ret = fix_file_contents( + 'from libthree import util\n', + imports_to_replace=[(['libone', 'util'], ['libtwo', 'util'], '')], + ) + assert ret == 'from libthree import util\n' + + cases = pytest.mark.parametrize( ('s', 'expected'), ( From 7e670a6382a3fbbb1844aedf4e36b0778bbbdab6 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Tue, 26 Apr 2022 17:58:13 -0700 Subject: [PATCH 11/11] v3.1.0 --- .pre-commit-config.yaml | 2 +- README.md | 2 +- setup.cfg | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index fcff0fc..5244a5d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,7 +14,7 @@ repos: hooks: - id: setup-cfg-fmt - repo: https://github.com/asottile/reorder_python_imports - rev: v3.0.1 + rev: v3.1.0 hooks: - id: reorder-python-imports args: [--py37-plus, --add-import, 'from __future__ import annotations'] diff --git a/README.md b/README.md index 8b2331e..3049a2a 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ Sample `.pre-commit-config.yaml` ```yaml - repo: https://github.com/asottile/reorder_python_imports - rev: v3.0.1 + rev: v3.1.0 hooks: - id: reorder-python-imports ``` diff --git a/setup.cfg b/setup.cfg index f6bd373..c0ea600 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = reorder_python_imports -version = 3.0.1 +version = 3.1.0 description = Tool for reordering python imports long_description = file: README.md long_description_content_type = text/markdown