From f9b1caf8c17f1c308521f90589d7dbffd9b981e1 Mon Sep 17 00:00:00 2001 From: Priyansh Nandwana Date: Sat, 5 Sep 2026 13:58:12 +0530 Subject: [PATCH] test(rate_limiter): set TZ through monkeypatch and drop the sys.path insert --- .../proxy/hooks/test_rate_limit_reset_time.py | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/tests/test_litellm/proxy/hooks/test_rate_limit_reset_time.py b/tests/test_litellm/proxy/hooks/test_rate_limit_reset_time.py index f725b674348..f380a625478 100644 --- a/tests/test_litellm/proxy/hooks/test_rate_limit_reset_time.py +++ b/tests/test_litellm/proxy/hooks/test_rate_limit_reset_time.py @@ -1,7 +1,5 @@ """Regression tests for #39816: the 429 body labels its reset time UTC.""" -import os -import sys import time from contextlib import contextmanager from datetime import datetime, timezone @@ -9,8 +7,6 @@ from unittest.mock import MagicMock import pytest -sys.path.insert(0, os.path.abspath("../../../..")) - from litellm.proxy.common_utils.proxy_rate_limit_error import ProxyRateLimitError from litellm.proxy.hooks.batch_rate_limiter import BatchFileUsage, _PROXY_BatchRateLimiter from litellm.proxy.hooks.parallel_request_limiter_v3 import ( @@ -27,17 +23,13 @@ needs_tzset = pytest.mark.skipif( @contextmanager -def a_proxy_running_in(tz: str): - previous = os.environ.get("TZ") - os.environ["TZ"] = tz +def a_proxy_running_in(tz: str, monkeypatch: pytest.MonkeyPatch): + monkeypatch.setenv("TZ", tz) time.tzset() try: yield finally: - if previous is None: - del os.environ["TZ"] - else: - os.environ["TZ"] = previous + monkeypatch.undo() time.tzset() @@ -76,8 +68,8 @@ def _parallel_limiter(): class TestParallelLimiterReportsUTC: @needs_tzset @pytest.mark.parametrize("tz", ["Asia/Kolkata", "Europe/Paris", "America/Los_Angeles"]) - def test_a_non_utc_proxy_still_reports_utc(self, tz): - with a_proxy_running_in(tz): + def test_a_non_utc_proxy_still_reports_utc(self, tz, monkeypatch): + with a_proxy_running_in(tz, monkeypatch): limiter = _parallel_limiter() with pytest.raises(ProxyRateLimitError) as exc_info: limiter._handle_rate_limit_error( @@ -104,7 +96,7 @@ class TestParallelLimiterReportsUTC: class TestBatchLimiterReportsUTC: @needs_tzset @pytest.mark.parametrize("tz", ["Asia/Kolkata", "America/Los_Angeles"]) - def test_a_non_utc_proxy_still_reports_utc(self, tz): + def test_a_non_utc_proxy_still_reports_utc(self, tz, monkeypatch): parallel_request_limiter = MagicMock() parallel_request_limiter.window_size = WINDOW_SIZE limiter = _PROXY_BatchRateLimiter( @@ -112,7 +104,7 @@ class TestBatchLimiterReportsUTC: parallel_request_limiter=parallel_request_limiter, ) - with a_proxy_running_in(tz): + with a_proxy_running_in(tz, monkeypatch): before = datetime.now(timezone.utc) with pytest.raises(ProxyRateLimitError) as exc_info: limiter._raise_rate_limit_error(