[FIX] Replace unmaintained pytest-dotenv with direct dotenv loading#1992
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 backend; correctly loads test.env with override=False and logs when the file is absent. |
| backend/pyproject.toml | Removes pytest-dotenv from test deps and removes env_files from pytest.ini_options, with a comment pointing to the new conftest.py. |
| prompt-service/pyproject.toml | Removes pytest-dotenv from test deps but the promised replacement conftest.py was never added, leaving .env loading unhooked for prompt-service tests. |
| backend/uv.lock | Lockfile updated to remove pytest-dotenv entry; consistent with pyproject.toml change. |
| prompt-service/uv.lock | Lockfile updated to remove pytest-dotenv entry; consistent with pyproject.toml change. |
Sequence Diagram
sequenceDiagram
participant pytest
participant conftest.py
participant load_dotenv
participant test_env as test.env / .env
participant tests
Note over pytest,tests: Before (pytest-dotenv plugin)
pytest->>pytest: pytest_load_initial_conftests hook
pytest->>test_env: read env_files from pytest.ini
test_env-->>pytest: env vars injected
Note over pytest,tests: After (new conftest.py)
pytest->>conftest.py: import conftest at startup
conftest.py->>load_dotenv: "load_dotenv(Path / "test.env", override=False)"
load_dotenv->>test_env: read file
test_env-->>load_dotenv: vars
load_dotenv-->>pytest: env vars set
pytest->>tests: "collect & run"
Comments Outside Diff (1)
-
prompt-service/pyproject.toml, line 30-37 (link)Missing
prompt-service/conftest.pyreplacementpytest-dotenvhas been removed from the test dependencies, but the replacementprompt-service/conftest.pydescribed in the PR is absent from both the diff and the repository. Without it, noload_dotenv()call runs before collection, so any env vars that tests rely on fromprompt-service/.envwill be silently missing at test time.Prompt To Fix With AI
This is a comment left during a code review. Path: prompt-service/pyproject.toml Line: 30-37 Comment: **Missing `prompt-service/conftest.py` replacement** `pytest-dotenv` has been removed from the test dependencies, but the replacement `prompt-service/conftest.py` described in the PR is absent from both the diff and the repository. Without it, no `load_dotenv()` call runs before collection, so any env vars that tests rely on from `prompt-service/.env` will be silently missing at test time. 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 `prompt-service/conftest.py` replacement**
`pytest-dotenv` has been removed from the test dependencies, but the replacement `prompt-service/conftest.py` described in the PR is absent from both the diff and the repository. Without it, no `load_dotenv()` call runs before collection, so any env vars that tests rely on from `prompt-service/.env` will be silently missing at test time.
Reviews (3): Last reviewed commit: "Merge branch 'main' into fix/replace-pyt..." | 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.