[FIX] Replace unmaintained pytest-dotenv with direct dotenv loading#1992
[FIX] Replace unmaintained pytest-dotenv with direct dotenv loading#1992jaseemjaskp wants to merge 2 commits into
Conversation
pytest-dotenv 0.5.2 has not been released since Feb 2020. It monkey-patches private pytest Config internals to inject its env-loading hook, which makes it a recurring break risk across pytest major upgrades (7 → 8 → 9). It also has open issues against newer pytest versions and no path to maintenance. The plugin's only behavior in this repo is a 1-line side effect: read 'env_files' from [tool.pytest.ini_options] and call python-dotenv before collection. Replacing it with a 4-line top-level conftest.py removes a third-party dependency that touches pytest internals, with no behavior change. Backend: - Remove pytest-dotenv from [dependency-groups].test - Remove 'env_files = "test.env"' from [tool.pytest.ini_options] - Add backend/conftest.py that calls load_dotenv(Path(__file__).parent / 'test.env', override=False) (override=False matches pytest-dotenv's default of letting existing env vars win) Prompt-service: - Remove pytest-dotenv from [dependency-groups].test - Add prompt-service/conftest.py that calls load_dotenv(Path(__file__).parent / '.env', override=False) (pytest-dotenv's default is to load '.env' from rootdir when env_files is unset, so this preserves exact previous behavior) python-dotenv is already present as a direct or transitive dependency in both packages (unstract-sdk1 pins python-dotenv==1.0.1, and prompt-service pins it directly), so no new dependency is added. Verified: - prompt-service: 'uv run pytest --collect-only -q' collects 19/19 tests cleanly, matching pre-change count. - conftest.py round-trip: load_dotenv reads .env and populates os.environ for PG_BE_HOST/PG_BE_PORT as expected. Follow-up to review feedback on #1981.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (2)
📒 Files selected for processing (1)
Summary by CodeRabbit
WalkthroughThis PR migrates test environment loading from the unmaintained pytest-dotenv plugin to direct python-dotenv configuration via pytest's conftest.py mechanism. A new conftest.py loads test.env at import time, and pyproject.toml files are updated to remove the old plugin and add explicit pytest constraints. ChangesTest environment setup migration
🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
| Filename | Overview |
|---|---|
| backend/conftest.py | New top-level conftest that replaces pytest-dotenv for the backend; correctly loads test.env with override=False and prints a diagnostic when the file is missing. |
| backend/pyproject.toml | Removes pytest-dotenv from the test dependency group and replaces the env_files ini option with a comment pointing to the new conftest. |
| prompt-service/pyproject.toml | Removes pytest-dotenv from test deps, but the promised replacement prompt-service/conftest.py was never added; .env will no longer be loaded before test collection. |
| backend/uv.lock | Lockfile correctly removes pytest-dotenv and its resolved entry; no other dependency changes. |
| prompt-service/uv.lock | Lockfile correctly removes pytest-dotenv; python-dotenv remains as a direct dependency. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[pytest starts] --> B{conftest.py present?}
subgraph backend ["backend/ (complete)"]
B1[backend/conftest.py] --> C1["load_dotenv(test.env, override=False)"]
C1 --> D1[env vars available before collection]
end
subgraph prompt ["prompt-service/ (incomplete)"]
B2["prompt-service/conftest.py (MISSING)"] --> C2["load_dotenv(.env, override=False)"]
C2 --> D2[env vars available before collection]
B3[pytest-dotenv REMOVED] -.->|"was providing this"| C2
end
B --> B1
B --> B2
B2:::missing
classDef missing fill:#f88,stroke:#c00,color:#000
Comments Outside Diff (1)
-
prompt-service/pyproject.toml, line 30-37 (link)Missing replacement
conftest.pyfor prompt-serviceThe PR description states that
prompt-service/conftest.pywas added to replacepytest-dotenv's default behavior of loading.envfrom the rootdir. However, that file is absent from the diff —prompt-service/conftest.pydoes not exist in the repository. Withpytest-dotenvremoved and no top-level conftest in place, any env vars inprompt-service/.envthat tests relied on will no longer be injected before collection, silently breaking tests that depend on those values.Prompt To Fix With AI
This is a comment left during a code review. Path: prompt-service/pyproject.toml Line: 30-37 Comment: **Missing replacement `conftest.py` for prompt-service** The PR description states that `prompt-service/conftest.py` was added to replace `pytest-dotenv`'s default behavior of loading `.env` from the rootdir. However, that file is absent from the diff — `prompt-service/conftest.py` does not exist in the repository. With `pytest-dotenv` removed and no top-level conftest in place, any env vars in `prompt-service/.env` that tests relied on will no longer be injected before collection, silently breaking tests that depend on those values. How can I resolve this? If you propose a fix, please make it concise.
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 1
prompt-service/pyproject.toml:30-37
**Missing replacement `conftest.py` for prompt-service**
The PR description states that `prompt-service/conftest.py` was added to replace `pytest-dotenv`'s default behavior of loading `.env` from the rootdir. However, that file is absent from the diff — `prompt-service/conftest.py` does not exist in the repository. With `pytest-dotenv` removed and no top-level conftest in place, any env vars in `prompt-service/.env` that tests relied on will no longer be injected before collection, silently breaking tests that depend on those values.
Reviews (2): Last reviewed commit: "Address PR review: drop prompt-service c..." | Re-trigger Greptile
jaseemjaskp
left a comment
There was a problem hiding this comment.
Automated multi-agent review (PR Review Toolkit: Comment Analyzer, PR Test Analyzer, Silent Failure Hunter, Type Design Analyzer, Code Reviewer, Code Simplifier).
Intent is sound — dropping an unmaintained, internals-touching plugin is the right call, override=False correctly mirrors pytest-dotenv's env_override_existing_values=0 default, and python-dotenv==1.0.1 is already in both lockfiles (no new dependency). Type Design Analyzer found nothing (no new types).
Two issues, however, undercut the PR's central "preserves exact prior behavior" claim: (1) the prompt-service conftest is skipped by --noconftest in CI, and (2) the uv.lock revision was bumped 2→3 by a newer local uv than CI's pinned 0.6.14. Details inline.
PR review on #1992 surfaced three valid issues: 1. HIGH — prompt-service/conftest.py was skipped by tox's --noconftest anyway (tox.ini:62), and pytest-dotenv was effectively dead code in prompt-service to begin with: with no 'env_files' configured, the plugin defaulted to '<rootdir>/.env', and pytest's rootdir resolves to the repo root (where [tool.pytest.ini_options] lives) — which has no .env file. So nothing was being loaded under any path. Drop the conftest entirely; there is no behavior to preserve. 2. MEDIUM — backend/uv.lock and prompt-service/uv.lock had their 'revision' header bumped 2→3 by local uv 0.9.5, but CI pins uv 0.6.14 in .github/workflows/ci-test.yaml:32. Restored the lockfiles from main (revision 2) and surgically removed only the pytest-dotenv entries — diff is now 15 deletions per lock, no revision change, validated against uv 0.6.14 with 'uv lock --check'. 3. LOW — backend/conftest.py docstring softened (pytest-dotenv used the documented pytest_load_initial_conftests hook, not 'monkey-patched private pytest internals'), and the duplicated comment was removed. Added a small print on missing test.env to make a mis-located file debuggable rather than silently empty ('-s' is set in pyproject so prints surface). Also documents that backend tests don't run under tox in CI today, so this file only affects local/IDE runs. Verified: - prompt-service 'uv run pytest --collect-only -q': 19/19 tests collect - 'uv lock --check' on both locks passes under uv 0.6.14 (CI version)
|
Test ResultsSummary
Runner Tests - Full Report
SDK1 Tests - Full Report
|



What
Replace the unmaintained
pytest-dotenv==0.5.2plugin with a 4-lineconftest.pythat callspython-dotenvdirectly. Affectsbackend/andprompt-service/only.Why
pytest-dotenvhas not had a release since February 2020 (~6 years). It monkey-patches private_pytest.config.Configinternals to inject its env-loading hook, which makes it a recurring break risk across pytest major upgrades (7 → 8 → 9). Surfaced as a MEDIUM finding in the review of #1981.The plugin's entire behavior in this repo is one side effect: read
env_filesfrom[tool.pytest.ini_options]and calldotenv.load_dotenv()before collection. We can do that ourselves in 4 lines and drop a third-party dep that touches pytest internals.How
Backend (
backend/):pytest-dotenv==0.5.2from[dependency-groups].testenv_files = "test.env"from[tool.pytest.ini_options]backend/conftest.pythat callsload_dotenv(Path(__file__).parent / "test.env", override=False)—override=Falsemirrors pytest-dotenv's default of letting existing env vars winPrompt-service (
prompt-service/):pytest-dotenv==0.5.2from[dependency-groups].testprompt-service/conftest.pythat callsload_dotenv(Path(__file__).parent / ".env", override=False)— pytest-dotenv's default with noenv_filesset is to load.envfrom rootdir, so this preserves exact prior behaviorpython-dotenvis already in both lockfiles (transitive viaunstract-sdk1and direct in prompt-service), so no new dependency is added — pytest-dotenv is the only thing removed.Can this PR break any existing features. If yes, please list possible items. If no, please explain why. (PS: Admins do not merge the PR without this section filled)
No behavior change expected. The new conftest.py replicates pytest-dotenv's semantics 1:1:
pytest_load_initial_conftestshook)override=False→ process env winsPath(__file__).parent / "file"→ service root (same place)Possible (low-likelihood) edge cases:
Database Migrations
None.
Env Config
No new env vars. The same
test.env(backend) and.env(prompt-service) files are still consumed.Relevant Docs
Related Issues or PRs
--collect-onlybreakage. Collection didn't break under pytest 9 in Bump the uv group across 15 directories with 11 updates #1981, but the underlying maintenance concern is independent of that bump and worth landing on its own.Dependencies Versions
pytest-dotenv==0.5.2python-dotenv==1.0.1(already in both lockfiles)Notes on Testing
uv run pytest --collect-only -qfromprompt-service/: 19 tests collected (same as before the change).python-dotenvround-trip: confirmedload_dotenv(Path('.env'))correctly populatesPG_BE_HOST/PG_BE_PORTfrom prompt-service's local.env.uv synchit unrelated issues in local environment (django-celery-beat==2.5.0wheel-zip corruption,psycopg2-binarybuild) — both pre-existing and unrelated to this change; CI will exercise the full backend collection.Screenshots
N/A — config-only change.
Checklist
I have read and understood the Contribution Guidelines.