Skip to content

Commit 3c197ec

Browse files
authored
Merge pull request #264 from asottile/remove-combine
combine remove_imports and remove_duplicated_imports
2 parents 1557a71 + b10eb67 commit 3c197ec

2 files changed

Lines changed: 22 additions & 37 deletions

File tree

reorder_python_imports.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,6 @@ def add_imports(
162162
]
163163

164164

165-
def remove_imports(
166-
partitions: Iterable[CodePartition],
167-
to_remove: set[tuple[str, str | None] | tuple[str, str, str | None]],
168-
) -> list[CodePartition]:
169-
return [
170-
partition for partition in partitions
171-
if (
172-
partition.code_type is not CodeType.IMPORT or
173-
import_obj_from_str(partition.src).key not in to_remove
174-
)
175-
]
176-
177-
178165
class Replacements(NamedTuple):
179166
# (orig_mod, attr) => new_mod
180167
exact: dict[tuple[str, str], str]
@@ -289,16 +276,18 @@ def _module_to_base_modules(s: str) -> Generator[str, None, None]:
289276

290277
def remove_duplicated_imports(
291278
partitions: Iterable[CodePartition],
279+
*,
280+
to_remove: set[tuple[str, ...]],
292281
) -> list[CodePartition]:
293-
seen: set[Import | ImportFrom] = set()
282+
seen = set(to_remove)
294283
seen_module_names: set[str] = set()
295284
without_exact_duplicates = []
296285

297286
for partition in partitions:
298287
if partition.code_type is CodeType.IMPORT:
299288
import_obj = import_obj_from_str(partition.src)
300-
if import_obj not in seen:
301-
seen.add(import_obj)
289+
if import_obj.key not in seen:
290+
seen.add(import_obj.key)
302291
if (
303292
isinstance(import_obj, Import) and
304293
not import_obj.key.asname
@@ -385,7 +374,7 @@ def fix_file_contents(
385374
contents: str,
386375
*,
387376
to_add: tuple[str, ...] = (),
388-
to_remove: set[tuple[str, str | None] | tuple[str, str, str | None]],
377+
to_remove: set[tuple[str, ...]],
389378
to_replace: Replacements,
390379
settings: Settings = Settings(),
391380
) -> str:
@@ -401,9 +390,8 @@ def fix_file_contents(
401390
partitioned = combine_trailing_code_chunks(partitioned)
402391
partitioned = add_imports(partitioned, to_add=to_add)
403392
partitioned = separate_comma_imports(partitioned)
404-
partitioned = remove_imports(partitioned, to_remove=to_remove)
405393
partitioned = replace_imports(partitioned, to_replace=to_replace)
406-
partitioned = remove_duplicated_imports(partitioned)
394+
partitioned = remove_duplicated_imports(partitioned, to_remove=to_remove)
407395
partitioned = apply_import_sorting(partitioned, settings=settings)
408396

409397
return _partitions_to_src(partitioned).replace('\n', nl)
@@ -413,7 +401,7 @@ def _fix_file(
413401
filename: str,
414402
args: argparse.Namespace,
415403
*,
416-
to_remove: set[tuple[str, str | None] | tuple[str, str, str | None]],
404+
to_remove: set[tuple[str, ...]],
417405
to_replace: Replacements,
418406
settings: Settings = Settings(),
419407
) -> int:

tests/reorder_python_imports_test.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -209,56 +209,53 @@ def test_separate_comma_imports_does_not_remove_comments_when_not_splitting():
209209

210210

211211
def test_remove_duplicated_imports_trivial():
212-
assert remove_duplicated_imports([]) == []
212+
assert remove_duplicated_imports([], to_remove=set()) == []
213213

214214

215215
def test_remove_duplicated_imports_no_dupes_no_removals():
216-
input_partitions = [
216+
partitions = [
217217
CodePartition(CodeType.IMPORT, 'import sys\n'),
218218
CodePartition(CodeType.NON_CODE, '\n'),
219219
CodePartition(CodeType.IMPORT, 'from six import text_type\n'),
220220
]
221-
assert remove_duplicated_imports(input_partitions) == input_partitions
221+
assert remove_duplicated_imports(partitions, to_remove=set()) == partitions
222222

223223

224224
def test_remove_duplicated_imports_removes_duplicated():
225-
assert remove_duplicated_imports([
225+
partitions = [
226226
CodePartition(CodeType.IMPORT, 'import sys\n'),
227227
CodePartition(CodeType.IMPORT, 'import sys\n'),
228-
]) == [
229-
CodePartition(CodeType.IMPORT, 'import sys\n'),
230228
]
229+
expected = [CodePartition(CodeType.IMPORT, 'import sys\n')]
230+
assert remove_duplicated_imports(partitions, to_remove=set()) == expected
231231

232232

233233
def test_remove_duplicate_redundant_import_imports():
234-
assert remove_duplicated_imports([
235-
CodePartition(CodeType.IMPORT, 'import os\n'),
236-
CodePartition(CodeType.IMPORT, 'import os.path\n'),
237-
]) == [
238-
CodePartition(CodeType.IMPORT, 'import os.path\n'),
239-
]
240-
assert remove_duplicated_imports([
241-
CodePartition(CodeType.IMPORT, 'import os.path\n'),
234+
partitions = [
242235
CodePartition(CodeType.IMPORT, 'import os\n'),
243-
]) == [
244236
CodePartition(CodeType.IMPORT, 'import os.path\n'),
245237
]
238+
expected = [CodePartition(CodeType.IMPORT, 'import os.path\n')]
239+
240+
assert remove_duplicated_imports(partitions, to_remove=set()) == expected
241+
partitions.reverse()
242+
assert remove_duplicated_imports(partitions, to_remove=set()) == expected
246243

247244

248245
def test_aliased_imports_not_considered_redundant():
249246
partitions = [
250247
CodePartition(CodeType.IMPORT, 'import os\n'),
251248
CodePartition(CodeType.IMPORT, 'import os.path as os_path\n'),
252249
]
253-
assert remove_duplicated_imports(partitions) == partitions
250+
assert remove_duplicated_imports(partitions, to_remove=set()) == partitions
254251

255252

256253
def test_aliased_imports_not_considered_redundant_v2():
257254
partitions = [
258255
CodePartition(CodeType.IMPORT, 'import os as osmod\n'),
259256
CodePartition(CodeType.IMPORT, 'import os.path\n'),
260257
]
261-
assert remove_duplicated_imports(partitions) == partitions
258+
assert remove_duplicated_imports(partitions, to_remove=set()) == partitions
262259

263260

264261
def test_apply_import_sorting_trivial():

0 commit comments

Comments
 (0)