diff --git a/README.md b/README.md index a5564312c..6bd080430 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ notified. You will notice above the \"multi\_line\_output\" setting. This setting defines how from imports wrap when they extend past the line\_length -limit and has 6 possible settings: +limit and has 12 possible settings: **0 - Grid** @@ -285,7 +285,18 @@ from third_party import ( lib4, lib5, lib6) ``` -Note: to change the how constant indents appear - simply change the +**11 - Backslash Grid** + +Same as Mode 0 - _Grid_ but uses backslashes instead of parentheses to group imports. + +```python +from third_party import lib1, lib2, lib3, \ + lib4, lib5 +``` + +## Indentation + +To change the how constant indents appear - simply change the indent property with the following accepted formats: - Number of spaces you would like. For example: 4 would cause standard diff --git a/isort/wrap_modes.py b/isort/wrap_modes.py index 8e10a9474..ccfc1cd5a 100644 --- a/isort/wrap_modes.py +++ b/isort/wrap_modes.py @@ -306,6 +306,12 @@ def hanging_indent_with_parentheses(**interface): return _hanging_indent_common(use_parentheses=True, **interface) +@_wrap_mode +def backslash_grid(**interface): + interface["indent"] = interface["white_space"][:-1] + return _hanging_indent_common(use_parentheses=False, **interface) + + WrapModes = enum.Enum( # type: ignore "WrapModes", {wrap_mode: index for index, wrap_mode in enumerate(_wrap_modes.keys())} ) diff --git a/tests/unit/test_wrap_modes.py b/tests/unit/test_wrap_modes.py index d27f92e77..c95a75136 100644 --- a/tests/unit/test_wrap_modes.py +++ b/tests/unit/test_wrap_modes.py @@ -1,5 +1,6 @@ from hypothesis_auto import auto_pytest_magic +import isort from isort import wrap_modes auto_pytest_magic(wrap_modes.grid, auto_allow_exceptions_=(ValueError,)) @@ -21,6 +22,7 @@ imports=["one", "two"], ) auto_pytest_magic(wrap_modes.hanging_indent_with_parentheses, auto_allow_exceptions_=(ValueError,)) +auto_pytest_magic(wrap_modes.backslash_grid, auto_allow_exceptions_=(ValueError,)) def test_wrap_mode_interface(): @@ -65,3 +67,30 @@ def test_auto_saved(): ) == '*\x12\x07\U0009e994🁣"\U000ae787\x0e \x00\U0001ae99\U0005c3e7\U0004d08e \x1e ' ) + + +def test_backslash_grid(): + """Tests the backslash_grid grid wrap mode, ensuring it matches formatting expectations. + See: https://github.com/PyCQA/isort/issues/1434 + """ + assert ( + isort.code( + """ +from kopf.engines import loggers, posting +from kopf.reactor import causation, daemons, effects, handling, lifecycles, registries +from kopf.storage import finalizers, states +from kopf.structs import (bodies, configuration, containers, diffs, + handlers as handlers_, patches, resources) +""", + multi_line_output=11, + line_length=88, + combine_as_imports=True, + ) + == """ +from kopf.engines import loggers, posting +from kopf.reactor import causation, daemons, effects, handling, lifecycles, registries +from kopf.storage import finalizers, states +from kopf.structs import bodies, configuration, containers, diffs, \\ + handlers as handlers_, patches, resources +""" + )