Skip to content

Do not interpolate single-quoted values (fixes #422) - #745

Open
36taransingh5-dotcom wants to merge 4 commits into
theskumar:mainfrom
36taransingh5-dotcom:single-quote-no-interpolation
Open

36taransingh5-dotcom wants to merge 4 commits into
theskumar:mainfrom
36taransingh5-dotcom:single-quote-no-interpolation

Conversation

@36taransingh5-dotcom

Copy link
Copy Markdown

Problem

set_key writes values in single quotes intending to preserve them literally, but reading them back with dotenv_values/load_dotenv (which interpolate by default) still expands variable references inside single-quoted values. The value is silently corrupted:

from dotenv import set_key, dotenv_values

set_key(".env", "KEY", "a${X}b")
dotenv_values(".env")["KEY"]  # -> "ab" (X is undefined), not "a${X}b"

This is the round-trip data loss reported in #422.

Root cause

parser.Binding discarded the quote character before main.resolve_variables ran, so the resolver had no way to know a value was single-quoted.

Fix

  • parser.parse_value now returns the quote character alongside the value, and Binding carries it in a new quote field.
  • main.dict() uses a private _parse_full() that keeps the quote triples (the public parse() shape is unchanged).
  • resolve_variables returns single-quoted values verbatim, matching POSIX shell semantics where single quotes inhibit expansion. The literal text of a single-quoted value remains visible to later values, consistent with the existing chained-resolution behaviour: with B='${A}' then C=${B}, C resolves to the literal text ${A}.

Interpolation of double-quoted and unquoted values is unchanged.

Test changes

  • New cases cover the set_key round-trip and literal single-quote handling.
  • One existing parametrized case was updated: ({"b": "c"}, "a='${b}'", True, ...) previously expected {"a": "c"} and now expects the literal {"a": "${b}"}. This is an intentional behaviour change implementing Unable to escape posix expansion #422; flagging it explicitly for review.

Fixes #422.

Developed with AI assistance.

This branch has not been deployed

No deployments
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.

Unable to escape posix expansion

1 participant