Skip to content

fix: apply expanduser to dotenv_path - #615

Merged
theskumar merged 4 commits into
theskumar:mainfrom
veeceey:fix/issue-525-expanduser
Oct 1, 2026
Merged

theskumar merged 4 commits into
theskumar:mainfrom
veeceey:fix/issue-525-expanduser

Conversation

@veeceey

@veeceey veeceey commented Feb 8, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Apply os.path.expanduser() (for str paths) and Path.expanduser() (for Path objects) to dotenv_path so that ~ is properly expanded to the user's home directory
  • The fix preserves the original type: str inputs remain str, Path inputs remain Path
  • Applied in DotEnv.__init__, set_key(), and unset_key() — all places where dotenv_path is used directly

Fixes #525

Test plan

  • All 106 existing tests in test_main.py pass with no modifications
  • Verified that dotenv_path="~/.env" correctly expands to the full home directory path
  • Verified that Path("~/.env") correctly expands via Path.expanduser()
  • Verified that paths without ~ are unchanged
  • Verified that None is handled correctly (no expansion attempted)

Apply os.path.expanduser() / Path.expanduser() to dotenv_path so that
~ is properly expanded to the user's home directory. The fix preserves
the original type (str stays str, Path stays Path) and is applied in
DotEnv.__init__, set_key, and unset_key.
@veeceey

veeceey commented Feb 19, 2026

Copy link
Copy Markdown
Contributor Author

Friendly ping - any chance someone could take a look at this when they get a chance? Happy to make any changes if needed.

The library fix covered set and unset, which go through set_key and
unset_key, but list, get and run open --file directly, so
`dotenv --file=~/.env list` still failed. Expand it once in the cli group.

Test every library entry point with str and Path inputs, and every CLI
subcommand. Add a CHANGELOG entry.
@theskumar

Copy link
Copy Markdown
Owner

Thanks @veeceey, and sorry for the wait. Your change is correct: I reproduced #525 independently and arrived at the same fix in the same three places. I pushed two commits on top of yours; your commit is unchanged:

  1. 5b5b514: merges main into the branch, with no conflicts.
  2. 6cd93a5: covers the CLI and adds tests.

Why the CLI change. With only the library fix, the dotenv command was split. set and unset go through set_key/unset_key, so they expanded ~. But list and get open --file directly and run checks it with os.path.isfile, so those still failed. That matters when the shell doesn't expand ~: --file=~/.env, a quoted '~/.env', or Windows cmd. The commit expands --file once in the cli group, so all five subcommands agree.

HOME/.env exists, cwd elsewhere main your commit now
dotenv_values/load_dotenv/get_key/set_key/unset_key("~/.env"), str or Path ❌ ✅ ✅
dotenv --file=~/.env set/unset ❌ ✅ ✅
dotenv --file=~/.env list/get/run ❌ exit 2 ❌ exit 2 ✅

Tests. 13 library cases cover every entry point with str and Path, plus load_dotenv, and check that str/Path/None keep their type. 5 CLI cases run each subcommand as a subprocess. They set both HOME and USERPROFILE, so they work on Windows too. Each mutant below was applied on its own and caught:

Mutant Caught by
DotEnv doesn't expand dotenv_values, get_key, load_dotenv and type cases
set_key / unset_key doesn't expand their cases
Path inputs not expanded the Path cases
Path turned into str type case
None expanded type case, plus existing tests
CLI doesn't expand CLI list, get, run

Locally: 286 passed, ruff check/ruff format --check are clean, and mypy passes on 3.10 and 3.14. I also added a CHANGELOG entry crediting you.

find_dotenv("~/.env") (and IPython's %dotenv ~/.env) still doesn't expand. It changes what a search function does with its filename argument, so I'll propose it in a separate PR rather than holding this one up.

@theskumar
theskumar merged commit 7914148 into theskumar:main Oct 1, 2026
9 checks passed
theskumar added a commit that referenced this pull request Oct 1, 2026
find_dotenv and the IPython %dotenv magic now expand a leading ~ in the file name. Follows #615 (#525).
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.

support expanduser() for dotenv_path

2 participants