Skip to content

Commit b4b92dc

Browse files
committed
Fix up type annotation
1 parent 0c4746f commit b4b92dc

File tree

3 files changed

+15
-42
lines changed

3 files changed

+15
-42
lines changed

posthog/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from posthog.client import Client
66
from posthog.exception_capture import Integrations # noqa: F401
77
from posthog.version import VERSION
8-
from posthog.types import FlagsAndPayloads
8+
from posthog.types import FlagsAndPayloads, FeatureFlag
99

1010
__version__ = VERSION
1111

@@ -404,7 +404,7 @@ def get_feature_flag(
404404
only_evaluate_locally=False, # type: bool
405405
send_feature_flag_events=True, # type: bool
406406
disable_geoip=None, # type: Optional[bool]
407-
) -> str | bool | None:
407+
) -> FeatureFlag | None:
408408
"""
409409
Get feature flag variant for users. Used with experiments.
410410
Example:
@@ -447,7 +447,7 @@ def get_all_flags(
447447
group_properties={}, # type: dict
448448
only_evaluate_locally=False, # type: bool
449449
disable_geoip=None, # type: Optional[bool]
450-
) -> dict[str, str | bool] | None:
450+
) -> dict[str, FeatureFlag] | None:
451451
"""
452452
Get all flags for a given user.
453453
Example:
@@ -478,7 +478,7 @@ def get_feature_flag_payload(
478478
only_evaluate_locally=False,
479479
send_feature_flag_events=True,
480480
disable_geoip=None, # type: Optional[bool]
481-
) -> str:
481+
) -> str | None:
482482
return _proxy(
483483
"get_feature_flag_payload",
484484
key=key,

posthog/test/test_feature_flag.py

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,14 @@ def test_feature_flag_from_json_without_metadata(self):
6565

6666
def test_flag_reason_from_json(self):
6767
# Test with complete data
68-
resp = {
69-
"code": "user_in_segment",
70-
"condition_index": 1,
71-
"description": "User is in segment 'beta_users'"
72-
}
68+
resp = {"code": "user_in_segment", "condition_index": 1, "description": "User is in segment 'beta_users'"}
7369
reason = FlagReason.from_json(resp)
7470
self.assertEqual(reason.code, "user_in_segment")
7571
self.assertEqual(reason.condition_index, 1)
7672
self.assertEqual(reason.description, "User is in segment 'beta_users'")
7773

7874
# Test with partial data
79-
resp = {
80-
"code": "user_in_segment"
81-
}
75+
resp = {"code": "user_in_segment"}
8276
reason = FlagReason.from_json(resp)
8377
self.assertEqual(reason.code, "user_in_segment")
8478
self.assertEqual(reason.condition_index, 0) # default value
@@ -89,22 +83,15 @@ def test_flag_reason_from_json(self):
8983

9084
def test_flag_metadata_from_json(self):
9185
# Test with complete data
92-
resp = {
93-
"id": 123,
94-
"payload": {"key": "value"},
95-
"version": 1,
96-
"description": "Test flag"
97-
}
86+
resp = {"id": 123, "payload": {"key": "value"}, "version": 1, "description": "Test flag"}
9887
metadata = FlagMetadata.from_json(resp)
9988
self.assertEqual(metadata.id, 123)
10089
self.assertEqual(metadata.payload, {"key": "value"})
10190
self.assertEqual(metadata.version, 1)
10291
self.assertEqual(metadata.description, "Test flag")
10392

10493
# Test with partial data
105-
resp = {
106-
"id": 123
107-
}
94+
resp = {"id": 123}
10895
metadata = FlagMetadata.from_json(resp)
10996
self.assertEqual(metadata.id, 123)
11097
self.assertIsNone(metadata.payload)
@@ -123,14 +110,9 @@ def test_feature_flag_from_json_complete(self):
123110
"reason": {
124111
"code": "user_in_segment",
125112
"condition_index": 1,
126-
"description": "User is in segment 'beta_users'"
113+
"description": "User is in segment 'beta_users'",
127114
},
128-
"metadata": {
129-
"id": 123,
130-
"payload": {"key": "value"},
131-
"version": 1,
132-
"description": "Test flag"
133-
}
115+
"metadata": {"id": 123, "payload": {"key": "value"}, "version": 1, "description": "Test flag"},
134116
}
135117
flag = FeatureFlag.from_json(resp)
136118
self.assertEqual(flag.key, "test-flag")
@@ -144,10 +126,7 @@ def test_feature_flag_from_json_complete(self):
144126

145127
def test_feature_flag_from_json_minimal_data(self):
146128
# Test with minimal data
147-
resp = {
148-
"key": "test-flag",
149-
"enabled": False
150-
}
129+
resp = {"key": "test-flag", "enabled": False}
151130
flag = FeatureFlag.from_json(resp)
152131
self.assertEqual(flag.key, "test-flag")
153132
self.assertFalse(flag.enabled)
@@ -158,13 +137,7 @@ def test_feature_flag_from_json_minimal_data(self):
158137

159138
def test_feature_flag_from_json_with_reason(self):
160139
# Test with reason but no metadata
161-
resp = {
162-
"key": "test-flag",
163-
"enabled": True,
164-
"reason": {
165-
"code": "user_in_segment"
166-
}
167-
}
140+
resp = {"key": "test-flag", "enabled": True, "reason": {"code": "user_in_segment"}}
168141
flag = FeatureFlag.from_json(resp)
169142
self.assertEqual(flag.key, "test-flag")
170143
self.assertTrue(flag.enabled)

posthog/types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class FlagReason:
2020
description: str
2121

2222
@classmethod
23-
def from_json(cls, resp: Any) -> "FlagReason":
23+
def from_json(cls, resp: Any) -> "FlagReason" | None:
2424
if not resp:
2525
return None
2626
return cls(
@@ -33,12 +33,12 @@ def from_json(cls, resp: Any) -> "FlagReason":
3333
@dataclass(frozen=True)
3434
class FlagMetadata:
3535
id: int
36-
payload: Any
36+
payload: str | None
3737
version: int
3838
description: str
3939

4040
@classmethod
41-
def from_json(cls, resp: Any) -> "FlagMetadata":
41+
def from_json(cls, resp: Any) -> "FlagMetadata" | None:
4242
if not resp:
4343
return None
4444
return cls(

0 commit comments

Comments
 (0)