Skip to content

Commit d90f8f4

Browse files
bp72fsouza
andauthored
Add support of ignore comment on the top of the file (#291)
* Add support of ignore comment on the top of the file * test_autoflake: add some empty lines to make test more realistic --------- Co-authored-by: francisco souza <108725+fsouza@users.noreply.github.com>
1 parent 23cfe6f commit d90f8f4

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ options:
127127
-s, --stdout print changed text to stdout. defaults to true when formatting stdin, or to false otherwise
128128
```
129129

130+
To ignore the file, you can also add a comment to the top of the file:
131+
```python
132+
# autoflake: skip_file
133+
import os
134+
```
130135

131136
## Configuration
132137

autoflake.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@
6363

6464
MAX_PYTHON_FILE_DETECTION_BYTES = 1024
6565

66+
IGNORE_COMMENT_REGEX = re.compile(
67+
r"\s*#\s{1,}autoflake:\s{1,}\bskip_file\b",
68+
re.MULTILINE,
69+
)
70+
6671

6772
def standard_paths() -> Iterable[str]:
6873
"""Yield paths to standard modules."""
@@ -904,6 +909,9 @@ def fix_code(
904909
if not source:
905910
return source
906911

912+
if IGNORE_COMMENT_REGEX.search(source):
913+
return source
914+
907915
# pyflakes does not handle "nonlocal" correctly.
908916
if "nonlocal" in source:
909917
remove_unused_variables = False

test_autoflake.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,6 +2008,49 @@ class SystemTests(unittest.TestCase):
20082008

20092009
"""System tests."""
20102010

2011+
def test_skip_file(self) -> None:
2012+
skipped_file_file_text = """
2013+
# autoflake: skip_file
2014+
import re
2015+
import os
2016+
import my_own_module
2017+
x = 1
2018+
"""
2019+
with temporary_file(skipped_file_file_text) as filename:
2020+
output_file = io.StringIO()
2021+
autoflake._main(
2022+
argv=["my_fake_program", filename, "--stdout"],
2023+
standard_out=output_file,
2024+
standard_error=None,
2025+
)
2026+
self.assertEqual(
2027+
skipped_file_file_text,
2028+
output_file.getvalue(),
2029+
)
2030+
2031+
def test_skip_file_with_shebang_respect(self) -> None:
2032+
skipped_file_file_text = """
2033+
#!/usr/bin/env python3
2034+
2035+
# autoflake: skip_file
2036+
2037+
import re
2038+
import os
2039+
import my_own_module
2040+
x = 1
2041+
"""
2042+
with temporary_file(skipped_file_file_text) as filename:
2043+
output_file = io.StringIO()
2044+
autoflake._main(
2045+
argv=["my_fake_program", filename, "--stdout"],
2046+
standard_out=output_file,
2047+
standard_error=None,
2048+
)
2049+
self.assertEqual(
2050+
skipped_file_file_text,
2051+
output_file.getvalue(),
2052+
)
2053+
20112054
def test_diff(self) -> None:
20122055
with temporary_file(
20132056
"""\

0 commit comments

Comments
 (0)