Skip to content

Commit c8e6b68

Browse files
committedApr 7, 2021
Fix issue #1208: Separately identify import sections for each from import
1 parent d9d95ec commit c8e6b68

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed
 

‎isort/parse.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -432,21 +432,21 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
432432

433433
if type_of_import == "from":
434434
import_from = just_imports.pop(0)
435-
placed_module = finder(import_from)
436-
if config.verbose and not config.only_modified:
437-
print(f"from-type place_module for {import_from} returned {placed_module}")
438-
439-
elif config.verbose:
440-
verbose_output.append(
441-
f"from-type place_module for {import_from} returned {placed_module}"
442-
)
443-
if placed_module == "":
444-
warn(
445-
f"could not place module {import_from} of line {line} --"
446-
" Do you need to define a default section?"
447-
)
448-
root = imports[placed_module][type_of_import] # type: ignore
449435
for import_name in just_imports:
436+
placed_module = finder(f"{import_from}.{import_name}")
437+
if config.verbose and not config.only_modified:
438+
print(f"from-type place_module for {import_from} returned {placed_module}")
439+
440+
elif config.verbose:
441+
verbose_output.append(
442+
f"from-type place_module for {import_from} returned {placed_module}"
443+
)
444+
if placed_module == "":
445+
warn(
446+
f"could not place module {import_from} of line {line} --"
447+
" Do you need to define a default section?"
448+
)
449+
root = imports[placed_module][type_of_import] # type: ignore
450450
associated_comment = nested_comments.get(import_name)
451451
if associated_comment:
452452
categorized_comments["nested"].setdefault(import_from, {})[

‎isort/place.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def module_with_reason(name: str, config: Config = DEFAULT_CONFIG) -> Tuple[str,
2323
return (
2424
_forced_separate(name, config)
2525
or _local(name, config)
26-
or _known_pattern(name, config)
26+
or known_pattern(name, config)
2727
or _src_path(name, config)
2828
or (config.default_section, "Default option in Config or universal default.")
2929
)
@@ -49,7 +49,8 @@ def _local(name: str, config: Config) -> Optional[Tuple[str, str]]:
4949
return None
5050

5151

52-
def _known_pattern(name: str, config: Config) -> Optional[Tuple[str, str]]:
52+
@lru_cache(maxsize=1000)
53+
def known_pattern(name: str, config: Config) -> Optional[Tuple[str, str]]:
5354
parts = name.split(".")
5455
module_names_to_check = (".".join(parts[:first_k]) for first_k in range(len(parts), 0, -1))
5556
for module_name_to_check in module_names_to_check:

‎tests/unit/test_isort.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1038,8 +1038,8 @@ def test_forced_separate() -> None:
10381038
"from django.core.urlresolvers import reverse\n"
10391039
"from django.db import models\n"
10401040
"from django.db.models.fields import FieldDoesNotExist\n"
1041-
"from django.utils import six\n"
10421041
"\n"
1042+
"from django.utils import six\n"
10431043
"from django.utils.deprecation import RenameMethodsBase\n"
10441044
"from django.utils.encoding import force_str, force_text\n"
10451045
"from django.utils.http import urlencode\n"

0 commit comments

Comments
 (0)
Please sign in to comment.