From b583c12d8218bf499b5063b2a3fe657a6b2b2a3b Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Thu, 1 Oct 2020 23:04:25 -0700 Subject: [PATCH] Fix #1486: Google profile not quite correct. --- CHANGELOG.md | 2 ++ isort/output.py | 1 + isort/profiles.py | 1 + isort/settings.py | 1 + isort/sorting.py | 3 +++ tests/unit/profiles/test_google.py | 20 ++++++++++++++++++-- 6 files changed, 26 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 712e554d0..150256ea0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/ - Fixed #1482: pylama integration is not working correctly out-of-the-box. - Fixed #1492: --check does not work with stdin source. - Fixed #1499: isort gets confused by single line, multi-line style comments when using float-to-top. +Potentially breaking changes: + - Fixed #1486: "Google" profile is not quite Google style. Goal Zero: (Tickets related to aspirational goal of achieving 0 regressions for remaining 5.0.0 lifespan): - Implemented #1472: Full testing of stdin CLI Options diff --git a/isort/output.py b/isort/output.py index d7a14009a..d2633ffdd 100644 --- a/isort/output.py +++ b/isort/output.py @@ -102,6 +102,7 @@ def sorted_imports( lexicographical=config.lexicographical, length_sort=config.length_sort, reverse_relative=config.reverse_relative, + group_by_package=config.group_by_package, ), ) diff --git a/isort/profiles.py b/isort/profiles.py index 77afef242..bfc9b4f7b 100644 --- a/isort/profiles.py +++ b/isort/profiles.py @@ -22,6 +22,7 @@ "lexicographical": True, "single_line_exclusions": ("typing",), "order_by_type": False, + "group_by_package": True, } open_stack = { "force_single_line": True, diff --git a/isort/settings.py b/isort/settings.py index a173366a9..a4e5905ad 100644 --- a/isort/settings.py +++ b/isort/settings.py @@ -169,6 +169,7 @@ class _Config: force_grid_wrap: int = 0 force_sort_within_sections: bool = False lexicographical: bool = False + group_by_package: bool = False ignore_whitespace: bool = False no_lines_before: FrozenSet[str] = frozenset() no_inline_sort: bool = False diff --git a/isort/sorting.py b/isort/sorting.py index 780747a3d..3d3961367 100644 --- a/isort/sorting.py +++ b/isort/sorting.py @@ -58,6 +58,7 @@ def section_key( lexicographical: bool = False, length_sort: bool = False, reverse_relative: bool = False, + group_by_package: bool = False, ) -> str: section = "B" @@ -65,6 +66,8 @@ def section_key( match = re.match(r"^from (\.+)\s*(.*)", line) if match: line = f"from {' '.join(match.groups())}" + if group_by_package and line.strip().startswith("from"): + line = line.split(" import", 1)[0] if lexicographical: line = _import_line_intro_re.sub("", _import_line_midline_import_re.sub(".", line)) diff --git a/tests/unit/profiles/test_google.py b/tests/unit/profiles/test_google.py index 3a40bbc30..4a3b41313 100644 --- a/tests/unit/profiles/test_google.py +++ b/tests/unit/profiles/test_google.py @@ -5,6 +5,22 @@ google_isort_test = partial(isort_test, profile="google") +def test_google_code_snippet_shared_example(): + """Tests snippet examples directly shared with the isort project. + See: https://github.com/PyCQA/isort/issues/1486. + """ + google_isort_test( + """import collections +import cProfile +""" + ) + google_isort_test( + """from a import z +from a.b import c +""" + ) + + def test_google_code_snippet_one(): google_isort_test( '''# coding=utf-8 @@ -156,12 +172,13 @@ def test_google_code_snippet_one(): from .interpreters import ad from .interpreters import batching from .interpreters import invertible_ad as iad -from .interpreters.invertible_ad import custom_ivjp from .interpreters import masking from .interpreters import partial_eval as pe from .interpreters import pxla from .interpreters import xla +from .interpreters.invertible_ad import custom_ivjp from .lib import xla_bridge as xb +from .lib import xla_client as xc # Unused imports to be exported from .lib.xla_bridge import device_count from .lib.xla_bridge import devices @@ -170,7 +187,6 @@ def test_google_code_snippet_one(): from .lib.xla_bridge import host_ids from .lib.xla_bridge import local_device_count from .lib.xla_bridge import local_devices -from .lib import xla_client as xc from .traceback_util import api_boundary from .tree_util import Partial from .tree_util import tree_flatten