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
feat: add support multiple dotenv files (#1497) #3222
feat: add support multiple dotenv files (#1497) #3222
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otherwise LGTM.
pydantic/env_settings.py
Outdated
@@ -134,30 +136,47 @@ def __repr__(self) -> str: | |||
class EnvSettingsSource: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you elaborate on it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See how sources work in settings. I propose moving dotenv logic into a whole new class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, but it will break backwards compatibility.
This is because customise_sources
needs to be changed to take 4 callables as arguments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's leave to v2.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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/main.py
Outdated
@@ -337,7 +337,7 @@ def __init__(__pydantic_self__, **data: Any) -> None: | |||
object_setattr(__pydantic_self__, '__fields_set__', fields_set) | |||
__pydantic_self__._init_private_attributes() | |||
|
|||
@no_type_check | |||
@no_type_check # noqa: C901 (ignore complexity) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@no_type_check # noqa: C901 (ignore complexity) | |
@no_type_check |
please update. |
Thanks @rekyungmin for this, and everyone for your feedback. |
Change Summary
Allow
BaseSettings
to 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:
.env
file:.env.prod
file:test code:
Related issue number
#1497
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)