feat: add support multiple dotenv files (#1497)#3222
feat: add support multiple dotenv files (#1497)#3222samuelcolvin merged 18 commits intopydantic:masterfrom
Conversation
Allow BaseSettings to accept multiple dotenv files in Config as a list or tuple.
|
please review |
9b63964 to
5970635
Compare
5970635 to
aabd806
Compare
aabd806 to
12f4cc3
Compare
a0dc6fa to
7a050bc
Compare
| @@ -134,30 +136,47 @@ def __repr__(self) -> str: | |||
| class EnvSettingsSource: | |||
There was a problem hiding this comment.
I wonder if it would be smarter to split dotenv processing into a separate source?
I suggested the same thing here but this case is more clear cut.
There was a problem hiding this comment.
Could you elaborate on it?
There was a problem hiding this comment.
See how sources work in settings. I propose moving dotenv logic into a whole new class.
There was a problem hiding this comment.
Good idea, but it will break backwards compatibility.
This is because customise_sources needs to be changed to take 4 callables as arguments.
|
Please update. |
|
please review |
|
Thanks for the review. Please review. |
b9d1517 to
b36360c
Compare
Resolve conflicts
|
@rekyungmin @samuelcolvin IMO this is counterintuitive, I would expect the files to be loaded in the order in which they are defined which would mean the last one in the list gets the final say. So instead of this: for f in reverse(env_file):
props = load_dotenv(f)I would expect this: for f in env_file:
props = load_dotenv(f)This also matches the semi-documented behavior for multiple sources in python-dotenv. As long as it's consistent I suppose not a huge deal either way, I'm just excited to see this feature!! What's left here to merge? |
In Python, it is common for the rightmost value to win (e.g. unpacking, dict union). @samuelcolvin, what do you think? |
|
Hey, any news on this PR? would love to see it merged! :) |
There was a problem hiding this comment.
Very nearly there, one tiny comment, but more generally I agree with @phillipuniverse - the right most entry in the list/tuple should "win".
Otherwise this is ready to go.
Please update.
| __pydantic_self__._init_private_attributes() | ||
|
|
||
| @no_type_check | ||
| @no_type_check # noqa: C901 (ignore complexity) |
There was a problem hiding this comment.
| @no_type_check # noqa: C901 (ignore complexity) | |
| @no_type_check |
|
please update. |
|
Thanks @rekyungmin for this, and everyone for your feedback. |
Change Summary
Allow
BaseSettingsto accept multiple .env files in Config as a list or tuple.The first value takes priority over the second value., this i changed below, the last file now takes priority.Example:
.envfile:.env.prodfile:test code:
Related issue number
#1497
Checklist
changes/<pull request or issue id>-<github username>.mdfile added describing change(see changes/README.md for details)