fix: parse empty unquoted value with inline comment as empty string#663
Open
Noethix55555 wants to merge 1 commit into
Open
fix: parse empty unquoted value with inline comment as empty string#663Noethix55555 wants to merge 1 commit into
Noethix55555 wants to merge 1 commit into
Conversation
An unquoted empty value followed by an inline comment (e.g. `KEY= # comment`) was parsed as the comment text because the equal-sign regex consumed the whitespace after `=`, leaving no whitespace for the comment-stripping rule to match. When the value is empty (whitespace follows `=`) and starts with `#`, treat the rest of the line as a comment and the value as empty. Values with no space before `#` (e.g. `KEY=#novalue`, `KEY=a#b`) are unchanged. Closes theskumar#600
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
Fixes #600.
An unquoted empty value followed by an inline comment is parsed as the comment text instead of an empty string:
Root cause
_equal_sign = (=[^\S\r\n]*)consumes the whitespace after=, so the captured unquoted value is# commentwith no leading whitespace.parse_unquoted_valuestrips comments withre.sub(r"\s+#.*", "", part), which requires whitespace before#and therefore does not strip it.Fix
In
parse_binding, after reading the equal sign, if it consumed trailing whitespace (so the value is empty) and the next character is#, treat the value as""and let the rest of the line be parsed as a comment. When there is no space before#(e.g.KEY=#novalue,KEY=a#b) the#remains part of the value, preserving existing behavior.Behavior preserved (with tests)
KEY=val # comment->valKEY=a#b->a#b(no space before#)KEY=#novalue->#novalue(no space, unchanged)KEY= # comment/KEY= # comment->''(the fix)a = b->b,a=b c->b c)Tests
Added parametrized cases in
tests/test_parser.py(a= #b,a= # b,a=#b) and an end-to-enddotenv_valuesregression intests/test_main.py. Verified they fail onmainand pass with the fix. Full test suite remains green.