fix: preserve comment indentation when rewriting requirements files - #1081
Open
citizen204 wants to merge 1 commit into
Open
fix: preserve comment indentation when rewriting requirements files#1081citizen204 wants to merge 1 commit into
citizen204 wants to merge 1 commit into
Conversation
`pip-requirements-parser` unconditionally strips comment-only lines, which discards the leading indentation `pip-compile` uses to mark a "# via ..." backreference comment as belonging to the preceding requirement. When `--fix` rewrote such a file, these comments lost their indentation. Recover the original indentation from the source file when the physical line consists solely of the comment. Fixes pypa#566
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.
Summary
--fixstrips the indentation of comment-only lines (e.g. thepip-compile-style# via ...backreference comment) when rewriting a requirements file, even though it correctly preserves everything else. The root cause is thatpip-requirements-parser's comment parsing unconditionally.strip()s comment-only lines, discarding the leading whitespace thatpip-compileuses to signal that a comment belongs to the preceding requirement rather than being a standalone comment.Since that stripping happens inside the third-party parser before
pip-auditever sees the line, this fix recovers the original indentation from the source file at write time: for any parsed comment-only line, if the corresponding physical line in the original file is (once stripped) identical to the parsed comment, the original raw line (including its indentation) is written back instead of the parser's stripped version.Changes
pip_audit/_dependency_source/requirement.py: inRequirementSource._fix_file, read the original file's lines once, and for eachCommentRequirementLinerestore the original line (with its indentation) when the physical line consists solely of that comment.test/dependency_source/test_requirement.py: addtest_requirement_source_fix_comments_preserve_indentcovering thepip-compile-style indented# via ...comment from the issue, and updatetest_requirement_source_fix_explicit_subdep_comment_retention's expected output/docstring — this test previously codified the buggy unindented behavior as expected, and now asserts the corrected indentation instead.Fixes #566