Repository navigation
Conversation
set_key() writes the key as given, but the unquoted key grammar only
accepts [^=#\s]+, so a key with a space, a # or an = produces a line the
parser cannot read. The variable then disappears without an error - the
file gets a warning at read time and the value is silently lost:
>>> set_key('.env', 'MY VAR', 'x')
>>> dotenv_values('.env')
{} # and a 'could not parse' warning
The same happens for 'A#B' and 'A=B'. Since the parser already supports
quoted keys ('A B' = 'x' is read back as A B), set_key now quotes the key
whenever it contains whitespace, a # or an =, so the value survives the
round-trip:
>>> set_key('.env', 'MY VAR', 'x')
>>> dotenv_values('.env')
{'MY VAR': 'x'}
Two keys stay impossible to write and now raise ValueError instead of
writing a file that loses the line: an empty key, and a key that needs
quoting and contains a single quote (the quoted-key grammar has no
escape). Updating an existing quoted key rewrites it in place and keeps
the rest of the file untouched.
Tests: keys with a space, #, = and a tab round-trip through set_key and
get_key without swallowing the line after them; an existing quoted key is
updated in place; empty and quote-containing keys raise ValueError. These
seven tests fail on main and tests/test_main.py goes from 153 to 160
passing (5 failures remain in tests/test_cli.py, all from $HOME paths in
this sandbox).
This branch has not been deployed
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.
set_key()writes the key exactly as it is given, but the unquoted key grammar only accepts[^=#\s]+. A key with a space, a#or an=therefore produces a line the parser cannot read, and the variable is lost without any error:Same for
A#BandA=B— all 13 values I tried disappeared. The call reports success, so a script that builds key names from labels writes a.envthat silently misses the variable.The parser already knows how to read a quoted key (
'A B'='x'comes back as{'A B': 'x'}), soset_key()now quotes the key whenever it contains whitespace,#or=:Two keys remain impossible to represent, and now raise
ValueErrorinstead of writing a line that will be dropped: an empty key, and a key that needs quoting and contains a'(the quoted-key grammar has no escape for it). A key that can be written unquoted is left exactly as before, and updating an existing quoted key rewrites that one line in place, keeping the rest of the file byte for byte.Tests
#, an=and a tab surviveset_key→get_key, and the line after them is not swallowed;'a b'='old'→'a b'='new',c=1untouched);a b'craiseValueError.On
mainthose seven tests fail;tests/test_main.pygoes from 153 to 160 passing with the change. (tests/test_cli.pystill has failures from$HOMEhandling in this sandbox, unrelated.)Written with AI assistance.