diff --git a/CHANGELOG.md b/CHANGELOG.md index a22e15b1c..b0f0c336a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/ - Fixed #1772: skip-gitignore will check files not in the git repository. - Fixed #1762: in some cases when skip-gitignore is set, isort fails to skip any files. - Fixed #1767: Encoding issues surfacing when invalid characters set in `__init__.py` files during placement. + - Fixed #1771: Improved handling of skips against named streamed in content. ### 5.9.1 June 21st 2021 [hotfix] - Fixed #1758: projects with many files and skip_ignore set can lead to a command-line overload. diff --git a/isort/main.py b/isort/main.py index 0c3950bc9..34ce3fdd0 100644 --- a/isort/main.py +++ b/isort/main.py @@ -1087,9 +1087,10 @@ def main(argv: Optional[Sequence[str]] = None, stdin: Optional[TextIOWrapper] = if show_files: sys.exit("Error: can't show files for streaming input.") + input_stream = sys.stdin if stdin is None else stdin if check: incorrectly_sorted = not api.check_stream( - input_stream=sys.stdin if stdin is None else stdin, + input_stream=input_stream, config=config, show_diff=show_diff, file_path=file_path, @@ -1098,15 +1099,18 @@ def main(argv: Optional[Sequence[str]] = None, stdin: Optional[TextIOWrapper] = wrong_sorted_files = incorrectly_sorted else: - api.sort_stream( - input_stream=sys.stdin if stdin is None else stdin, - output_stream=sys.stdout, - config=config, - show_diff=show_diff, - file_path=file_path, - extension=ext_format, - raise_on_skip=False, - ) + try: + api.sort_stream( + input_stream=input_stream, + output_stream=sys.stdout, + config=config, + show_diff=show_diff, + file_path=file_path, + extension=ext_format, + raise_on_skip=False, + ) + except FileSkipped: + sys.stdout.write(input_stream.read()) elif "/" in file_names and not allow_root: printer = create_terminal_printer( color=config.color_output, error=config.format_error, success=config.format_success diff --git a/tests/unit/test_main.py b/tests/unit/test_main.py index 9039787cc..78d1e9374 100644 --- a/tests/unit/test_main.py +++ b/tests/unit/test_main.py @@ -370,6 +370,23 @@ def build_input_content(): import b +def function(): + pass +""" + ) + + # if file is skipped it should output unchanged. + main.main( + ["-", "--filename", "x.py", "--skip", "x.py", "--filter-files"], + stdin=build_input_content(), + ) + out, error = capsys.readouterr() + assert not error + assert out == ( + """ +import b +import a + def function(): pass """