Skip to content

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

Merged
fsouza merged 1 commit into
PyCQA:mainfrom
gaoflow:fix-multiline-import-backslash-continuation-259
Aug 22, 2026
Merged

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

Conversation

@gaoflow

@gaoflow gaoflow commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

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

@fsouza fsouza left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for contributing!

@fsouza
fsouza merged commit bc0c65b into PyCQA:main Aug 22, 2026
17 checks passed
fsouza added a commit that referenced this pull request Aug 22, 2026
#365)

Follow-up to #354. Instead of threading a give-up flag through
FilterMultilineImport.__init__ and __call__, skip constructing the
filter entirely when the first line has no `import` keyword: the
statement can never be filtered, and the class never accumulated any
continuation lines in that case anyway (with empty `imports` the
pending fix was immediately over). Behavior is unchanged.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
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

2 participants