Skip to content

Commit 7ec5b6a

Browse files
committed
Fix up type annotations, tests, and formatting
1 parent 132851c commit 7ec5b6a

File tree

10 files changed

+247
-213
lines changed

10 files changed

+247
-213
lines changed

posthog/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
from posthog.client import Client
66
from posthog.exception_capture import Integrations # noqa: F401
7+
from posthog.types import FeatureFlag, FlagsAndPayloads
78
from posthog.version import VERSION
8-
from posthog.types import FlagsAndPayloads
9+
910
__version__ = VERSION
1011

1112
"""Settings."""
@@ -403,7 +404,7 @@ def get_feature_flag(
403404
only_evaluate_locally=False, # type: bool
404405
send_feature_flag_events=True, # type: bool
405406
disable_geoip=None, # type: Optional[bool]
406-
) -> str | bool | None:
407+
) -> Optional[FeatureFlag]:
407408
"""
408409
Get feature flag variant for users. Used with experiments.
409410
Example:
@@ -446,7 +447,7 @@ def get_all_flags(
446447
group_properties={}, # type: dict
447448
only_evaluate_locally=False, # type: bool
448449
disable_geoip=None, # type: Optional[bool]
449-
) -> dict[str, str | bool] | None:
450+
) -> Optional[dict[str, FeatureFlag]]:
450451
"""
451452
Get all flags for a given user.
452453
Example:
@@ -477,7 +478,7 @@ def get_feature_flag_payload(
477478
only_evaluate_locally=False,
478479
send_feature_flag_events=True,
479480
disable_geoip=None, # type: Optional[bool]
480-
) -> str:
481+
) -> Optional[str]:
481482
return _proxy(
482483
"get_feature_flag_payload",
483484
key=key,

posthog/client.py

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import sys
77
import warnings
88
from datetime import datetime, timedelta
9-
from typing import Any
9+
from typing import Any, Optional, Union
1010
from uuid import UUID, uuid4
1111

1212
import distro # For Linux OS detection
@@ -18,28 +18,20 @@
1818
from posthog.exception_utils import exc_info_from_error, exceptions_from_error_tuple, handle_in_app
1919
from posthog.feature_flags import InconclusiveMatchError, match_feature_flag_properties
2020
from posthog.poller import Poller
21-
from posthog.request import (
22-
DEFAULT_HOST,
23-
APIError,
24-
batch_post,
25-
decide,
26-
determine_server_host,
27-
get,
28-
remote_config,
29-
DecideResponse,
30-
)
31-
from posthog.utils import SizeLimitedDict, clean, guess_timezone, remove_trailing_slash
32-
from posthog.version import VERSION
21+
from posthog.request import DEFAULT_HOST, APIError, batch_post, decide, determine_server_host, get, remote_config
3322
from posthog.types import (
34-
FlagsAndPayloads,
23+
DecideResponse,
3524
FeatureFlag,
36-
FlagValue,
3725
FlagMetadata,
38-
to_values,
39-
to_payloads,
40-
to_flags_and_payloads,
26+
FlagsAndPayloads,
27+
FlagValue,
4128
normalize_decide_response,
29+
to_flags_and_payloads,
30+
to_payloads,
31+
to_values,
4232
)
33+
from posthog.utils import SizeLimitedDict, clean, guess_timezone, remove_trailing_slash
34+
from posthog.version import VERSION
4335

4436
try:
4537
import queue
@@ -265,7 +257,7 @@ def identify(self, distinct_id=None, properties=None, context=None, timestamp=No
265257

266258
def get_feature_variants(
267259
self, distinct_id, groups=None, person_properties=None, group_properties=None, disable_geoip=None
268-
) -> dict[str, str | bool]:
260+
) -> dict[str, Union[bool, str]]:
269261
"""
270262
Get feature flag variants for a distinct_id by calling decide.
271263
"""
@@ -352,7 +344,7 @@ def capture(
352344
msg["properties"]["$groups"] = groups
353345

354346
extra_properties: dict[str, Any] = {}
355-
feature_variants: dict[str, bool | str] | None = {}
347+
feature_variants: Optional[dict[str, Union[bool, str]]] = {}
356348
if send_feature_flags:
357349
try:
358350
feature_variants = self.get_feature_variants(distinct_id, groups, disable_geoip=disable_geoip)
@@ -820,7 +812,7 @@ def get_feature_flag(
820812
only_evaluate_locally=False,
821813
send_feature_flag_events=True,
822814
disable_geoip=None,
823-
) -> FlagValue | None:
815+
) -> Optional[FlagValue]:
824816
"""
825817
Get a feature flag value for a key by evaluating locally or remotely
826818
depending on whether local evaluation is enabled and the flag can be
@@ -859,7 +851,7 @@ def get_feature_flag(
859851
self._capture_feature_flag_called(
860852
distinct_id,
861853
key,
862-
response,
854+
response or False,
863855
None,
864856
flag_was_locally_evaluated,
865857
groups,
@@ -877,7 +869,7 @@ def _locally_evaluate_flag(
877869
groups: dict[str, str],
878870
person_properties: dict[str, str],
879871
group_properties: dict[str, str],
880-
) -> FlagValue | None:
872+
) -> Optional[FlagValue]:
881873
if self.feature_flags is None and self.personal_api_key:
882874
self.load_feature_flags()
883875
response = None
@@ -949,7 +941,7 @@ def get_feature_flag_payload(
949941
self._capture_feature_flag_called(
950942
distinct_id,
951943
key,
952-
response,
944+
response or False,
953945
payload,
954946
flag_was_locally_evaluated,
955947
groups,
@@ -967,33 +959,33 @@ def _get_feature_flag_details_from_decide(
967959
groups: dict[str, str],
968960
person_properties: dict[str, str],
969961
group_properties: dict[str, str],
970-
disable_geoip: bool | None,
971-
) -> tuple[FeatureFlag | None, str]:
962+
disable_geoip: Optional[bool],
963+
) -> tuple[Optional[FeatureFlag], Optional[str]]:
972964
"""
973965
Calls /decide and returns the flag details and request id
974966
"""
975967
resp_data = self.get_decide(distinct_id, groups, person_properties, group_properties, disable_geoip)
976968
request_id = resp_data.get("requestId")
977969
flags = resp_data.get("flags")
978-
flag_details = flags.get(key)
970+
flag_details = flags.get(key) if flags else None
979971
return flag_details, request_id
980972

981973
def _capture_feature_flag_called(
982974
self,
983975
distinct_id: str,
984976
key: str,
985977
response: FlagValue,
986-
payload: str | None,
978+
payload: Optional[str],
987979
flag_was_locally_evaluated: bool,
988980
groups: dict[str, str],
989-
disable_geoip: bool | None,
990-
request_id: str | None,
991-
flag_details: FeatureFlag | None,
981+
disable_geoip: Optional[bool],
982+
request_id: Optional[str],
983+
flag_details: Optional[FeatureFlag],
992984
):
993985
feature_flag_reported_key = f"{key}_{str(response)}"
994986

995987
if feature_flag_reported_key not in self.distinct_ids_feature_flags_reported[distinct_id]:
996-
properties = {
988+
properties: dict[str, Any] = {
997989
"$feature_flag": key,
998990
"$feature_flag_response": response,
999991
"locally_evaluated": flag_was_locally_evaluated,
@@ -1043,7 +1035,7 @@ def get_remote_config_payload(self, key: str):
10431035
except Exception as e:
10441036
self.log.exception(f"[FEATURE FLAGS] Unable to get decrypted feature flag payload: {e}")
10451037

1046-
def _compute_payload_locally(self, key: str, match_value: FlagValue) -> str | None:
1038+
def _compute_payload_locally(self, key: str, match_value: FlagValue) -> Optional[str]:
10471039
payload = None
10481040

10491041
if self.feature_flags_by_key is None:
@@ -1068,7 +1060,7 @@ def get_all_flags(
10681060
group_properties={},
10691061
only_evaluate_locally=False,
10701062
disable_geoip=None,
1071-
) -> dict[str, bool | str] | None:
1063+
) -> Optional[dict[str, Union[bool, str]]]:
10721064
response = self.get_all_flags_and_payloads(
10731065
distinct_id,
10741066
groups=groups,

posthog/request.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
from datetime import date, datetime
44
from gzip import GzipFile
55
from io import BytesIO
6-
from typing import Any, Optional, Union, cast
6+
from typing import Any, Optional, Union
77

88
import requests
99
from dateutil.tz import tzutc
1010

11-
from posthog.types import DecideResponse, FeatureFlag, FlagValue
1211
from posthog.utils import remove_trailing_slash
1312
from posthog.version import VERSION
1413

posthog/test/ai/openai/test_openai.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def mock_openai_response_with_responses_api():
8383
usage=ResponseUsage(
8484
input_tokens=10,
8585
output_tokens=10,
86+
input_tokens_details={"prompt_tokens": 10, "cached_tokens": 0},
8687
output_tokens_details={"reasoning_tokens": 15},
8788
total_tokens=20,
8889
),

posthog/test/test_client.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
from posthog.client import Client
1111
from posthog.request import APIError
1212
from posthog.test.test_utils import FAKE_TEST_API_KEY
13-
from posthog.version import VERSION
1413
from posthog.types import FeatureFlag, LegacyFlagMetadata
14+
from posthog.version import VERSION
15+
1516

1617
class TestClient(unittest.TestCase):
1718
@classmethod
@@ -1213,11 +1214,11 @@ def test_mock_system_context(
12131214

12141215
assert context == expected_context
12151216

1216-
@mock.patch("posthog.client.decide")
1217+
@mock.patch("posthog.client.decide")
12171218
def test_get_decide_returns_normalized_decide_response(self, patch_decide):
12181219
patch_decide.return_value = {
12191220
"featureFlags": {"beta-feature": "random-variant", "alpha-feature": True, "off-feature": False},
1220-
"featureFlagPayloads": {"beta-feature": "{\"some\": \"data\"}"},
1221+
"featureFlagPayloads": {"beta-feature": '{"some": "data"}'},
12211222
"errorsWhileComputingFlags": False,
12221223
"requestId": "test-id",
12231224
}
@@ -1226,9 +1227,9 @@ def test_get_decide_returns_normalized_decide_response(self, patch_decide):
12261227
distinct_id = "test_distinct_id"
12271228
groups = {"test_group_type": "test_group_id"}
12281229
person_properties = {"test_property": "test_value"}
1229-
1230+
12301231
response = client.get_decide(distinct_id, groups, person_properties)
1231-
1232+
12321233
assert response == {
12331234
"flags": {
12341235
"beta-feature": FeatureFlag(
@@ -1237,8 +1238,8 @@ def test_get_decide_returns_normalized_decide_response(self, patch_decide):
12371238
variant="random-variant",
12381239
reason=None,
12391240
metadata=LegacyFlagMetadata(
1240-
payload="{\"some\": \"data\"}",
1241-
)
1241+
payload='{"some": "data"}',
1242+
),
12421243
),
12431244
"alpha-feature": FeatureFlag(
12441245
key="alpha-feature",
@@ -1247,7 +1248,7 @@ def test_get_decide_returns_normalized_decide_response(self, patch_decide):
12471248
reason=None,
12481249
metadata=LegacyFlagMetadata(
12491250
payload=None,
1250-
)
1251+
),
12511252
),
12521253
"off-feature": FeatureFlag(
12531254
key="off-feature",
@@ -1256,8 +1257,8 @@ def test_get_decide_returns_normalized_decide_response(self, patch_decide):
12561257
reason=None,
12571258
metadata=LegacyFlagMetadata(
12581259
payload=None,
1259-
)
1260-
)
1260+
),
1261+
),
12611262
},
12621263
"errorsWhileComputingFlags": False,
12631264
"requestId": "test-id",

0 commit comments

Comments
 (0)