Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Commit 7758ff4

Browse files
authored
Merge pull request #20 from prusse-martin/fb-fix-isort-skipped-files
Fb fix isort skipped files
2 parents bcb154d + e2ad3fb commit 7758ff4

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

esss_fix_format/cli.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,12 @@ def main(files_or_directories, check, stdin, commit):
9696
errors.append(error_msg)
9797

9898
sorter = isort.SortImports(file_contents=new_contents, settings_path=settings_path)
99-
# strangely, if the entire file is skipped by an "isort:skip_file"
100-
# instruction in the docstring, SortImports doesn't even contain an
101-
# "output" attribute
102-
if hasattr(sorter, 'output'):
103-
new_contents = sorter.output
99+
# On older versions if the entire file is skipped (eg.: by an "isort:skip_file")
100+
# instruction in the docstring, SortImports doesn't even contain an "output" attribute.
101+
# In some recent versions it is `None`.
102+
new_contents = getattr(sorter, 'output', None)
103+
if new_contents is None:
104+
new_contents = original_contents
104105

105106
new_contents = fix_whitespace(new_contents.splitlines(True), eol, ends_with_eol)
106107
changed = new_contents != original_contents

tests/test_esss_fix_format.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,20 @@ def test_empty_file(tmpdir, sort_cfg_to_tmpdir):
202202
run([str(filename)], expected_exit=0)
203203

204204

205-
def test_skip_entire_file(tmpdir, sort_cfg_to_tmpdir):
206-
"""Check that a module-level isort:skip_file correctly skips that file"""
207-
source = textwrap.dedent('''\
208-
"""
209-
isort:skip_file
210-
"""
211-
import sys
212-
''')
205+
@pytest.mark.parametrize(
206+
'source',
207+
[
208+
'',
209+
'"""\nisort:skip_file\n"""\nimport sys\nimport os\n',
210+
'# isort:skip_file\nimport sys\nimport os\n',
211+
],
212+
ids=[
213+
'empty file',
214+
'module-level isort:skip_file docstring',
215+
'module-level isort:skip_file comment',
216+
]
217+
)
218+
def test_skip_entire_file(tmpdir, sort_cfg_to_tmpdir, source):
213219
filename = tmpdir.join('test.py')
214220
filename.write(source)
215221
output = run([str(filename)], expected_exit=0)

0 commit comments

Comments
 (0)