fix: prevent ValueError in break_up_import when \r in source shifts line numbers - #355
Open
koteshyelamati wants to merge 1 commit into
Open
fix: prevent ValueError in break_up_import when \r in source shifts line numbers#355koteshyelamati wants to merge 1 commit into
koteshyelamati wants to merge 1 commit into
Conversation
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 #326
Problem
fix_code()raisesValueError: not enough values to unpack (expected 2, got 1)inbreak_up_importwhen the source contains a bare\rcharacter (e.g. a Windows CR without LF, or a CR mid-line):Root cause
io.StringIO.readlines()does not treat\ralone as a line separator, but Python's tokenizer (andstr.splitlines()) does. When pyflakes analyses the source, it usessplitlines(), so a bare\ris counted as an extra line. This causes the line numbers that pyflakes marks as import lines to be offset from the lines thatfilter_codesees viareadlines(). As a result,break_up_importis sometimes called with a continuation line that contains noimportkeyword, causing there.split()to return a 1-element list and the tuple unpacking to fail.Fix
Add a guard in
break_up_import: ifre.splitdoes not produce exactly 2 parts (i.e. noimportkeyword was found), return the line unchanged rather than crashing. This is a minimal, defensive fix at the point of failure.