Summary
Four unit tests fail in the full pytest tests/ run but pass in isolation:
tests/test_client.py::TestKalshiClientFromEnv::test_from_env_demo_flag
tests/test_client.py::TestKalshiClientFromEnv::test_from_env_base_url_override
tests/test_async_client.py::TestAsyncKalshiClientFromEnv::test_from_env_demo_flag
tests/test_async_client.py::TestAsyncKalshiClientFromEnv::test_from_env_base_url_override
This is pre-existing (reproduces on main, independent of #443/PR #444) and is an environment-variable test-isolation leak.
Root cause
tests/integration/conftest.py has a session-scoped autouse fixture that does:
if "KALSHI_DEMO" not in os.environ:
_monkeypatch_session.setenv("KALSHI_DEMO", "true")
_monkeypatch_session is a session-scoped MonkeyPatch, so it only undoes at session end — not between tests. Once the first test under tests/integration/ triggers fixture setup (the integration tests pytest.skip() in their bodies, but autouse fixtures still set up first), KALSHI_DEMO=true is set for the remainder of the session and leaks into later unit tests.
test_from_env_base_url_override sets KALSHI_API_BASE_URL but does not delenv("KALSHI_DEMO"), so from_env sees the leaked KALSHI_DEMO=true alongside an explicit base_url and raises the "Conflicting environment: demo=True together with explicit base_url" error.
Reproduction (deterministic)
uv run pytest tests/integration tests/test_client.py::TestKalshiClientFromEnv \
tests/test_async_client.py::TestAsyncKalshiClientFromEnv -q -p no:randomly
# -> 4 failed, 8 passed, 313 skipped
Excluding tests/integration makes all four pass.
Suggested fixes (any one)
- Make the demo-env autouse fixture function-scoped (or use a fresh
MonkeyPatch per test), so KALSHI_DEMO doesn't persist across the session.
- Have the
TestKalshiClientFromEnv / TestAsyncKalshiClientFromEnv from_env tests monkeypatch.delenv("KALSHI_DEMO", raising=False) (they already delenv KALSHI_API_BASE_URL).
- Add a session-end teardown that explicitly restores
KALSHI_DEMO.
Option 1 fixes the class of bug (any unit test reading KALSHI_DEMO is currently exposed), so it's preferable.
Summary
Four unit tests fail in the full
pytest tests/run but pass in isolation:This is pre-existing (reproduces on
main, independent of #443/PR #444) and is an environment-variable test-isolation leak.Root cause
tests/integration/conftest.pyhas a session-scoped autouse fixture that does:_monkeypatch_sessionis a session-scopedMonkeyPatch, so it only undoes at session end — not between tests. Once the first test undertests/integration/triggers fixture setup (the integration testspytest.skip()in their bodies, but autouse fixtures still set up first),KALSHI_DEMO=trueis set for the remainder of the session and leaks into later unit tests.test_from_env_base_url_overridesetsKALSHI_API_BASE_URLbut does notdelenv("KALSHI_DEMO"), sofrom_envsees the leakedKALSHI_DEMO=truealongside an explicitbase_urland raises the "Conflicting environment: demo=True together with explicit base_url" error.Reproduction (deterministic)
uv run pytest tests/integration tests/test_client.py::TestKalshiClientFromEnv \ tests/test_async_client.py::TestAsyncKalshiClientFromEnv -q -p no:randomly # -> 4 failed, 8 passed, 313 skippedExcluding
tests/integrationmakes all four pass.Suggested fixes (any one)
MonkeyPatchper test), soKALSHI_DEMOdoesn't persist across the session.TestKalshiClientFromEnv/TestAsyncKalshiClientFromEnvfrom_envtestsmonkeypatch.delenv("KALSHI_DEMO", raising=False)(they already delenvKALSHI_API_BASE_URL).KALSHI_DEMO.Option 1 fixes the class of bug (any unit test reading
KALSHI_DEMOis currently exposed), so it's preferable.