Skip to content

Commit

Permalink
Merge pull request #1706 from PyCQA/issue/1705/stream/skip-failure
Browse files Browse the repository at this point in the history
Issue/1705/stream/skip failure
  • Loading branch information
timothycrosley authored Apr 17, 2021
2 parents f074239 + 3dff805 commit 99faae8
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions isort/api.py
Original file line number Diff line number Diff line change
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
9 changes: 7 additions & 2 deletions isort/core.py
Original file line number Diff line number Diff line change
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,10 @@ 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")
isort_off = True
skip_file = True

if not in_quote and stripped_line == "# isort: off":
isort_off = True
Expand Down Expand Up @@ -195,7 +200,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
2 changes: 1 addition & 1 deletion isort/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand Down
1 change: 0 additions & 1 deletion isort/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion isort/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
1 change: 1 addition & 0 deletions isort/main.py
Original file line number Diff line number Diff line change
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
5 changes: 2 additions & 3 deletions isort/wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 25 additions & 0 deletions tests/unit/test_main.py
Original file line number Diff line number Diff line change
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 99faae8

Please sign in to comment.