Skip to content

fix: crash on backslash-continuation where import is on the second line - #354

Open
gaoflow wants to merge 1 commit into
PyCQA:mainfrom
gaoflow:fix-multiline-import-backslash-continuation-259
Open

fix: crash on backslash-continuation where import is on the second line#354
gaoflow wants to merge 1 commit into
PyCQA:mainfrom
gaoflow:fix-multiline-import-backslash-continuation-259

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 25, 2026

Copy link
Copy Markdown

Fixes #259.

Problem

FilterMultilineImport.__init__ splits the first line on \bimport\b
to separate the from X fragment from the imported names. For the
valid Python pattern

from very.long.module.path \
    import SomeName, OtherName

the first line contains no import keyword, so
IMPORT_RE.split(line, maxsplit=1) returns a one-element list.
Unpacking that into two variables raised:

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

Fix

Detect the one-element result, assign self.from_ = line and
imports = "", and set give_up = True so the entire statement is
passed through unchanged. The __call__ give-up return path is also
fixed so it does not spuriously prepend "import " in this case.

Test

Added test_filter_code_backslash_continuation_import_on_second_line
which exercises exactly the pattern from the issue report.

173 passed in 0.78s

This pull request was prepared with the assistance of AI, under my direction and review.

…line

``FilterMultilineImport.__init__`` assumed that the first line always
contains the ``import`` keyword and therefore that ``IMPORT_RE.split()``
always returns two parts.  For the valid Python pattern

    from very.long.module.path \
        import SomeName, OtherName

the first line has no ``import``, so the split returned a one-element
list and Python raised ``ValueError: not enough values to unpack``.

Fix by detecting the one-element split, storing the whole first line
in ``self.from_`` with ``imports = ""``, and setting ``give_up = True``
so the statement is passed through unchanged.  The ``__call__`` give_up
path is also corrected so it does not spuriously insert ``"import "``
into the reconstruction in this case.

Fixes PyCQA#259
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.

Crash on specific pattern

1 participant