Fix duplicated loading of environment variables when collecting settings#222
Merged
LucasQR merged 4 commits intobenavlabs:mainfrom Nov 19, 2025
Merged
Fix duplicated loading of environment variables when collecting settings#222LucasQR merged 4 commits intobenavlabs:mainfrom
LucasQR merged 4 commits intobenavlabs:mainfrom
Conversation
This was referenced Nov 19, 2025
Contributor
Author
|
Just to clarify that the code is broken at the moment without these changes @igorbenav @carlosplanchon |
Collaborator
…g the native pydantic casting of env variables to lists via JSON conversion
Collaborator
|
Hey @rragundez , I reviewed this one, and before I can approve and merge it, I'd like to know if this and the other two prs are finished. I intend to review ll three before merging them |
Contributor
Author
|
Hi @LucasQR , thanks so much for reviewing it :) Good point about if the other PRs are finished, the WIP might be confusing, I will make sure to be clear if it is finished or not. |
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.
There are several potential bugs on the current way of loading of settings via environment variables and .env files, mainly caused by a misconfiguration where both, the
pydanticway and thestarletteway of loading the settings are being used.This can create quite obfuscated bugs as each one has its own logic and loading time (at import and/or at instantiation). I think the reason why it has not presented itself in a big way in the current form of the code is because all settings have a primitive type (even though it fails now for composite settings where the setting retains the default values and not the ones in the .env file e.g. the databases uris).
It's quite cumbersome to explain every scenario that breaks but in a nutshell there is a lack of consistency. The starlette way loads at import time, it does define the path to the .env file but it prioritizes loading from environment variables. In addition, the pydantic way loads both at import time and instantiation time, at instantiation time it does it from environment variables and it will override any values set by the starlette way at import time. To top it up, the docker compose definition adds the .env file both as environment variables and as an .env file that starlette loads into config.
This is the first PR of several that will try to correct and simplify how settings are being loaded from environment variables or .env files, and to simplify the documentation. Also at the moment it seems the repository promotes the idea of making an .env file part of the src code and using an .env file in staging or production, which both are not a good practice.
In particular this PR at the moment removes the loading of duplicated settings coming from starlette and pydantic, and applies only the pydantic best practice. This means that composite settings cannot be formed during import time as it is now but they have to be computed, if not they would pick up the default value only, regardless if an environment variable was set.
In addition, it starts aligning with the idea that regardless if coming from environment variables directly or from and .env file, settings will always be loaded from environment variables when running the application, therefore separating both tasks ( setting env variables and loading settings)