From 56a1e7e474a17fc11bc3fad1c84561c317bbc730 Mon Sep 17 00:00:00 2001 From: Kotesh Kumar Yelamati Date: Thu, 2 Jul 2026 12:44:24 -0400 Subject: [PATCH] fix: guard break_up_import against missing import keyword when \r shifts line numbers --- autoflake.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/autoflake.py b/autoflake.py index 95a8153..c7e1bc0 100755 --- a/autoflake.py +++ b/autoflake.py @@ -496,11 +496,16 @@ def filter_from_import(line: str, unused_module: Iterable[str]) -> str: Return line without unused import modules, or `pass` if all of the module in import is unused. """ - indentation, imports = re.split( + parts = re.split( pattern=r"\bimport\b", string=line, maxsplit=1, ) + if len(parts) != 2: + # No 'import' keyword found — line-number mismatch caused by \r + # in source (splitlines vs readlines differ); return unchanged. + return line + indentation, imports = parts match = re.search( pattern=r"\bfrom\s+([^ ]+)", string=indentation,