Skip to content

Commit

Permalink
Teach the E265 fixer to fix only E265
Browse files Browse the repository at this point in the history
Previously it would end up changing E266 as well, which the user
may not want it to do (for example if they've disabled E266).

This also adds a battery of tests to excercise the differences
between these errors.
  • Loading branch information
PeterJCLaw committed Sep 16, 2022
1 parent f3ac0b9 commit 211d822
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 5 additions & 2 deletions autopep8.py
Expand Up @@ -825,9 +825,12 @@ def fix_e265(self, result):
target = self.source[result['line'] - 1]

indent = _get_indentation(target)
comment = target.lstrip(' \t#')
line = target.lstrip(' \t')
pos = next((index for index, c in enumerate(line) if c != '#'))
hashes = line[:pos]
comment = line[pos:].lstrip(' \t')

fixed = indent + ('# ' + comment if comment.strip() else '\n')
fixed = indent + hashes + (' ' + comment if comment.strip() else '\n')

self.source[result['line'] - 1] = fixed

Expand Down
24 changes: 24 additions & 0 deletions test/test_autopep8.py
Expand Up @@ -2232,12 +2232,36 @@ def test_e265(self):
with autopep8_context(line) as result:
self.assertEqual(fixed, result)

def test_e265_only(self):
line = "##A comment\n#B comment\n123\n"
fixed = "## A comment\n# B comment\n123\n"
with autopep8_context(line, options=['--select=E265']) as result:
self.assertEqual(fixed, result)

def test_ignore_e265(self):
line = "## A comment\n#B comment\n123\n"
fixed = "# A comment\n#B comment\n123\n"
with autopep8_context(line, options=['--ignore=E265']) as result:
self.assertEqual(fixed, result)

def test_e266(self):
line = "## comment\n123\n"
fixed = "# comment\n123\n"
with autopep8_context(line) as result:
self.assertEqual(fixed, result)

def test_e266_only(self):
line = "## A comment\n#B comment\n123\n"
fixed = "# A comment\n#B comment\n123\n"
with autopep8_context(line, options=['--select=E266']) as result:
self.assertEqual(fixed, result)

def test_ignore_e266(self):
line = "##A comment\n#B comment\n123\n"
fixed = "## A comment\n# B comment\n123\n"
with autopep8_context(line, options=['--ignore=E266']) as result:
self.assertEqual(fixed, result)

def test_e271(self):
line = 'True and False\n'
fixed = 'True and False\n'
Expand Down

0 comments on commit 211d822

Please sign in to comment.