From 9647353ed5ca0ae5c746c221a8ec1c95a5be9875 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Fri, 16 Apr 2021 23:43:52 -0700 Subject: [PATCH 1/7] 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 From 5592f424c13ff16bc688832cab176dd1af33e41c Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Fri, 16 Apr 2021 23:52:25 -0700 Subject: [PATCH 2/7] Implemented #1705: More intuitive handling of isort:skip_file comments on streams. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 155c3d88c..fb41a693c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/ ### 5.9.0 TBD - Fixed (https://github.com/PyCQA/isort/pull/1695) added imports being added to doc string in some cases. - Implemented #1697: Provisional support for PEP 582: skip `__pypackages__` directories by default. + - Implemented #1705: More intuitive handling of isort:skip_file comments on streams. ### 5.8.0 March 20th 2021 - Fixed #1631: as import comments can in some cases be duplicated. From fab05f271b19b702351e33dda94802a4eb95f2b6 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Fri, 16 Apr 2021 23:53:08 -0700 Subject: [PATCH 3/7] Small typo fix a vs an --- isort/exceptions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/isort/exceptions.py b/isort/exceptions.py index a73444ba5..3a54d6429 100644 --- a/isort/exceptions.py +++ b/isort/exceptions.py @@ -56,7 +56,7 @@ class FileSkipComment(FileSkipped): def __init__(self, file_path: str): super().__init__( - f"{file_path} contains an file skip comment and was skipped.", file_path=file_path + f"{file_path} contains a file skip comment and was skipped.", file_path=file_path ) From 008d0a6e9042dd2387bf785431f8973adec21ecf Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Sat, 17 Apr 2021 00:00:14 -0700 Subject: [PATCH 4/7] Fix mergeable if statement found by deep source --- isort/wrap.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/isort/wrap.py b/isort/wrap.py index e993ae0f3..5fb4631f7 100644 --- a/isort/wrap.py +++ b/isort/wrap.py @@ -130,9 +130,8 @@ def line(content: str, line_separator: str, config: Config = DEFAULT_CONFIG) -> lines[-1] = content + ")" + config.comment_prefix + comment[:-1] return line_separator.join(lines) return f"{content}{splitter}\\{line_separator}{cont_line}" - elif len(content) > config.line_length and wrap_mode == Modes.NOQA: # type: ignore - if "# NOQA" not in content: - return f"{content}{config.comment_prefix} NOQA" + elif len(content) > config.line_length and wrap_mode == Modes.NOQA and "# NOQA" not in content: # type: ignore + return f"{content}{config.comment_prefix} NOQA" return content From a5ce76fd400da73be0d56609274d8473904f845d Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Sat, 17 Apr 2021 00:01:15 -0700 Subject: [PATCH 5/7] Remove unnecessary else found by deep source --- isort/core.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/isort/core.py b/isort/core.py index f8f386cda..cc9d7c810 100644 --- a/isort/core.py +++ b/isort/core.py @@ -153,9 +153,8 @@ def process( if file_skip_comment in line: if raise_on_skip: raise FileSkipComment("Passed in content") - else: - isort_off = True - skip_file = True + isort_off = True + skip_file = True if not in_quote and stripped_line == "# isort: off": isort_off = True From bedc20739a7bf48f6beb79af90a42d143ec030b4 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Sat, 17 Apr 2021 00:03:13 -0700 Subject: [PATCH 6/7] Skip issue for intentionally blank function --- isort/io.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/isort/io.py b/isort/io.py index 2f30be0c7..7b107ab02 100644 --- a/isort/io.py +++ b/isort/io.py @@ -66,7 +66,7 @@ def read(filename: Union[str, Path]) -> Iterator["File"]: class _EmptyIO(StringIO): - def write(self, *args, **kwargs): + def write(self, *args, **kwargs): # skipcq: PTC-W0049 pass From 3dff8058f09d323517a5e47b5454c8d981342938 Mon Sep 17 00:00:00 2001 From: Timothy Crosley Date: Sat, 17 Apr 2021 00:04:12 -0700 Subject: [PATCH 7/7] Remove blank line after doc string found by deep source --- isort/hooks.py | 1 - 1 file changed, 1 deletion(-) diff --git a/isort/hooks.py b/isort/hooks.py index dfd7eb3dc..244e7ea11 100644 --- a/isort/hooks.py +++ b/isort/hooks.py @@ -53,7 +53,6 @@ def git_hook( :return number of errors if in strict mode, 0 otherwise. """ - # Get list of files modified and staged diff_cmd = ["git", "diff-index", "--cached", "--name-only", "--diff-filter=ACMRTUXB", "HEAD"] if lazy: