You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Test isolation: integration conftest leaks KALSHI_DEMO into unit from_env tests (4 full-suite failures) #445
_monkeypatch_session is a session-scopedMonkeyPatch, 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 notdelenv("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.
The leaked variable is KALSHI_PRIVATE_KEY_PATH, not KALSHI_DEMO as I first wrote. The actual failure is:
kalshi.errors.KalshiAuthError: Both KALSHI_PRIVATE_KEY and KALSHI_PRIVATE_KEY_PATH are set.
Mechanism: tests/integration/conftest.py::_bridge_env_vars (session-scoped autouse) bridges a developer's ambient KALSHI_DEMO_PRIVATE_KEY_PATH → KALSHI_PRIVATE_KEY_PATH for the whole session. test_from_env_demo_flag / test_from_env_base_url_override set KALSHI_PRIVATE_KEY but — unlike their passing siblings (test_from_env_with_pem_string, test_from_env_with_key_path) — never delenv("KALSHI_PRIVATE_KEY_PATH"), so from_env() sees both and raises. (This is why it only surfaces on machines with demo creds exported, including the maintainer's; CI without demo creds wouldn't bridge a path and wouldn't trip it.)
Fix: make the two non-hermetic tests match their siblings by dropping the ambient PEM path — monkeypatch.delenv("KALSHI_PRIVATE_KEY_PATH", raising=False) in both test_client.py and test_async_client.py. The session-scoped bridge itself is left as-is (session scope is required for the session-scoped integration clients); the right boundary to harden is the unit test's own env, consistent with the rest of the class. Will be closed by PR #444.
Activity
TexasCoding commentedon Jun 9, 2026
Correction + fix incoming (PR #444).
The leaked variable is
KALSHI_PRIVATE_KEY_PATH, notKALSHI_DEMOas I first wrote. The actual failure is:Mechanism:
tests/integration/conftest.py::_bridge_env_vars(session-scoped autouse) bridges a developer's ambientKALSHI_DEMO_PRIVATE_KEY_PATH→KALSHI_PRIVATE_KEY_PATHfor the whole session.test_from_env_demo_flag/test_from_env_base_url_overridesetKALSHI_PRIVATE_KEYbut — unlike their passing siblings (test_from_env_with_pem_string,test_from_env_with_key_path) — neverdelenv("KALSHI_PRIVATE_KEY_PATH"), sofrom_env()sees both and raises. (This is why it only surfaces on machines with demo creds exported, including the maintainer's; CI without demo creds wouldn't bridge a path and wouldn't trip it.)Fix: make the two non-hermetic tests match their siblings by dropping the ambient PEM path —
monkeypatch.delenv("KALSHI_PRIVATE_KEY_PATH", raising=False)in bothtest_client.pyandtest_async_client.py. The session-scoped bridge itself is left as-is (session scope is required for the session-scoped integration clients); the right boundary to harden is the unit test's own env, consistent with the rest of the class. Will be closed by PR #444.test: make from_env tests hermetic against bridged PEM path (closes #445