From 3d24226446384f9c7be46d6b219b0be0e23f704c Mon Sep 17 00:00:00 2001 From: Josh Vera Date: Sun, 13 Jul 2025 11:47:26 -0600 Subject: [PATCH] fix: Use monkeypatch for proper test isolation in model restrictions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace @patch.dict decorator with pytest monkeypatch fixture in test_fallback_with_shorthand_restrictions to ensure proper environment variable cleanup between tests. This prevents OPENAI_ALLOWED_MODELS from leaking into subsequent tests. Also remove the manual clearing of _restriction_service singleton as it's no longer needed with proper environment variable isolation. This fixes test isolation issues where o3-pro tests would fail when run after restriction tests due to environment variable persistence. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- tests/test_model_restrictions.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/test_model_restrictions.py b/tests/test_model_restrictions.py index 2639c03..e3fd48d 100644 --- a/tests/test_model_restrictions.py +++ b/tests/test_model_restrictions.py @@ -656,9 +656,13 @@ class TestAutoModeWithRestrictions: model = ModelProviderRegistry.get_preferred_fallback_model(ToolModelCategory.FAST_RESPONSE) assert model == "o4-mini" - @patch.dict(os.environ, {"OPENAI_ALLOWED_MODELS": "mini", "GEMINI_API_KEY": "", "OPENAI_API_KEY": "test-key"}) - def test_fallback_with_shorthand_restrictions(self): + def test_fallback_with_shorthand_restrictions(self, monkeypatch): """Test fallback model selection with shorthand restrictions.""" + # Use monkeypatch to set environment variables with automatic cleanup + monkeypatch.setenv("OPENAI_ALLOWED_MODELS", "mini") + monkeypatch.setenv("GEMINI_API_KEY", "") + monkeypatch.setenv("OPENAI_API_KEY", "test-key") + # Clear caches and reset registry import utils.model_restrictions from providers.registry import ModelProviderRegistry @@ -694,6 +698,3 @@ class TestAutoModeWithRestrictions: registry._initialized_providers.clear() registry._providers.update(original_providers) registry._initialized_providers.update(original_initialized) - - # Clear the restriction service to prevent state leakage - utils.model_restrictions._restriction_service = None