diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2af2c8ae..ffcdf175 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ exclude: tests/cases/(refactor|source).* repos: - repo: https://github.com/psf/black - rev: 23.1.0 + rev: 23.3.0 hooks: - id: black @@ -12,24 +12,24 @@ repos: - id: isort - repo: https://github.com/hakancelikdev/unimport - rev: 0.14.1 + rev: 0.15.0 hooks: - id: unimport - repo: https://github.com/PyCQA/docformatter - rev: v1.6.0.rc1 + rev: v1.5.1 hooks: - id: docformatter args: [--config=pyproject.toml] - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.991 + rev: v1.1.1 hooks: - id: mypy additional_dependencies: [types-toml==0.1.3] - repo: https://github.com/pre-commit/mirrors-prettier - rev: v3.0.0-alpha.4 + rev: v3.0.0-alpha.6 hooks: - id: prettier args: [--prose-wrap=always, --print-width=88] diff --git a/action.yml b/action.yml index 95c1ec27..c5f0e26e 100644 --- a/action.yml +++ b/action.yml @@ -10,7 +10,7 @@ inputs: runs: using: "composite" steps: - - run: pip install --upgrade pip && python -m pip install unimport==0.14.1 + - run: pip install --upgrade pip && python -m pip install unimport==0.15.0 shell: bash - run: unimport --color auto --gitignore --ignore-init ${{ inputs.extra_args }} shell: bash diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 964dda92..9e7d46c5 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. ## [Unreleased] - YYYY-MM-DD +## [0.15.0] - 2023-03-31 + +### Fixed + +- Fix: attribute as import refactor #284 + ## [0.14.1] - 2023-02-04 ### Fixed diff --git a/docs/tutorial/use-with-github-action.md b/docs/tutorial/use-with-github-action.md index ab002b6d..38f3af35 100644 --- a/docs/tutorial/use-with-github-action.md +++ b/docs/tutorial/use-with-github-action.md @@ -27,7 +27,7 @@ jobs: - uses: actions/checkout@v3 - uses: actions/setup-python@v3 - name: Check unused imports - uses: hakancelikdev/unimport@0.14.1 + uses: hakancelikdev/unimport@0.15.0 with: extra_args: --include src/ ``` diff --git a/src/unimport/__init__.py b/src/unimport/__init__.py index 2875c878..8b616942 100644 --- a/src/unimport/__init__.py +++ b/src/unimport/__init__.py @@ -1,2 +1,2 @@ -__version__ = "0.14.1" +__version__ = "0.15.0" __description__ = "A linter, formatter for finding and removing unused import statements." diff --git a/src/unimport/refactor.py b/src/unimport/refactor.py index 3d0293ec..264c02f4 100644 --- a/src/unimport/refactor.py +++ b/src/unimport/refactor.py @@ -64,7 +64,10 @@ def leave_import_alike( # already handled by leave_ImportFrom for column, import_alias in enumerate(names): if isinstance(import_alias.name, cst.Attribute): - import_name = self.get_import_name_from_attr(attr_node=import_alias.name) + if import_alias.asname: + import_name = import_alias.asname.name.value + else: + import_name = self.get_import_name_from_attr(attr_node=import_alias.name) else: raw_import = import_alias.asname or import_alias raw_import_name = cst.ensure_type(raw_import.name, cst.Name) diff --git a/tests/cases/analyzer/as_import/dotted_module_name_simple.py b/tests/cases/analyzer/as_import/dotted_module_name_simple.py new file mode 100644 index 00000000..46e8c038 --- /dev/null +++ b/tests/cases/analyzer/as_import/dotted_module_name_simple.py @@ -0,0 +1,14 @@ +from typing import List, Union + +from unimport.statement import Import, ImportFrom, Name + +__all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] + + +NAMES: List[Name] = [] +IMPORTS: List[Union[Import, ImportFrom]] = [ + Import(lineno=1, column=1, name="c", package="a.b"), +] +UNUSED_IMPORTS: List[Union[Import, ImportFrom]] = [ + Import(lineno=1, column=1, name="c", package="a.b"), +] diff --git a/tests/cases/analyzer/dotted/dotted_module_name_simple.py b/tests/cases/analyzer/dotted/dotted_module_name_simple.py new file mode 100644 index 00000000..482c8bf2 --- /dev/null +++ b/tests/cases/analyzer/dotted/dotted_module_name_simple.py @@ -0,0 +1,14 @@ +from typing import List, Union + +from unimport.statement import Import, ImportFrom, Name + +__all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] + + +NAMES: List[Name] = [] +IMPORTS: List[Union[Import, ImportFrom]] = [ + Import(lineno=1, column=1, name="a.b", package="a.b"), +] +UNUSED_IMPORTS: List[Union[Import, ImportFrom]] = [ + Import(lineno=1, column=1, name="a.b", package="a.b"), +] diff --git a/tests/cases/refactor/as_import/dotted_module_name_simple.py b/tests/cases/refactor/as_import/dotted_module_name_simple.py new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/tests/cases/refactor/as_import/dotted_module_name_simple.py @@ -0,0 +1 @@ + diff --git a/tests/cases/refactor/dotted/dotted_module_name_simple.py b/tests/cases/refactor/dotted/dotted_module_name_simple.py new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/tests/cases/refactor/dotted/dotted_module_name_simple.py @@ -0,0 +1 @@ + diff --git a/tests/cases/source/as_import/dotted_module_name_simple.py b/tests/cases/source/as_import/dotted_module_name_simple.py new file mode 100644 index 00000000..62ba4cfd --- /dev/null +++ b/tests/cases/source/as_import/dotted_module_name_simple.py @@ -0,0 +1 @@ +import a.b as c diff --git a/tests/cases/source/dotted/dotted_module_name_simple.py b/tests/cases/source/dotted/dotted_module_name_simple.py new file mode 100644 index 00000000..1fc44691 --- /dev/null +++ b/tests/cases/source/dotted/dotted_module_name_simple.py @@ -0,0 +1 @@ +import a.b