Skip to content

Commit

Permalink
Better skip_file behavior for streams
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Apr 17, 2021
1 parent f074239 commit 9647353
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
3 changes: 3 additions & 0 deletions isort/api.py
Expand Up @@ -125,6 +125,7 @@ def sort_stream(
file_path: Optional[Path] = None,
disregard_skip: bool = False,
show_diff: Union[bool, TextIO] = False,
raise_on_skip: bool = True,
**config_kwargs,
) -> bool:
"""Sorts any imports within the provided code stream, outputs to the provided output stream.
Expand All @@ -150,6 +151,7 @@ def sort_stream(
config=config,
file_path=file_path,
disregard_skip=disregard_skip,
raise_on_skip=raise_on_skip,
**config_kwargs,
)
_output_stream.seek(0)
Expand Down Expand Up @@ -187,6 +189,7 @@ def sort_stream(
_internal_output,
extension=extension or (file_path and file_path.suffix.lstrip(".")) or "py",
config=config,
raise_on_skip=raise_on_skip,
)
except FileSkipComment:
raise FileSkipComment(content_source)
Expand Down
10 changes: 8 additions & 2 deletions isort/core.py
Expand Up @@ -30,6 +30,7 @@ def process(
input_stream: TextIO,
output_stream: TextIO,
extension: str = "py",
raise_on_skip: bool = True,
config: Config = DEFAULT_CONFIG,
) -> bool:
"""Parses stream identifying sections of contiguous imports and sorting them
Expand Down Expand Up @@ -61,6 +62,7 @@ def process(
first_import_section: bool = True
indent: str = ""
isort_off: bool = False
skip_file: bool = False
code_sorting: Union[bool, str] = False
code_sorting_section: str = ""
code_sorting_indent: str = ""
Expand Down Expand Up @@ -149,7 +151,11 @@ def process(

for file_skip_comment in FILE_SKIP_COMMENTS:
if file_skip_comment in line:
raise FileSkipComment("Passed in content")
if raise_on_skip:
raise FileSkipComment("Passed in content")
else:
isort_off = True
skip_file = True

if not in_quote and stripped_line == "# isort: off":
isort_off = True
Expand Down Expand Up @@ -195,7 +201,7 @@ def process(
not_imports = bool(in_quote) or was_in_quote or in_top_comment or isort_off
if not (in_quote or was_in_quote or in_top_comment):
if isort_off:
if stripped_line == "# isort: on":
if not skip_file and stripped_line == "# isort: on":
isort_off = False
elif stripped_line.endswith("# isort: split"):
not_imports = True
Expand Down
1 change: 1 addition & 0 deletions isort/main.py
Expand Up @@ -1089,6 +1089,7 @@ def main(argv: Optional[Sequence[str]] = None, stdin: Optional[TextIOWrapper] =
show_diff=show_diff,
file_path=file_path,
extension=ext_format,
raise_on_skip=False,
)
elif "/" in file_names and not allow_root:
printer = create_terminal_printer(color=config.color_output)
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/test_main.py
Expand Up @@ -918,6 +918,31 @@ def test_unsupported_encodings(tmpdir, capsys):
out, error = capsys.readouterr()


def test_stream_skip_file(tmpdir, capsys):
input_with_skip = """
# isort: skip_file
import b
import a
"""
stream_with_skip = as_stream(input_with_skip)
main.main(["-"], stdin=stream_with_skip)
out, error = capsys.readouterr()
assert out == input_with_skip

input_without_skip = input_with_skip.replace("isort: skip_file", "generic comment")
stream_without_skip = as_stream(input_without_skip)
main.main(["-"], stdin=stream_without_skip)
out, error = capsys.readouterr()
assert (
out
== """
# generic comment
import a
import b
"""
)


def test_only_modified_flag(tmpdir, capsys):
# ensures there is no verbose output for correct files with only-modified flag

Expand Down

0 comments on commit 9647353

Please sign in to comment.