Skip to content

Commit f4c3c5f

Browse files
committed
Prevent one line comments from being re-formatted
1 parent f19a187 commit f4c3c5f

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

usort/tests/functional.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,18 @@ def foo():
331331
from something import nothing
332332
""",
333333
)
334+
335+
def test_white_space_pytorch_example(self) -> None:
336+
config = replace(DEFAULT_CONFIG, preserve_inline_comments=True)
337+
self.assertUsortResult(
338+
"""
339+
from package.test_save_load import TestSaveLoad # noqa: F401
340+
""",
341+
"""
342+
from package.test_save_load import TestSaveLoad # noqa: F401
343+
""",
344+
config
345+
)
334346

335347
def test_case_insensitive_sorting(self) -> None:
336348
content = """

usort/translate.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,10 @@ def import_to_node(
258258
if config.magic_commas and imp.stem and imp.trailing_comma:
259259
return import_to_node_multi(imp, module)
260260

261-
# If preserve_inline_comments is enabled and any item has inline comments,
262-
# use multi-line format to preserve them
263-
if config.preserve_inline_comments and imp.stem:
261+
# If preserve_inline_comments is enabled and multiple items have inline comments,
262+
# use multi-line format to preserve them. Single-item imports can keep comments
263+
# on the same line.
264+
if config.preserve_inline_comments and imp.stem and len(imp.items) > 1:
264265
has_item_comments = any(item.comments.inline for item in imp.items)
265266
if has_item_comments:
266267
return import_to_node_multi(imp, module)

0 commit comments

Comments
 (0)