Skip to content

Commit b4d67bf

Browse files
committed
Refactor requests exception imports through request.py
Export RequestsTimeout and RequestsConnectionError from posthog/request.py to keep all requests library imports in one place and avoid mypy issues.
1 parent 4adb5b8 commit b4d67bf

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

posthog/client.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@
3737
DEFAULT_HOST,
3838
APIError,
3939
QuotaLimitError,
40+
RequestsConnectionError,
41+
RequestsTimeout,
4042
batch_post,
4143
determine_server_host,
4244
flags,
4345
get,
4446
remote_config,
4547
)
46-
import requests.exceptions
4748
from posthog.contexts import (
4849
_get_current_context,
4950
get_context_distinct_id,
@@ -1597,10 +1598,10 @@ def _get_feature_flag_result(
15971598
except QuotaLimitError as e:
15981599
self.log.exception(f"[FEATURE FLAGS] Quota limit exceeded: {e}")
15991600
feature_flag_error = "quota_limited"
1600-
except requests.exceptions.Timeout as e:
1601+
except RequestsTimeout as e:
16011602
self.log.exception(f"[FEATURE FLAGS] Request timed out: {e}")
16021603
feature_flag_error = "timeout"
1603-
except requests.exceptions.ConnectionError as e:
1604+
except RequestsConnectionError as e:
16041605
self.log.exception(f"[FEATURE FLAGS] Connection error: {e}")
16051606
feature_flag_error = "connection_error"
16061607
except APIError as e:

posthog/request.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,12 @@ class QuotaLimitError(APIError):
312312
pass
313313

314314

315+
# Re-export requests exceptions for use in client.py
316+
# This keeps all requests library imports centralized in this module
317+
RequestsTimeout = requests.exceptions.Timeout
318+
RequestsConnectionError = requests.exceptions.ConnectionError
319+
320+
315321
class DatetimeSerializer(json.JSONEncoder):
316322
def default(self, obj: Any):
317323
if isinstance(obj, (date, datetime)):

posthog/test/test_feature_flag_result.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,9 @@ def test_get_feature_flag_result_unknown_error(self, patch_capture, patch_flags)
559559
@mock.patch.object(Client, "capture")
560560
def test_get_feature_flag_result_timeout_error(self, patch_capture, patch_flags):
561561
"""Test that timeout errors are captured specifically."""
562-
import requests.exceptions
562+
from posthog.request import RequestsTimeout
563563

564-
patch_flags.side_effect = requests.exceptions.Timeout("Request timed out")
564+
patch_flags.side_effect = RequestsTimeout("Request timed out")
565565

566566
flag_result = self.client.get_feature_flag_result("my-flag", "some-distinct-id")
567567

@@ -584,11 +584,9 @@ def test_get_feature_flag_result_timeout_error(self, patch_capture, patch_flags)
584584
@mock.patch.object(Client, "capture")
585585
def test_get_feature_flag_result_connection_error(self, patch_capture, patch_flags):
586586
"""Test that connection errors are captured specifically."""
587-
import requests.exceptions
587+
from posthog.request import RequestsConnectionError
588588

589-
patch_flags.side_effect = requests.exceptions.ConnectionError(
590-
"Connection refused"
591-
)
589+
patch_flags.side_effect = RequestsConnectionError("Connection refused")
592590

593591
flag_result = self.client.get_feature_flag_result("my-flag", "some-distinct-id")
594592

0 commit comments

Comments
 (0)