From 9647353ed5ca0ae5c746c221a8ec1c95a5be9875 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Fri, 16 Apr 2021 23:43:52 -0700 Subject: [PATCH] Better skip_file behavior for streams --- isort/api.py | 3 +++ isort/core.py | 10 ++++++++-- isort/main.py | 1 + tests/unit/test_main.py | 25 +++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/isort/api.py b/isort/api.py index a861da122..9deb2c2c2 100644 --- a/isort/api.py +++ b/isort/api.py @@ -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. @@ -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) @@ -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) diff --git a/isort/core.py b/isort/core.py index 9f577dac6..f8f386cda 100644 --- a/isort/core.py +++ b/isort/core.py @@ -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 @@ -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 = "" @@ -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 @@ -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 diff --git a/isort/main.py b/isort/main.py index b6954d249..93bd48534 100644 --- a/isort/main.py +++ b/isort/main.py @@ -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) diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 581b3a568..e7d139406 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -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