Skip to content

fix: handle missing 'import' keyword in break_up_import - #346

Closed
frankgoldfish wants to merge 1 commit into
PyCQA:mainfrom
frankgoldfish:fix/crash-on-backslash-multiline-import-259
Closed

fix: handle missing 'import' keyword in break_up_import#346
frankgoldfish wants to merge 1 commit into
PyCQA:mainfrom
frankgoldfish:fix/crash-on-backslash-multiline-import-259

Conversation

@frankgoldfish

Copy link
Copy Markdown

Problem

When source code contains carriage returns (`\r`) within parenthesized import statements, io.StringIO.readlines() may produce continuation lines that don't contain the import keyword. When break_up_import() is called on such a line, re.split(r'\bimport\b', line) returns only one element, and unpacking to (indentation, imports) raises:

ValueError: not enough values to unpack (expected 2, got 1)

Reproducible with:

import autoflake
s = '''
from t import (
\r  a,
    b,
)
from t import (
    a,
    b,
)
'''
autoflake.fix_code(s, remove_all_unused_imports=True)  # raises ValueError

Fix

In break_up_import(), check the number of parts returned by re.split(). If 'import' is not found in the line (i.e., it's a bare continuation line), return the line unchanged.

Tests

Added TestCarriageReturnInImport test class verifying the fix.

Fixes #326

When source code contains carriage returns (\r) within parenthesized
import statements, Python's io.StringIO.readlines() may produce
continuation lines that don't contain the 'import' keyword. In such
cases, re.split(r'\bimport\b', line) returns a list with only one
element, causing a ValueError when trying to unpack to
(indentation, imports).

Fix: check the length of parts returned by re.split in break_up_import.
If 'import' is not found in the line, return the line unchanged.

Fixes #326
@frankgoldfish

Copy link
Copy Markdown
Author

Closing this PR — found that PR #328 already addresses this issue. Apologies for the duplicate.

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.

ValueError: not enough values to unpack (expected 2, got 1) in break_up_import

1 participant