Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: treat inline comment as empty value when key= is followed by # comment#624

Open
Jo2234 wants to merge 1 commit intotheskumar:mainfrom
Jo2234:fix/inline-comment-empty-value
Open

fix: treat inline comment as empty value when key= is followed by # comment#624
Jo2234 wants to merge 1 commit intotheskumar:mainfrom
Jo2234:fix/inline-comment-empty-value

Conversation

@Jo2234
Copy link

@Jo2234 Jo2234 commented Mar 1, 2026

Summary

Fix inline comment being included as the value when a key has an empty value.

Problem

WITH_VALUE=hello # comment   ->  'hello'    ✓ correct
EMPTY_VALUE= # comment       ->  '# comment' ✗ wrong (should be '')
JUST_EMPTY=                  ->  ''           ✓ correct

Root cause

In parse_value(), after the = sign and trailing whitespace are consumed by _equal_sign, the next character is checked:

elif char in ('', '\n', '\r'):
    return ''

When the value is KEY= # comment, after = is consumed, the next char is #. Since # isn't in the check, it falls through to parse_unquoted_value(), which captures # comment as the value. The regex \s+#.* in parse_unquoted_value can't strip it because the # is at the start — there's no whitespace before it.

Fix

-    elif char in ('', '\n', '\r'):
+    elif char in ('', '\n', '\r', '#'):
         return ''

Adding # to the empty-value check ensures that when the value position starts with #, it's recognized as an inline comment and the value is returned as empty. The comment itself is then properly consumed by reader.read_regex(_comment) in parse_binding().

Fixes #600

When a key has an empty value followed by an inline comment, e.g.:
    EMPTY_VALUE= # comment

The parser incorrectly included the comment as the value ('# comment')
instead of returning an empty string ('').

This happened because parse_value() only checked for '', '\n', '\r'
as indicators of an empty value. After the '=' and trailing whitespace
are consumed, a '#' character should also indicate an empty value
(the rest is a comment).

The fix adds '#' to the empty-value character check in parse_value().

Fixes theskumar#600
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Inline comment is included as value when key has empty value (KEY= # comment)

1 participant