Skip to content

Commit

Permalink
feat: The remove command is set to the default behavior. (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
hakancelikdev committed Jan 31, 2023
1 parent 33ead41 commit d209538
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 56 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,8 @@ publish:
docs:
pip install -e .[docs]
mkdocs serve

.PHONY: sync-main
sync-main:
git fetch origin main
git rebase origin/main
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.12.3
- run: pip install --upgrade pip && python -m pip install unimport==0.13.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.13.0] - YYYY-MM-DD

### Changed

- The remove command is set to the default behavior. #273

## [0.12.3] - 2022-12-04

### Added
Expand Down
8 changes: 4 additions & 4 deletions docs/tutorial/command-line-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ You can give as many file or directory paths as you want.
## Check
> (optional: default `True`) Prints which file the unused imports are in.
When the `--diff`, `--permission` and `--remove` flags are used, the `--check` flag set
as `False` If you still want to see the results, use the `--check` flag.
> (optional: default `False`) Prints which file the unused imports are in.
**Usage**
Expand Down Expand Up @@ -156,6 +153,9 @@ Ignore the **init**.py file.
> (optional: default `False`) remove unused imports automatically.
When the `--diff` and `--check` flags are not used, the `--remove` flag set as `True` If
you still want to remove the imports, use the `--remove` flag.
**Usage**
- `$ unimport -r`
Expand Down
2 changes: 2 additions & 0 deletions docs/tutorial/configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exclude = '__init__.py|tests/'
include = 'test|test2|tests.py'
gitignore = true
remove = false
check = true
diff = true
include_star_import = true
ignore_init = true
Expand All @@ -29,6 +30,7 @@ exclude = __init__.py|tests/
include = test|test2|tests.py
gitignore = true
remove = false
check = true
diff = true
include_star_import = true
ignore_init = true
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.12.2
uses: hakancelikdev/unimport@0.13.0
with:
extra_args: --include src/
```
1 change: 0 additions & 1 deletion docs/tutorial/use-with-pre-commit.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ repos:
hooks:
- id: unimport
args:
- --remove
- --include-star-import
- --ignore-init
- --gitignore
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ line-length = 120
target-version = ['py36', 'py37', 'py38', 'py39', 'py310']

[tool.unimport]
remove = true
include_start_import = true
ignore_init = true
gitignore = true
Expand Down
13 changes: 2 additions & 11 deletions src/unimport/commands/permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,8 @@
__all__ = ("permission",)


def permission(
path: Path,
encoding: str,
newline: str,
refactor_result: str,
use_color: bool,
) -> None:
def permission(path: Path, use_color: bool) -> bool:
action = input(f"Apply suggested changes to '{paint(str(path), Color.YELLOW, use_color)}' [Y/n/q] ? >").lower()
if action == "q":
raise SystemExit(1)
elif utils.action_to_bool(action):
from unimport import commands

commands.remove(path, encoding, newline, refactor_result, use_color)
return utils.action_to_bool(action)
2 changes: 1 addition & 1 deletion src/unimport/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __post_init__(self):
self.sources = self.default_sources

self.diff = self.diff or self.permission
self.check = self.check or not any((self.diff, self.remove))
self.remove = self.remove or not any((self.diff, self.check))
self.use_color: bool = self._use_color(self.color)

if self.gitignore:
Expand Down
13 changes: 4 additions & 9 deletions src/unimport/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,9 @@ def remove(self, result: _Result, refactor_result):
def diff(self, result, refactor_result):
return commands.diff(result.path, result.source, refactor_result, self.config.use_color)

def permission(self, result, refactor_result):
commands.permission(
result.path,
result.encoding,
result.newline,
refactor_result,
self.config.use_color,
)
@staticmethod
def permission(result) -> bool:
return commands.permission(result.path, result.encoding)

@classmethod
def run(cls, argv: Optional[Sequence[str]] = None) -> "Main":
Expand All @@ -118,7 +113,7 @@ def run(cls, argv: Optional[Sequence[str]] = None) -> "Main":
if self.config.diff:
exists_diff = self.diff(result, refactor_result)
if self.config.permission and exists_diff:
self.permission(result, refactor_result)
self.config.remove = self.permission(result)
if self.config.remove and result.source != refactor_result:
self.remove(result, refactor_result)
return self
Expand Down
15 changes: 1 addition & 14 deletions tests/config/configs/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
[tool.black]
line-length = 119
target-version = ['py37']
include = '\.pyi?$'
exclude = '''
(
/(
| just a
| multiline
)/
| test.py
)
'''

[tool.unimport]
sources = ["path1", "path2"]
exclude = '__init__.py|tests/'
include = 'test|test2|tests.py'
gitignore = false
remove = false
check = false
diff = false
ignore_init = false
1 change: 1 addition & 0 deletions tests/config/configs/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ exclude = __init__.py|tests/
include = test|test2|tests.py
gitignore = false
remove = false
check = false
diff = false
ignore_init = false
27 changes: 14 additions & 13 deletions tests/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def test_parse_config_toml_parse():
"include": "test|test2|tests.py",
"gitignore": False,
"remove": False,
"check": False,
"diff": False,
"ignore_init": False,
}
Expand All @@ -42,6 +43,7 @@ def test_parse_config_cfg_parse():
"include": "test|test2|tests.py",
"gitignore": False,
"remove": False,
"check": False,
"diff": False,
"ignore_init": False,
}
Expand Down Expand Up @@ -111,10 +113,8 @@ def test_config_build_default_command_same_with_default_config():
def test_config_build_default_check():
config = Config.build()

parser = generate_parser()
assert config.check is True
assert config.check is False
assert Config.build(args={"check": True}).check is True

assert Config.build(args={"diff": True}).check is False
assert Config.build(args={"remove": True}).check is False
assert Config.build(args={"permission": True}).check is False
Expand All @@ -123,26 +123,27 @@ def test_config_build_default_check():
def test_config_build_default_command_diff():
config = Config.build()

parser = generate_parser()

assert config.diff is False
assert Config.build(args={"remove": True}).diff is False
assert Config.build(args={"diff": True}).diff is True
assert Config.build(args={"permission": True}).diff is True


def test_config_build_default_remove():
config = Config.build()

assert config.remove is True
assert Config.build(args={"check": True}).remove is False
assert Config.build(args={"diff": True}).remove is False
assert Config.build(args={"remove": True}).remove is True
assert Config.build(args={"permission": True}).remove is False


def test_parse_config_toml_command_check():
config_context = ParseConfig(pyproject).parse()
parser = generate_parser()

assert Config.build(args={"check": True}, config_context=config_context).check is True
assert (
Config.build(
args={"gitignore": True},
config_context=config_context,
).check
is True
)
assert Config.build(args={"gitignore": True}, config_context=config_context).check is False
assert Config.build(args={"diff": True}, config_context=config_context).check is False
assert Config.build(args={"remove": True}, config_context=config_context).check is False
assert (
Expand Down
22 changes: 22 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from textwrap import dedent
from unittest import mock

import pytest

Expand Down Expand Up @@ -71,3 +72,24 @@ def test_exit_code():

main.refactor_applied = True
assert main.exit_code() == 0


@mock.patch("unimport.main.Main.permission")
def test_commands_in_run(mock_permission):
source = dedent(
"""\
import os
"""
)

mock_permission.return_value = False

assert Main([]).config.remove is True
assert Main([]).config.permission is False

with reopenable_temp_file(source) as temp_file:
main = Main.run([f"--permission", temp_file.as_posix()])

assert main.config.remove is False
assert main.config.permission is True

0 comments on commit d209538

Please sign in to comment.