fix: expand ~ in find_dotenv and the %dotenv magic - #714
Merged
Merged
Conversation
find_dotenv joins filename onto each directory it searches, so an absolute filename is already returned as is when it exists, but `~/.env` was searched for literally. Expand it first, which also fixes IPython's `%dotenv ~/.env`. Follows #615, which did the same for dotenv_path and the CLI --file option.
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.
Follow-up to #615 (#525), which made
dotenv_pathand the CLI--fileoption expand a leading~. Two entry points still didn't:HOME/.envexistsfind_dotenv("~/.env")'''<home>/.env'%dotenv ~/.envWhy it's a separate PR:
find_dotenvsearches upward forfilename, so expanding~changes what a search function does with its argument. It already returns an absolutefilenameunchanged when it exists (os.path.join(dir, "/abs")is"/abs"), and this treats~/...the same way. The only behaviour change is thatfind_dotenv("~/.env")no longer looks for a literal~directory in each parent, which seems very unlikely to be intended.Change: one line at the top of
find_dotenv, plus a docstring note. The README's%dotenvsection gets a~/path/to/.envexample.Tests:
test_find_dotenv_expands_userandtest_ipython_path_expands_userboth fail without the fix and pass with it. Both setHOMEandUSERPROFILEso they work on Windows. Locally: 288 passed, ruff and format are clean, and mypy passes on 3.10 and 3.14.