Skip to content

Commit

Permalink
#284 Fix: attribute as import refactor issue
Browse files Browse the repository at this point in the history
  • Loading branch information
hakancelikdev committed Mar 30, 2023
1 parent 955908b commit b5fb1d5
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 9 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/tutorial/use-with-github-action.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/
```
2 changes: 1 addition & 1 deletion src/unimport/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.14.1"
__version__ = "0.15.0"
__description__ = "A linter, formatter for finding and removing unused import statements."
5 changes: 4 additions & 1 deletion src/unimport/refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions tests/cases/analyzer/as_import/dotted_module_name_simple.py
Original file line number Diff line number Diff line change
@@ -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"),
]
14 changes: 14 additions & 0 deletions tests/cases/analyzer/dotted/dotted_module_name_simple.py
Original file line number Diff line number Diff line change
@@ -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"),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/cases/refactor/dotted/dotted_module_name_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions tests/cases/source/as_import/dotted_module_name_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import a.b as c
1 change: 1 addition & 0 deletions tests/cases/source/dotted/dotted_module_name_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import a.b

0 comments on commit b5fb1d5

Please sign in to comment.