Lesson learned from PR #1795 (infer dependencies from setup.cfg)
When writing tests that set usethis_config.backend or usethis_config.inferred_backend directly (e.g. usethis_config.backend = BackendEnum.poetry), the state persists to subsequent tests because there is no cleanup. This caused spurious test failures in CI:
- Tests in
TestGetProjectDeps::TestPoetry and TestGetDepGroups::TestPoetry (newly added) set usethis_config.backend = BackendEnum.poetry directly inside a with block, which left inferred_backend stale after the test completed.
- Subsequent tests that relied on backend auto-detection (e.g.
TestAddDepsToGroup::test_no_pyproject_toml) received the stale inferred_backend = poetry and tried to call the Poetry subprocess, which is broken in the 'min dependencies' CI environment.
TestGetDepGroups::TestPoetry::test_not_read_without_poetry_backend failed because it set backend = none directly (not via context manager), but inferred_backend was already cached as poetry from prior tests.
Root cause
usethis_config.set(backend=...) (the context manager) saves and restores both backend AND inferred_backend. Direct attribute assignment (usethis_config.backend = ...) only updates backend, leaving inferred_backend stale. get_backend() in dispatch.py checks inferred_backend first (line 21) before doing any detection, so stale inferred_backend poisons all subsequent get_backend() calls.
Fix applied
Added usethis_config.backend = BackendEnum(BACKEND_DEFAULT) and usethis_config.inferred_backend = None to the clear_functools_caches autouse fixture in tests/conftest.py. This ensures clean backend state at the start of each test, preventing cross-test pollution.
Recommendation
- Always use
usethis_config.set(backend=...) as a context manager in tests, not direct attribute assignment.
- When that is not possible, rely on the autouse fixture reset now present in
tests/conftest.py.
Lesson learned from PR #1795 (infer dependencies from setup.cfg)
When writing tests that set
usethis_config.backendorusethis_config.inferred_backenddirectly (e.g.usethis_config.backend = BackendEnum.poetry), the state persists to subsequent tests because there is no cleanup. This caused spurious test failures in CI:TestGetProjectDeps::TestPoetryandTestGetDepGroups::TestPoetry(newly added) setusethis_config.backend = BackendEnum.poetrydirectly inside awithblock, which leftinferred_backendstale after the test completed.TestAddDepsToGroup::test_no_pyproject_toml) received the staleinferred_backend = poetryand tried to call the Poetry subprocess, which is broken in the 'min dependencies' CI environment.TestGetDepGroups::TestPoetry::test_not_read_without_poetry_backendfailed because it setbackend = nonedirectly (not via context manager), butinferred_backendwas already cached aspoetryfrom prior tests.Root cause
usethis_config.set(backend=...)(the context manager) saves and restores bothbackendANDinferred_backend. Direct attribute assignment (usethis_config.backend = ...) only updatesbackend, leavinginferred_backendstale.get_backend()indispatch.pychecksinferred_backendfirst (line 21) before doing any detection, so staleinferred_backendpoisons all subsequentget_backend()calls.Fix applied
Added
usethis_config.backend = BackendEnum(BACKEND_DEFAULT)andusethis_config.inferred_backend = Noneto theclear_functools_cachesautouse fixture intests/conftest.py. This ensures clean backend state at the start of each test, preventing cross-test pollution.Recommendation
usethis_config.set(backend=...)as a context manager in tests, not direct attribute assignment.tests/conftest.py.