Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistency in priorities of variable expansion #282

Closed
mathieu1 opened this issue Oct 29, 2020 · 1 comment · Fixed by #287
Closed

Inconsistency in priorities of variable expansion #282

mathieu1 opened this issue Oct 29, 2020 · 1 comment · Fixed by #287

Comments

@mathieu1
Copy link

Behavior

python-dotenv priorities are correctly documented, but inconsistent when loading a .env file with internal dependencies in variable expansion.

Consider the following .env file:

HOSTNAME=localhost:8080
URL=http://${HOSTNAME}

Currently, if we set the HOSTNAME environment variable to localhost:8081, then run load_dotenv(), then the environment ends up with HOSTNAME=localhost:8081 and URL=http://localhost8080, which at first is surprising.

Underlying reason

The reason for this is that (by default) load_dotenv() does not override set environment variables, but when doing variable expansion inside a given file, load_dotenvprefers the variable as set in the file than from the environment (as documented in https://github.com/theskumar/python-dotenv#usages).

Workaround

A workaround consists of changing the priorities in the file explicitly, as in this example:

HOSTNAME=${HOSTNAME:-localhost:8080}
URL=http://${HOSTNAME}

Suggestion

I would suggest that the variable expansion priority logic follow the same behavior as the general "overriding or not" behavior. More precisely:

  • when override==False, variable expansion should follow the following priorities:
    • Value of that variable in the environment.
    • Value of that variable in the .env file.
    • Default value, if provided.
    • Empty string.
  • when override==True, variable expansion should follow the currently documented priorities:
    • Value of that variable in the .env file.
    • Value of that variable in the environment.
    • Default value, if provided.
    • Empty string.

It would consist of a breaking change in behavior, but it seems that in the case it does matter, the current behavior is probably not what is expected.

Is that a change you'd be willing to accept?

@bbc2
Copy link
Collaborator

bbc2 commented Nov 15, 2020

I think that you suggestion makes sense. I've given it a go in #287. Would it solve your issue?

@bbc2 bbc2 closed this as completed in #287 Dec 5, 2020
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 a pull request may close this issue.

2 participants