File tree Expand file tree Collapse file tree 3 files changed +54
-0
lines changed
Expand file tree Collapse file tree 3 files changed +54
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 6363
6464MAX_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
6772def 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
Original file line number Diff line number Diff line change @@ -2008,6 +2008,47 @@ 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+ # autoflake: skip_file
2035+ import re
2036+ import os
2037+ import my_own_module
2038+ x = 1
2039+ """
2040+ with temporary_file (skipped_file_file_text ) as filename :
2041+ output_file = io .StringIO ()
2042+ autoflake ._main (
2043+ argv = ["my_fake_program" , filename , "--stdout" ],
2044+ standard_out = output_file ,
2045+ standard_error = None ,
2046+ )
2047+ self .assertEqual (
2048+ skipped_file_file_text ,
2049+ output_file .getvalue (),
2050+ )
2051+
20112052 def test_diff (self ) -> None :
20122053 with temporary_file (
20132054 """\
You can’t perform that action at this time.
0 commit comments