Skip to content

Fix IndexError in extract_package_name on malformed import lines#338

Merged
fsouza merged 1 commit intoPyCQA:mainfrom
worksbyfriday:fix-extract-package-name-crash
Feb 18, 2026
Merged

Fix IndexError in extract_package_name on malformed import lines#338
fsouza merged 1 commit intoPyCQA:mainfrom
worksbyfriday:fix-extract-package-name-crash

Conversation

@worksbyfriday
Copy link
Copy Markdown
Contributor

Summary

  • Fixes crash when line.split() produces fewer than 2 elements (e.g., a bare import with no module name, or lines corrupted by embedded carriage returns)
  • Returns None instead of raising IndexError

Fixes #324

Test plan

  • Added unit test for extract_package_name("import") (bare import, no module)
  • Added integration test for fix_code("import t\rs\nimport test\nimport0") (the exact reproduction from the issue)
  • All 171 existing tests continue to pass

When a line starts with "import" or "from" but has no subsequent word
(e.g., due to embedded carriage returns), `line.split()[1]` raises
IndexError. Return None instead.

Fixes PyCQA#324

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator

@fsouza fsouza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@fsouza fsouza merged commit aefc058 into PyCQA:main Feb 18, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Index out of range error in autoflake.fix_code

2 participants