-
Notifications
You must be signed in to change notification settings - Fork 183
/
test_remove_unused_imports.py
132 lines (113 loc) · 3.19 KB
/
test_remove_unused_imports.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
#
from libcst.codemod import CodemodTest
from libcst.codemod.commands.remove_unused_imports import RemoveUnusedImportsCommand
class RemoveUnusedImportsCommandTest(CodemodTest):
TRANSFORM = RemoveUnusedImportsCommand
def test_simple_case(self) -> None:
before = "import a, b\na()"
after = "import a\na()"
self.assertCodemod(before, after)
def test_double_import(self) -> None:
before = "import a\nimport a\na()"
self.assertCodemod(before, before)
def test_conditional_import(self) -> None:
before = """
if True:
import a
else:
import b as a
a()
"""
self.assertCodemod(before, before)
def test_unused_in_conditional(self) -> None:
before = """
if False:
import a
"""
after = """
if False:
pass
"""
self.assertCodemod(before, after)
def test_type_annotations(self) -> None:
before = """
import a
x: a = 1
"""
self.assertCodemod(before, before)
def test_dotted_imports(self) -> None:
before = """
import a.b, a.b.c
import e.f
import g.h
import x.y, x.y.z
def foo() -> None:
a.b
e.g
g.h.i
x.y.z
"""
after = """
import a.b, a.b.c
import e.f
import g.h
import x.y.z
def foo() -> None:
a.b
e.g
g.h.i
x.y.z
"""
self.assertCodemod(before, after)
def test_enclosed_attributes(self) -> None:
before = """
from a.b import c
import x
def foo() -> None:
x.y(c.d()).z()
"""
self.assertCodemod(before, before)
def test_access_in_assignment(self) -> None:
before = """
from a import b
b(0)[x] = False
"""
self.assertCodemod(before, before)
def test_no_formatting_if_no_unused_imports(self) -> None:
before = """
from m import (a, b,)
a(b, 'look at these ugly quotes')
"""
self.assertCodemod(before, before)
def test_suppression_on_first_line_of_multiline_import_refers_to_whole_block(
self,
) -> None:
before = """
from a import ( # lint-ignore: unused-import
b,
c,
)
"""
self.assertCodemod(before, before)
def test_suppression(self) -> None:
before = """
# noqa
import a, b
import c
from x import (
y,
z, # noqa
)
"""
after = """
# noqa
import a, b
from x import (
z, # noqa
)
"""
self.assertCodemod(before, after)