Skip to content

Commit bd93a0c

Browse files
committed
Getting rid of some bad practice
1 parent caf013b commit bd93a0c

File tree

6 files changed

+98
-110
lines changed

6 files changed

+98
-110
lines changed

docs/scripts/used-imports.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
```python
44
from pathlib import Path
55

6-
import unimport
6+
from unimport.session import Session
77

8-
session = unimport.Session()
8+
session = Session()
99

1010

1111
def get_all_modules(project_path): # write the path of your own project

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from setuptools import setup
55

6-
from unimport import __description__, __version__
6+
import unimport.constants as C
77

88
assert sys.version_info >= (3, 6, 0), "unimport requires Python 3.6+"
99

@@ -18,8 +18,8 @@ def get_long_description():
1818

1919
setup(
2020
name="unimport",
21-
version=__version__,
22-
description=__description__,
21+
version=C.VERSION,
22+
description=C.DESCRIPTION,
2323
long_description=get_long_description(),
2424
long_description_content_type="text/markdown",
2525
keywords=["unused", "import"],

unimport/__init__.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +0,0 @@
1-
from unimport.refactor import refactor_string
2-
from unimport.scan import Scanner
3-
from unimport.session import Session
4-
from unimport.statement import Import, ImportFrom, Name
5-
6-
__description__ = (
7-
"A linter, formatter for finding and removing unused import statements."
8-
)
9-
__version__ = "0.2.9"
10-
__all__ = [
11-
"Import",
12-
"ImportFrom",
13-
"Name",
14-
"Scanner",
15-
"Session",
16-
"refactor_string",
17-
]

unimport/__main__.py

Lines changed: 88 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -5,96 +5,11 @@
55
from pathlib import Path
66
from typing import List, Optional, Tuple, Union
77

8-
from unimport import __description__, __version__
8+
import unimport.constants as C
99
from unimport.color import Color
1010
from unimport.session import Session
1111
from unimport.statement import Import, ImportFrom
1212

13-
parser = argparse.ArgumentParser(
14-
prog="unimport",
15-
description=__description__,
16-
epilog="Get rid of all unused imports 🥳",
17-
)
18-
exclusive_group = parser.add_mutually_exclusive_group(required=False)
19-
parser.add_argument(
20-
"sources",
21-
default=[Path(".")],
22-
nargs="*",
23-
help="files and folders to find the unused imports.",
24-
action="store",
25-
type=Path,
26-
)
27-
parser.add_argument(
28-
"-c",
29-
"--config",
30-
default=".",
31-
help="read configuration from PATH.",
32-
metavar="PATH",
33-
action="store",
34-
type=Path,
35-
)
36-
parser.add_argument(
37-
"--include",
38-
help="file include pattern.",
39-
metavar="include",
40-
action="store",
41-
default="",
42-
type=str,
43-
)
44-
parser.add_argument(
45-
"--exclude",
46-
help="file exclude pattern.",
47-
metavar="exclude",
48-
action="store",
49-
default="",
50-
type=str,
51-
)
52-
parser.add_argument(
53-
"--include-star-import",
54-
action="store_true",
55-
help="Include star imports during scanning and refactor.",
56-
)
57-
parser.add_argument(
58-
"--show-error",
59-
action="store_true",
60-
help="Show or don't show errors captured during static analysis.",
61-
)
62-
parser.add_argument(
63-
"-d",
64-
"--diff",
65-
action="store_true",
66-
help="Prints a diff of all the changes unimport would make to a file.",
67-
)
68-
exclusive_group.add_argument(
69-
"-r",
70-
"--remove",
71-
action="store_true",
72-
help="remove unused imports automatically.",
73-
)
74-
exclusive_group.add_argument(
75-
"-p",
76-
"--permission",
77-
action="store_true",
78-
help="Refactor permission after see diff.",
79-
)
80-
parser.add_argument(
81-
"--requirements",
82-
action="store_true",
83-
help="Include requirements.txt file, You can use it with all other arguments",
84-
)
85-
parser.add_argument(
86-
"--check",
87-
action="store_true",
88-
help="Prints which file the unused imports are in.",
89-
)
90-
parser.add_argument(
91-
"-v",
92-
"--version",
93-
action="version",
94-
version=f"Unimport {__version__}",
95-
help="Prints version of unimport",
96-
)
97-
9813

9914
def color_diff(sequence: Tuple[str, ...]) -> str:
10015
contents = "\n".join(sequence)
@@ -149,6 +64,92 @@ def show(
14964

15065

15166
def main(argv: Optional[List[str]] = None) -> int:
67+
argv = argv if argv is not None else sys.argv[1:]
68+
parser = argparse.ArgumentParser(
69+
prog="unimport",
70+
description=C.DESCRIPTION,
71+
epilog="Get rid of all unused imports 🥳",
72+
)
73+
exclusive_group = parser.add_mutually_exclusive_group(required=False)
74+
parser.add_argument(
75+
"sources",
76+
default=[Path(".")],
77+
nargs="*",
78+
help="files and folders to find the unused imports.",
79+
action="store",
80+
type=Path,
81+
)
82+
parser.add_argument(
83+
"-c",
84+
"--config",
85+
default=".",
86+
help="read configuration from PATH.",
87+
metavar="PATH",
88+
action="store",
89+
type=Path,
90+
)
91+
parser.add_argument(
92+
"--include",
93+
help="file include pattern.",
94+
metavar="include",
95+
action="store",
96+
default="",
97+
type=str,
98+
)
99+
parser.add_argument(
100+
"--exclude",
101+
help="file exclude pattern.",
102+
metavar="exclude",
103+
action="store",
104+
default="",
105+
type=str,
106+
)
107+
parser.add_argument(
108+
"--include-star-import",
109+
action="store_true",
110+
help="Include star imports during scanning and refactor.",
111+
)
112+
parser.add_argument(
113+
"--show-error",
114+
action="store_true",
115+
help="Show or don't show errors captured during static analysis.",
116+
)
117+
parser.add_argument(
118+
"-d",
119+
"--diff",
120+
action="store_true",
121+
help="Prints a diff of all the changes unimport would make to a file.",
122+
)
123+
exclusive_group.add_argument(
124+
"-r",
125+
"--remove",
126+
action="store_true",
127+
help="remove unused imports automatically.",
128+
)
129+
exclusive_group.add_argument(
130+
"-p",
131+
"--permission",
132+
action="store_true",
133+
help="Refactor permission after see diff.",
134+
)
135+
parser.add_argument(
136+
"--requirements",
137+
action="store_true",
138+
help="Include requirements.txt file, You can use it with all other arguments",
139+
)
140+
parser.add_argument(
141+
"--check",
142+
action="store_true",
143+
help="Prints which file the unused imports are in.",
144+
)
145+
parser.add_argument(
146+
"-v",
147+
"--version",
148+
action="version",
149+
version=f"Unimport {C.VERSION}",
150+
help="Prints version of unimport",
151+
)
152+
152153
namespace = parser.parse_args(argv)
153154
namespace.check = namespace.check or not any(
154155
[namespace.diff, namespace.remove, namespace.permission]
@@ -251,4 +252,4 @@ def main(argv: Optional[List[str]] = None) -> int:
251252

252253

253254
if __name__ == "__main__":
254-
sys.exit(main(sys.argv[1:]))
255+
exit(main())

unimport/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
DESCRIPTION = (
2+
"A linter, formatter for finding and removing unused import statements."
3+
)
4+
VERSION = "0.2.9"

unimport/refactor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
class RemoveUnusedImportTransformer(cst.CSTTransformer):
10-
METADATA_DEPENDENCIES = [cst.metadata.PositionProvider]
10+
METADATA_DEPENDENCIES = (cst.metadata.PositionProvider,)
1111

1212
def __init__(
1313
self, unused_imports: List[Union[Import, ImportFrom]]

0 commit comments

Comments
 (0)