[Fix] parser handling of inline comments with empty values#601
Open
Turtle-Hwan wants to merge 2 commits intotheskumar:mainfrom
Open
[Fix] parser handling of inline comments with empty values#601Turtle-Hwan wants to merge 2 commits intotheskumar:mainfrom
Turtle-Hwan wants to merge 2 commits intotheskumar:mainfrom
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 #600
Problem
python-dotenv incorrectly parsed inline comments when a key had an empty value. Instead of treating the comment as a comment, it was included as part of the value.
For example:
Expected:
EMPTY_VALUEshould be an empty string ('')Actual:
EMPTY_VALUEwas set to'# comment'This only happened when there was whitespace between
=and#. The library correctly handled comments when values were non-empty (e.g.,KEY=hello # commentworked fine), making this behavior inconsistent.Root Cause
The parser had two issues:
_equal_signregex pattern (=[^\S\r\n]*) was consuming whitespace after the=sign, making it impossible to detect whether a#followed whitespace or came right after=#appeared after whitespace following the=sign (which should be treated as an inline comment, not a value)Solution
I made two changes to
src/dotenv/parser.py:_equal_signfromr"(=[^\S\r\n]*)"tor"(=)"to stop consuming whitespace=sign, check if there's whitespace followed by#. If so, treat it as an inline comment and set the value to empty stringNow the parser correctly handles these cases:
a= # comment→''(whitespace before#= inline comment)a=# comment→'# comment'(no whitespace = part of value)a=## comment→'## comment'(no whitespace = part of value)Testing
Added comprehensive tests in
tests/test_parser.pyto verify:a= # comment)#(a=# comment)#(a=\t# comment)a="")#characters in various positionsAll existing tests pass (210/210).
Impact
This change makes python-dotenv's behavior consistent with other dotenv implementations (JavaScript, Bash) and fixes a parsing inconsistency within the library itself.