Skip to content

Commit

Permalink
Teach E265 fixer to ignore special comments even in the middle of files
Browse files Browse the repository at this point in the history
This appears to be expected for compatibility, though is a somewhat
unexpected behaviour given that these comments are almost certainly
not actually 'shebang' comments.
  • Loading branch information
PeterJCLaw committed Sep 16, 2022
1 parent 7af08a3 commit f4a40b1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions autopep8.py
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/test_autopep8.py
Expand Up @@ -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"
Expand Down

0 comments on commit f4a40b1

Please sign in to comment.