Skip to content

Commit

Permalink
Resolve #1357: Add ability to treat all or select comments as code
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Jul 31, 2020
1 parent efd4352 commit 6ba0b3d
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Changelog

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)

### 5.2.2 July 30, 2020
- Fixed #1356: return status when arguments are passed in without files or a content stream.

Expand Down
11 changes: 9 additions & 2 deletions isort/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,9 @@ def _sort_imports(
if line == "# isort: on\n":
isort_off = False
new_input += line
elif line in ("# isort: split\n", "# isort: off\n", None):
elif line in ("# isort: split\n", "# isort: off\n", None) or str(line).endswith(
"# isort: split\n"
):
if line == "# isort: off\n":
isort_off = True
if current:
Expand All @@ -448,9 +450,12 @@ def _sort_imports(
extra_space += "\n"
current = current[:-1]
extra_space = extra_space.replace("\n", "", 1)
new_input += output.sorted_imports(
sorted_output = output.sorted_imports(
parsed, config, extension, import_type="import"
)
if sorted_output.strip() != output.strip():
made_changes = True
new_input += sorted_output
new_input += extra_space
current = ""
new_input += line or ""
Expand Down Expand Up @@ -541,6 +546,8 @@ def _sort_imports(
not stripped_line
or stripped_line.startswith("#")
and (not indent or indent + line.lstrip() == line)
and not config.treat_all_comments_as_code
and stripped_line not in config.treat_comments_as_code
):
import_section += line
elif stripped_line.startswith(IMPORT_START_IDENTIFIERS):
Expand Down
12 changes: 12 additions & 0 deletions isort/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,18 @@ def _build_arg_parser() -> argparse.ArgumentParser:
"Still it can be a great shortcut for collecting imports every once in a while when you put"
" them in the middle of a file.",
)
parser.add_argument(
"--treat-comment-as-code",
dest="treat_comments_as_code",
action="append",
help="Tells isort to treat the specified single line comment(s) as if they are code.",
)
parser.add_argument(
"--treat-all-comment-as-code",
dest="treat_all_comments_as_code",
action="store_true",
help="Tells isort to treat all single line comments as if they are code.",
)
parser.add_argument(
"--formatter",
dest="formatter",
Expand Down
4 changes: 4 additions & 0 deletions isort/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
and not last.endswith("'''")
and "isort:imports-" not in last
and "isort: imports-" not in last
and not config.treat_all_comments_as_code
and not last.strip() in config.treat_comments_as_code
):
categorized_comments["above"]["from"].setdefault(import_from, []).insert(
0, out_lines.pop(-1)
Expand Down Expand Up @@ -404,6 +406,8 @@ def file_contents(contents: str, config: Config = DEFAULT_CONFIG) -> ParsedConte
and not last.endswith("'''")
and "isort:imports-" not in last
and "isort: imports-" not in last
and not config.treat_all_comments_as_code
and not last.strip() in config.treat_comments_as_code
):
categorized_comments["above"]["straight"].setdefault(module, []).insert(
0, out_lines.pop(-1)
Expand Down
2 changes: 2 additions & 0 deletions isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ class _Config:
formatter: str = ""
formatting_function: Optional[Callable[[str, str, object], str]] = None
color_output: bool = False
treat_comments_as_code: FrozenSet[str] = frozenset()
treat_all_comments_as_code: bool = False

def __post_init__(self):
py_version = self.py_version
Expand Down
95 changes: 95 additions & 0 deletions tests/test_ticketed_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,98 @@ def test_isort_supports_formatting_plugins_issue_1353():
assert isort.code("import a", formatter="example") == "import a\n" # formatting plugin
with pytest.raises(exceptions.FormattingPluginDoesNotExist):
assert isort.code("import a", formatter="madeupfake") == "import a\n" # non-existent plugin


def test_treating_comments_as_code_issue_1357():
"""Test to ensure isort provides a way to treat comments as code.
See: https://github.com/timothycrosley/isort/issues/1357
"""
assert isort.code("""# %%
import numpy as np
np.array([1,2,3])
# %%
import pandas as pd
pd.Series([1,2,3])
# %%
# This is a comment on the second import
import pandas as pd
pd.Series([4,5,6])""", treat_comments_as_code=["# comment1", "# %%"]) == """# %%
import numpy as np
np.array([1,2,3])
# %%
import pandas as pd
pd.Series([1,2,3])
# %%
# This is a comment on the second import
import pandas as pd
pd.Series([4,5,6])
"""
assert isort.code("""# %%
import numpy as np
np.array([1,2,3])
# %%
import pandas as pd
pd.Series([1,2,3])
# %%
# This is a comment on the second import
import pandas as pd
pd.Series([4,5,6])""", treat_comments_as_code=["# comment1", "# %%"], float_to_top=True) == """# %%
import numpy as np
# This is a comment on the second import
import pandas as pd
np.array([1,2,3])
# %%
pd.Series([1,2,3])
# %%
pd.Series([4,5,6])
"""
assert isort.code("""# %%
import numpy as np
np.array([1,2,3])
# %%
import pandas as pd
pd.Series([1,2,3])
# %%
# This is a comment on the second import
import pandas as pd
pd.Series([4,5,6])""", treat_all_comments_as_code=True) == """# %%
import numpy as np
np.array([1,2,3])
# %%
import pandas as pd
pd.Series([1,2,3])
# %%
# This is a comment on the second import
import pandas as pd
pd.Series([4,5,6])
"""
assert isort.code("""import b
# these are special imports that have to do with installing X plugin
import c
import a
""", treat_all_comments_as_code=True) == """import b
# these are special imports that have to do with installing X plugin
import a
import c
"""

0 comments on commit 6ba0b3d

Please sign in to comment.