Skip to content

Commit 628a028

Browse files
authored
Merge pull request #350 from maggiemoss/add-config-option-to-collapse-lines
Add config option to collapse lines in same category
2 parents 423dc72 + efc7749 commit 628a028

File tree

3 files changed

+133
-2
lines changed

3 files changed

+133
-2
lines changed

usort/config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ class Config:
6868
# Whether to preserve inline comments on individual imports when sorting
6969
preserve_inline_comments: bool = False
7070

71+
# Whether to collapse blank lines within a single import category
72+
# Default True matches current behavior (collapse blank lines within categories)
73+
# Set to False to preserve one blank line within categories (pre-commit 58c01556 behavior)
74+
collapse_blank_lines_in_category: bool = True
75+
7176
# gitignore-style filename patterns to exclude when sorting entire directories
7277
excludes: List[str] = field(default_factory=list)
7378

@@ -172,6 +177,10 @@ def update_from_config(self, toml_path: Path) -> None:
172177
self.merge_imports = bool(tbl["merge_imports"])
173178
if "preserve_inline_comments" in tbl:
174179
self.preserve_inline_comments = bool(tbl["preserve_inline_comments"])
180+
if "collapse_blank_lines_in_category" in tbl:
181+
self.collapse_blank_lines_in_category = bool(
182+
tbl["collapse_blank_lines_in_category"]
183+
)
175184
if "excludes" in tbl:
176185
self.excludes = tbl["excludes"]
177186

usort/sorting.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,11 @@ def fixup_whitespace(
238238
elif imp.sort_key.category_index != cur_category:
239239
blanks = ("",)
240240
else:
241-
blanks = ()
241+
# Apply the config option for blank line behavior within categories
242+
if self.config.collapse_blank_lines_in_category:
243+
blanks = ()
244+
else:
245+
blanks = _old_blanks[:1]
242246

243247
imp.comments.before = [*blanks, *old_comments]
244248

usort/tests/functional.py

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from typing import Optional
1212

1313
from ..api import usort, usort_path
14-
from ..config import Config
14+
from ..config import CAT_FIRST_PARTY, Config
1515
from ..translate import import_from_node
1616
from ..util import parse_import
1717

@@ -1204,6 +1204,124 @@ def test_preserve_inline_comments_disabled(self) -> None:
12041204
config,
12051205
)
12061206

1207+
def test_collapse_blank_lines_in_category_enabled(self) -> None:
1208+
"""Test with collapse_blank_lines_in_category=True (default, post-commit 58c01556 behavior)"""
1209+
config = replace(DEFAULT_CONFIG, collapse_blank_lines_in_category=True)
1210+
self.assertUsortResult(
1211+
"""
1212+
import math
1213+
1214+
import gamma
1215+
1216+
import alpha
1217+
1218+
1219+
# special
1220+
import beta
1221+
1222+
from . import foo
1223+
1224+
import zeta
1225+
1226+
""",
1227+
"""
1228+
import math
1229+
1230+
import alpha
1231+
# special
1232+
import beta
1233+
import gamma
1234+
import zeta
1235+
1236+
from . import foo
1237+
1238+
""",
1239+
config,
1240+
)
1241+
1242+
def test_collapse_blank_lines_in_category_disabled(self) -> None:
1243+
"""Test with collapse_blank_lines_in_category=False (pre-commit 58c01556 behavior)"""
1244+
config = replace(DEFAULT_CONFIG, collapse_blank_lines_in_category=False)
1245+
self.assertUsortResult(
1246+
"""
1247+
import math
1248+
1249+
import gamma
1250+
1251+
import alpha
1252+
1253+
1254+
# special
1255+
import beta
1256+
1257+
from . import foo
1258+
1259+
import zeta
1260+
1261+
""",
1262+
"""
1263+
import math
1264+
1265+
import alpha
1266+
1267+
# special
1268+
import beta
1269+
1270+
import gamma
1271+
1272+
import zeta
1273+
1274+
from . import foo
1275+
1276+
""",
1277+
config,
1278+
)
1279+
1280+
def test_collapse_blank_lines_in_category_default(self) -> None:
1281+
# Configure torch as first_party so both imports are in the same category
1282+
known = DEFAULT_CONFIG.known.copy()
1283+
known["torch"] = CAT_FIRST_PARTY
1284+
config = replace(
1285+
DEFAULT_CONFIG,
1286+
collapse_blank_lines_in_category=True,
1287+
known=known,
1288+
)
1289+
self.assertUsortResult(
1290+
"""
1291+
import torch
1292+
1293+
from .runner import get_nn_runners
1294+
""",
1295+
"""
1296+
import torch
1297+
from .runner import get_nn_runners
1298+
""",
1299+
config,
1300+
)
1301+
1302+
def test_collapse_blank_lines_in_category_false(self) -> None:
1303+
# Configure torch as first_party so both imports are in the same category
1304+
known = DEFAULT_CONFIG.known.copy()
1305+
known["torch"] = CAT_FIRST_PARTY
1306+
config = replace(
1307+
DEFAULT_CONFIG,
1308+
collapse_blank_lines_in_category=False,
1309+
known=known,
1310+
)
1311+
self.assertUsortResult(
1312+
"""
1313+
import torch
1314+
1315+
from .runner import get_runners
1316+
""",
1317+
"""
1318+
import torch
1319+
1320+
from .runner import get_runners
1321+
""",
1322+
config,
1323+
)
1324+
12071325

12081326
if __name__ == "__main__":
12091327
unittest.main()

0 commit comments

Comments
 (0)