Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General refactoring #108

Merged
merged 15 commits into from
Sep 20, 2020
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ jobs:
- name: Generate coverage report
run: |
pytest --cov=./ --cov-report=xml

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1.0.7
with:
Expand Down
3 changes: 3 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.

## [Unreleased] - ././2020

- [General refactoring by @hakancelik96](https://github.com/hakancelik96/unimport/pull/108)
- Support append and extend `__all__` list
- Star import suggestions improved
- [🐞 Fix: Unnecessary refactoring by @hakancelik96](https://github.com/hakancelik96/unimport/pull/107)
- [💪 Support .gitignore exclude patterns by @hadialqattan](https://github.com/hakancelik96/unimport/pull/102)
- [💊 Optimize Python >=3.8 type comments support method by @hadialqattan](https://github.com/hakancelik96/unimport/pull/95)
Expand Down
44 changes: 20 additions & 24 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,26 @@ import os
__all__ = ["os"] # this import is used and umimport can understand
```

Other supported operations, **append** and **extend**

```python
from os import *

__all__ = []
__all__.append("removedirs")
__all__.extend(["walk"])
```

after refactoring

```python
from os import removedirs, walk

__all__ = []
__all__.append("removedirs")
__all__.extend(["walk"])
```

## Requirements.txt

You can automatically delete unused modules from the requirements.txt file
Expand Down Expand Up @@ -403,27 +423,3 @@ repos:
- id: unimport
args: [--remove, --requirements, --include-star-import]
```

## Our badge

[![style](https://img.shields.io/badge/unimport-ED1C24)](https://github.com/hakancelik96/unimport)

**Please insert this badge into your project**

`[![style](https://img.shields.io/badge/unimport-ED1C24)](https://github.com/hakancelik96/unimport)`

## CONTRIBUTING

[![](https://sourcerer.io/fame/hakancelik96/hakancelik96/unimport/images/0)](https://sourcerer.io/fame/hakancelik96/hakancelik96/unimport/links/0)[![](https://sourcerer.io/fame/hakancelik96/hakancelik96/unimport/images/1)](https://sourcerer.io/fame/hakancelik96/hakancelik96/unimport/links/1)[![](https://sourcerer.io/fame/hakancelik96/hakancelik96/unimport/images/2)](https://sourcerer.io/fame/hakancelik96/hakancelik96/unimport/links/2)[![](https://sourcerer.io/fame/hakancelik96/hakancelik96/unimport/images/3)](https://sourcerer.io/fame/hakancelik96/hakancelik96/unimport/links/3)[![](https://sourcerer.io/fame/hakancelik96/hakancelik96/unimport/images/4)](https://sourcerer.io/fame/hakancelik96/hakancelik96/unimport/links/4)[![](https://sourcerer.io/fame/hakancelik96/hakancelik96/unimport/images/5)](https://sourcerer.io/fame/hakancelik96/hakancelik96/unimport/links/5)[![](https://sourcerer.io/fame/hakancelik96/hakancelik96/unimport/images/6)](https://sourcerer.io/fame/hakancelik96/hakancelik96/unimport/links/6)[![](https://sourcerer.io/fame/hakancelik96/hakancelik96/unimport/images/7)](https://sourcerer.io/fame/hakancelik96/hakancelik96/unimport/links/7)

[CONTRIBUTING.md](/CONTRIBUTING.md)

## CHANGELOG

All notable changes to this project will be documented in this file.

[CHANGELOG.md](/CHANGELOG.md)

## Who's using Unimport

[![radity.com](https://raw.githubusercontent.com/hakancelik96/unimport/master/images/clients/radity.jpg)](https://radity.com/?ref=unimport)
2 changes: 1 addition & 1 deletion tests/test_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TestColor(unittest.TestCase):
def setUp(self):
self.test_content = "Test Content"

for color in COLORS.keys():
for color in COLORS:
test_template = textwrap.dedent(
f"""
def test_{color}(self):
Expand Down
12 changes: 9 additions & 3 deletions tests/test_refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def test_multiple_imports(self):
self.session.refactor(action),
)

def test_future(self):
def test_future_from_import(self):
action = (
"from __future__ import (\n"
" absolute_import, division, print_function, unicode_literals\n"
Expand All @@ -120,6 +120,13 @@ def test_future(self):
self.session.refactor(action),
)

def test_future_import(self):
action = "import __future__\n" "__future__.absolute_import\n"
self.assertEqual(
action,
self.session.refactor(action),
)

def test_local_import(self):
action = (
"from .x import y\n"
Expand Down Expand Up @@ -533,7 +540,6 @@ def test_star_imports(self):
)
expected = (
"from re import match, search\n"
"from t.s.d import *\n"
"from lib2to3.pgen2.token import NAME\n\n"
"print(match)\n"
"print(search)\n"
Expand Down Expand Up @@ -570,7 +576,7 @@ def test_star_import_2(self):
self.session.refactor(action),
)

def test_two_suggestion(self):
def test_two_suggestions(self):
action = (
"from time import *\n"
"from os import *\n"
Expand Down
43 changes: 6 additions & 37 deletions tests/test_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ def setUp(self):
def assertUnimportEqual(
self,
source,
expected_names,
expected_classes,
expected_functions,
expected_imports,
expected_names=[],
expected_imports=[],
):
self.scanner.scan(source)
self.assertEqual(expected_names, self.scanner.names)
self.assertEqual(expected_classes, self.scanner.classes)
self.assertEqual(expected_functions, self.scanner.functions)
self.assertEqual(expected_imports, self.scanner.imports)
self.scanner.clear()

Expand All @@ -50,9 +46,6 @@ def test_names(self):
Name(lineno=1, name="variable"),
Name(lineno=2, name="variable1"),
],
expected_classes=[Name(lineno=3, name="TestClass")],
expected_functions=[Name(lineno=5, name="function")],
expected_imports=[],
)

def test_names_with_import(self):
Expand All @@ -68,18 +61,13 @@ def test_names_with_import(self):
self.assertUnimportEqual(
source,
expected_names=[Name(lineno=1, name="variable")],
expected_classes=[Name(lineno=3, name="TestClass")],
expected_functions=[Name(lineno=6, name="test_function")],
expected_imports=[Import(lineno=2, module=os, name="os")],
expected_imports=[Import(lineno=2, name="os")],
)

def test_names_with_function(self):
self.assertUnimportEqual(
source="variable = 1\n" "def test():\n" "\tpass",
expected_names=[Name(lineno=1, name="variable")],
expected_classes=[],
expected_functions=[Name(lineno=2, name="test")],
expected_imports=[],
)

def test_names_with_class(self):
Expand All @@ -94,9 +82,6 @@ def test_names_with_class(self):
self.assertUnimportEqual(
source,
expected_names=[Name(lineno=1, name="variable")],
expected_classes=[Name(lineno=4, name="test")],
expected_functions=[Name(lineno=2, name="test_function")],
expected_imports=[],
)

def test_decator_in_class(self):
Expand All @@ -111,20 +96,13 @@ def test_decator_in_class(self):
self.assertUnimportEqual(
source,
expected_names=[Name(lineno=5, name="test2")],
expected_classes=[Name(lineno=1, name="Test")],
expected_functions=[],
expected_imports=[],
)


class SkipImportTest(ScannerTestCase):
def assertSkipEqual(self, source):
super().assertUnimportEqual(
source,
expected_names=[],
expected_classes=[],
expected_functions=[],
expected_imports=[],
)

def test_inside_try_except(self):
Expand Down Expand Up @@ -184,27 +162,18 @@ def test_type_comments(self):
Name(lineno=1, name="Tuple"),
Name(lineno=1, name="Tuple"),
]
expected_classes = []
expected_functions = [Name(4, "function")]
expected_imports = [
ImportFrom(
lineno=1, name="Any", star=False, module=typing, modules=[]
),
ImportFrom(lineno=1, name="Any", star=False, suggestions=[]),
ImportFrom(
lineno=2,
name="Tuple",
star=False,
module=typing,
modules=[],
),
ImportFrom(
lineno=3, name="Union", star=False, module=typing, modules=[]
suggestions=[],
),
ImportFrom(lineno=3, name="Union", star=False, suggestions=[]),
]
self.assertUnimportEqual(
source,
expected_names,
expected_classes,
expected_functions,
expected_imports,
)