fix: crash on backslash-continuation where import is on the second line - #354
Open
gaoflow wants to merge 1 commit into
Open
fix: crash on backslash-continuation where import is on the second line#354gaoflow wants to merge 1 commit into
import is on the second line#354gaoflow wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #259.
Problem
FilterMultilineImport.__init__splits the first line on\bimport\bto separate the
from Xfragment from the imported names. For thevalid Python pattern
the first line contains no
importkeyword, soIMPORT_RE.split(line, maxsplit=1)returns a one-element list.Unpacking that into two variables raised:
Fix
Detect the one-element result, assign
self.from_ = lineandimports = "", and setgive_up = Trueso the entire statement ispassed through unchanged. The
__call__give-up return path is alsofixed so it does not spuriously prepend
"import "in this case.Test
Added
test_filter_code_backslash_continuation_import_on_second_linewhich exercises exactly the pattern from the issue report.
This pull request was prepared with the assistance of AI, under my direction and review.