Skip to content

Commit 496e1c8

Browse files
Resolved #1181: Implemented ability to specify the types of imports.
1 parent 79bad69 commit 496e1c8

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ NOTE: isort follows the [semver](https://semver.org/) versioning standard.
66
### 5.3.0 TBD
77
- Implemented ability to treat all or select comments as code (issue #1357)
88
- Implemented ability to use different configs for different file extensions (issue #1162)
9+
- Implemented ability to specify the types of imports (issue #1181)
910
- Added experimental support for sorting literals (issue #1358)
1011
- Added experimental support for sorting and deduping groupings of assignments.
1112
- Improved handling of deprecated single line variables for usage with Visual Studio Code (issue #1363)

isort/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ class _Config:
183183
treat_all_comments_as_code: bool = False
184184
supported_extensions: FrozenSet[str] = SUPPORTED_EXTENSIONS
185185
blocked_extensions: FrozenSet[str] = BLOCKED_EXTENSIONS
186+
constants: FrozenSet[str] = frozenset()
187+
classes: FrozenSet[str] = frozenset()
188+
variables: FrozenSet[str] = frozenset()
186189

187190
def __post_init__(self):
188191
py_version = self.py_version

isort/sorting.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ def module_key(
2626
module_name = str(module_name)
2727

2828
if sub_imports and config.order_by_type:
29-
if module_name.isupper() and len(module_name) > 1: # see issue #376
29+
if module_name in config.constants:
3030
prefix = "A"
31-
elif module_name[0:1].isupper():
31+
elif module_name in config.classes:
32+
prefix = "B"
33+
elif module_name in config.variables:
34+
prefix = "C"
35+
elif module_name.isupper() and len(module_name) > 1: # see issue #376
36+
prefix = "A"
37+
elif module_name in config.classes or module_name[0:1].isupper():
3238
prefix = "B"
3339
else:
3440
prefix = "C"

tests/test_ticketed_features.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,27 @@ def method():
483483
# isort: dict
484484
y = {"b": "c", "z": "z"}"""
485485
)
486+
487+
488+
def test_isort_allows_setting_import_types_issue_1181():
489+
"""Test to ensure isort provides a way to set the type of imports.
490+
See: https://github.com/timothycrosley/isort/issues/1181
491+
"""
492+
assert isort.code("from x import AA, Big, variable") == "from x import AA, Big, variable\n"
493+
assert (
494+
isort.code("from x import AA, Big, variable", constants=["variable"])
495+
== "from x import AA, variable, Big\n"
496+
)
497+
assert (
498+
isort.code("from x import AA, Big, variable", variables=["AA"])
499+
== "from x import Big, AA, variable\n"
500+
)
501+
assert (
502+
isort.code(
503+
"from x import AA, Big, variable",
504+
constants=["Big"],
505+
variables=["AA"],
506+
classes=["variable"],
507+
)
508+
== "from x import Big, variable, AA\n"
509+
)

0 commit comments

Comments
 (0)