Skip to content

Preserve quoted dotenv key names when setting values - #735

Open
FanWu-ai wants to merge 1 commit into
theskumar:mainfrom
FanWu-ai:fix/quoted-key-round-trip
Open

FanWu-ai wants to merge 1 commit into
theskumar:mainfrom
FanWu-ai:fix/quoted-key-round-trip

Conversation

@FanWu-ai

Copy link
Copy Markdown

Problem

set_key() writes key names without the quoting accepted by the dotenv parser. Updating an existing 'APP NAME'='old' entry returns success but writes APP NAME='new', which cannot be read back. Names containing # or = are also corrupted. The CLI uses the same writer.

Change

Quote names when required by dotenv syntax, independently of value quote_mode. Keep ordinary unquoted names unchanged. Reject keys that this syntax cannot represent before opening or rewriting the target file. This does not add POSIX-shell export-name validation, which is separate from dotenv serialization.

Regression tests cover creating and updating quoted keys, all three value quote modes, neighboring entries, ordinary names, export=True, invalid keys with existing and missing files, and CLI round trips.

Validation

Python 3.12.14:

  • Original complete suite: 288 passed.
  • Fixed complete suite: 335 passed.
  • tox -e lint,manifest,coverage-report: passed, including all five configured mypy targets.
  • Pinned pre-commit hooks, mkdocs build --strict, source/wheel build, and git diff --check: passed.
  • Independently reviewed and the full fixed suite rerun successfully.

AI assistance was used to investigate, implement, test, and review this change.

@FanWu-ai
FanWu-ai marked this pull request as ready for review October 21, 2026 22:07

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.

1 participant