Skip to content

Commit

Permalink
Fix #1486: Google profile not quite correct.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Oct 2, 2020
1 parent f7a9ddc commit b583c12
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions isort/output.py
Expand Up @@ -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,
),
)

Expand Down
1 change: 1 addition & 0 deletions isort/profiles.py
Expand Up @@ -22,6 +22,7 @@
"lexicographical": True,
"single_line_exclusions": ("typing",),
"order_by_type": False,
"group_by_package": True,
}
open_stack = {
"force_single_line": True,
Expand Down
1 change: 1 addition & 0 deletions isort/settings.py
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions isort/sorting.py
Expand Up @@ -58,13 +58,16 @@ def section_key(
lexicographical: bool = False,
length_sort: bool = False,
reverse_relative: bool = False,
group_by_package: bool = False,
) -> str:
section = "B"

if reverse_relative and line.startswith("from ."):
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))
Expand Down
20 changes: 18 additions & 2 deletions tests/unit/profiles/test_google.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit b583c12

Please sign in to comment.