Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion autoflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,12 +533,17 @@ def break_up_import(line: str) -> str:
if not newline:
return line

indentation, imports = re.split(
parts = re.split(
pattern=r"\bimport\b",
string=line,
maxsplit=1,
)

if len(parts) != 2:
# No 'import' keyword found (e.g., a continuation line with \r line endings)
return line

indentation, imports = parts
indentation += "import "
assert newline

Expand Down
20 changes: 20 additions & 0 deletions test_autoflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -3619,3 +3619,23 @@ def write(*_: Any) -> None:

if __name__ == "__main__":
unittest.main()


class TestCarriageReturnInImport(unittest.TestCase):
"""Tests for fix of crash with carriage return in multiline import (issue #326)."""

def test_carriage_return_in_multiline_parenthesized_import(self):
"""Should not crash when a line within parenthesized import starts with \r."""
source = (
"from t import (\n"
"\r a,\n"
" b,\n"
")\n"
"from t import (\n"
" a,\n"
" b,\n"
")\n"
)
# Should not raise ValueError
result = autoflake.fix_code(source, remove_all_unused_imports=True)
self.assertIsNotNone(result)
Loading