diff --git a/autopep8.py b/autopep8.py index f93273aa..e5b73c11 100755 --- a/autopep8.py +++ b/autopep8.py @@ -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 diff --git a/test/test_autopep8.py b/test/test_autopep8.py index 7db4a246..7aa8ca0a 100755 --- a/test/test_autopep8.py +++ b/test/test_autopep8.py @@ -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'