fix(settings): only load MILVUS_ prefixed keys from dotenv to avoid env pollution - #3686
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: saitejabandaru-in The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
Welcome @saitejabandaru-in! It looks like this is your first PR to milvus-io/pymilvus 🎉 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3e2dd5ff81
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| dotenv_path = find_dotenv(usecwd=True) | ||
| if dotenv_path: | ||
| for k, v in dotenv_values(dotenv_path).items(): |
There was a problem hiding this comment.
In deployments using python-dotenv 1.2+ (allowed by this package's <2.0.0 dependency), setting PYTHON_DOTENV_DISABLED=1 made the previous load_dotenv() call a no-op for third-party packages. This manual find_dotenv/dotenv_values path bypasses that guard, so importing PyMilvus can still read .env and inject MILVUS_ variables in environments that explicitly disabled dotenv loading. Please gate this block on the same disable flag before reading the file.
Useful? React with 👍 / 👎.
| if k.startswith("MILVUS_") and k not in os.environ: | ||
| os.environ[k] = v |
There was a problem hiding this comment.
When a .env contains a valueless Milvus placeholder such as MILVUS_URI (a syntax python-dotenv parses as None), this assignment raises TypeError: str expected, not NoneType during import pymilvus. The old load_dotenv() path skipped None values, so this change makes the package unimportable for those .env files; check v is not None before assigning to os.environ.
Useful? React with 👍 / 👎.
…nv pollution Signed-off-by: Sai Teja Bandaru <[email protected]>
3e2dd5f to
900189a
Compare
|
Hi, I have updated the PR to address Codex feedback:
|
|
/assign @XuanYang-cn |
|
I have checked the codebase and both fixes have been applied:
|
|
Both fixes have been verified/applied:
Thanks for the review! |
Resolves #3666.
Description
Currently,
pymilvuscallsload_dotenv()unconditionally in global namespace when importingpymilvus/settings.py. This causes side effects by loading all key-value pairs from.env(such as app secrets or credentials for other systems) into the process-wideos.environenvironment variables, polluting the host application space.To solve this, this change replaces the unconditional global
load_dotenv()with a structured check usingdotenv_values(find_dotenv(usecwd=True))that filters and only injects keys prefixed withMILVUS_(e.g.MILVUS_URI,MILVUS_CONN_ALIAS) intoos.environif they are not already set.Changes
pymilvus/settings.pyto usefind_dotenv(usecwd=True)+dotenv_values()and only inject keys beginning withMILVUS_.tests/unit/test_settings.pychecking thatMILVUS_keys are loaded, while non-Milvus keys (likeMY_APP_SECRET) remain untouched in the host environment.