-
Notifications
You must be signed in to change notification settings - Fork 489
Open
Description
Summary
When a key has an empty value followed by an inline comment, the comment text becomes the value instead of being stripped.
Reproduction
from dotenv import dotenv_values
with open('.env.test', 'w') as f:
f.write('''
WITH_VALUE=hello # comment
EMPTY_VALUE= # comment
JUST_EMPTY=
''')
values = dotenv_values('.env.test')
print("WITH_VALUE:", repr(values.get('WITH_VALUE')))
print("EMPTY_VALUE:", repr(values.get('EMPTY_VALUE')))
print("JUST_EMPTY:", repr(values.get('JUST_EMPTY')))Output:
WITH_VALUE: 'hello'
EMPTY_VALUE: '# comment'
JUST_EMPTY: ''
Expected vs Actual
| Input | Expected | Actual |
|---|---|---|
KEY=value # comment |
'value' |
'value' |
KEY= # comment |
'' |
'# comment' |
KEY= |
'' |
'' |
Question
Is this the intended behavior? It seems inconsistent that inline comments are stripped when there's a value, but included when the value is empty.
Workaround
Use quoted empty values:
KEY="" # commentThis correctly parses to ''.
Environment
- python-dotenv version: 1.2.1
- Python version: 3.11
Volunteering
If this is considered a bug rather than intended behavior, I'd be good to work on a fix and submit a PR.
Metadata
Metadata
Assignees
Labels
No labels