From 09916f67ef711655f0c64c7aad01f472a85f310b Mon Sep 17 00:00:00 2001 From: Peter Law Date: Fri, 16 Sep 2022 20:32:16 +0100 Subject: [PATCH] Teach E265 fixer to ignore special comments even in the middle of files This appears to be expected for compatibility, though is a somewhat unexpected behaviour given that these comments are almost certainly not actually 'shebang' comments. --- autopep8.py | 4 ++++ test/test_autopep8.py | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/autopep8.py b/autopep8.py index fd0d3823..25f915ee 100755 --- a/autopep8.py +++ b/autopep8.py @@ -830,6 +830,10 @@ def fix_e265(self, result): hashes = line[:pos] comment = line[pos:].lstrip(' \t') + # Ignore special comments, even in the middle of the file. + if comment.startswith('!'): + return + fixed = indent + hashes + (' ' + comment if comment.strip() else '\n') self.source[result['line'] - 1] = fixed diff --git a/test/test_autopep8.py b/test/test_autopep8.py index 7aa8ca0a..d54423f5 100755 --- a/test/test_autopep8.py +++ b/test/test_autopep8.py @@ -2232,6 +2232,16 @@ def test_e265(self): with autopep8_context(line) as result: self.assertEqual(fixed, result) + def test_e265_ignores_special_comments(self): + line = "#!python\n456\n" + with autopep8_context(line) as result: + self.assertEqual(line, result) + + def test_e265_ignores_special_comments_in_middle_of_file(self): + line = "123\n\n#!python\n456\n" + with autopep8_context(line) as result: + self.assertEqual(line, result) + def test_e265_only(self): line = "##A comment\n#B comment\n123\n" fixed = "## A comment\n# B comment\n123\n"