fix: do not treat an inline comment as an empty value - #679
Open
dchaudhari7177 wants to merge 1 commit into
Open
fix: do not treat an inline comment as an empty value#679dchaudhari7177 wants to merge 1 commit into
dchaudhari7177 wants to merge 1 commit into
Conversation
`KEY= # comment` parsed to `"# comment"` rather than `""`, while `KEY=value # comment` correctly parsed to `"value"`. `_equal_sign` consumes the horizontal whitespace that follows `=`, so by the time `parse_unquoted_value` runs, its `\s+#.*` strip no longer has the whitespace it needs to recognise the comment, and the comment text becomes the value. Pass down whether that whitespace was present and, when it was, treat a value that starts with `#` as a comment. `KEY=#c` with no whitespace still parses to `"#c"`, matching the existing `a=b#c` behaviour. Fixes theskumar#600 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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 #600.
Problem
An inline comment after an empty unquoted value becomes the value:
EMPTY_VALUEshould be'', the same wayWITH_VALUEgets its comment stripped.Cause
_equal_signis(=[^\S\r\n]*), so it consumes the horizontal whitespace right after=.parse_unquoted_valuethen strips comments withre.sub(r"\s+#.*", "", part), which requires whitespace before the#. ForKEY= # cthat whitespace is already gone, the part is# cwith the#at position 0, the substitution does not fire, and the comment is returned as the value.Fix
parse_bindingalready gets the matched=plus whitespace back fromread_regex, so it can tell whether any whitespace followed the=. That flag is threaded toparse_unquoted_value, which treats a part starting with#as a comment only when the whitespace was there.This keeps the existing rule that
#starts a comment only when preceded by whitespace, soa=b#cand the newly-covereda=#cboth still parse to a literal value containing the#.a=b # cbba= # c# ca=#c#c#ca=b#cb#cb#ca=Tests
Four cases added to
test_parse_stream, including the no-whitespacea=#ccase that pins the unchanged behaviour and a two-binding case confirming the reader is left in the right position.pytest tests/passes. The twotest_cli.py::test_run_*failures on my machine are pre-existing and Windows-specific (they reproduce on an unmodified checkout).🤖 Generated with Claude Code