summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2021-04-16 23:43:52 -0700
committerTimothy Crosley <timothy.crosley@gmail.com>2021-04-16 23:43:52 -0700
commit9647353ed5ca0ae5c746c221a8ec1c95a5be9875 (patch)
tree2c297339c0f829db996e2327ec4c05c6691a9fbc
parentf074239e5e39c2dd9e4d373e3c6ce528dae3f78c (diff)
downloadisort-issue/1705/stream/skip-failure.tar.gz
Better skip_file behavior for streamsissue/1705/stream/skip-failure
-rw-r--r--isort/api.py3
-rw-r--r--isort/core.py10
-rw-r--r--isort/main.py1
-rw-r--r--tests/unit/test_main.py25
4 files changed, 37 insertions, 2 deletions
diff --git a/isort/api.py b/isort/api.py
index a861da12..9deb2c2c 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 9f577dac..f8f386cd 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 b6954d24..93bd4853 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 581b3a56..e7d13940 100644
--- a/tests/unit/test_main.py
+++ b/tests/unit/test_main.py
@@ -918,6 +918,31 @@ __revision__ = 'יייי'
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