Skip to content

Commit

Permalink
Resolved #1181: Implemented ability to specify the types of imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Aug 5, 2020
1 parent 79bad69 commit 496e1c8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ NOTE: isort follows the [semver](https://semver.org/) versioning standard.
### 5.3.0 TBD
- Implemented ability to treat all or select comments as code (issue #1357)
- Implemented ability to use different configs for different file extensions (issue #1162)
- Implemented ability to specify the types of imports (issue #1181)
- Added experimental support for sorting literals (issue #1358)
- Added experimental support for sorting and deduping groupings of assignments.
- Improved handling of deprecated single line variables for usage with Visual Studio Code (issue #1363)
Expand Down
3 changes: 3 additions & 0 deletions isort/settings.py
Expand Up @@ -183,6 +183,9 @@ class _Config:
treat_all_comments_as_code: bool = False
supported_extensions: FrozenSet[str] = SUPPORTED_EXTENSIONS
blocked_extensions: FrozenSet[str] = BLOCKED_EXTENSIONS
constants: FrozenSet[str] = frozenset()
classes: FrozenSet[str] = frozenset()
variables: FrozenSet[str] = frozenset()

def __post_init__(self):
py_version = self.py_version
Expand Down
10 changes: 8 additions & 2 deletions isort/sorting.py
Expand Up @@ -26,9 +26,15 @@ def module_key(
module_name = str(module_name)

if sub_imports and config.order_by_type:
if module_name.isupper() and len(module_name) > 1: # see issue #376
if module_name in config.constants:
prefix = "A"
elif module_name[0:1].isupper():
elif module_name in config.classes:
prefix = "B"
elif module_name in config.variables:
prefix = "C"
elif module_name.isupper() and len(module_name) > 1: # see issue #376
prefix = "A"
elif module_name in config.classes or module_name[0:1].isupper():
prefix = "B"
else:
prefix = "C"
Expand Down
24 changes: 24 additions & 0 deletions tests/test_ticketed_features.py
Expand Up @@ -483,3 +483,27 @@ def method():
# isort: dict
y = {"b": "c", "z": "z"}"""
)


def test_isort_allows_setting_import_types_issue_1181():
"""Test to ensure isort provides a way to set the type of imports.
See: https://github.com/timothycrosley/isort/issues/1181
"""
assert isort.code("from x import AA, Big, variable") == "from x import AA, Big, variable\n"
assert (
isort.code("from x import AA, Big, variable", constants=["variable"])
== "from x import AA, variable, Big\n"
)
assert (
isort.code("from x import AA, Big, variable", variables=["AA"])
== "from x import Big, AA, variable\n"
)
assert (
isort.code(
"from x import AA, Big, variable",
constants=["Big"],
variables=["AA"],
classes=["variable"],
)
== "from x import Big, variable, AA\n"
)

0 comments on commit 496e1c8

Please sign in to comment.