Skip to content

Commit f7e693a

Browse files
fix: resolve mypy type errors in client.py
- Add type annotation for feature_flags_by_key: Optional[dict[str, dict]] - Add type annotation for sse_response: Optional[Any] - Add type annotation for old_flags_copy: dict[str, dict] - Refactor _compute_payload_locally to avoid unreachable code - Add assertion after None check in _process_flag_update - Move json import inside sse_listener function scope - Remove unreachable local imports (threading was already at top) All mypy errors in client.py are now resolved. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 2cadaa8 commit f7e693a

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

posthog/client.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def __init__(
229229
self.gzip = gzip
230230
self.timeout = timeout
231231
self._feature_flags = None # private variable to store flags
232-
self.feature_flags_by_key = None
232+
self.feature_flags_by_key: Optional[dict[str, dict]] = None
233233
self.group_type_mapping: Optional[dict[str, str]] = None
234234
self.cohorts: Optional[dict[str, Any]] = None
235235
self.poll_interval = poll_interval
@@ -254,7 +254,7 @@ def __init__(
254254
self.realtime_flags = realtime_flags
255255
self.on_feature_flags_update = on_feature_flags_update
256256
self.sse_connection = None # type: Optional[Any]
257-
self.sse_response = None
257+
self.sse_response = None # type: Optional[Any]
258258
self.sse_connected = False
259259
self._sse_lock = threading.Lock()
260260
self._flags_lock = (
@@ -1265,7 +1265,9 @@ def _load_feature_flags(self):
12651265
"[FEATURE FLAGS] Using cached flag definitions from external cache"
12661266
)
12671267
with self._flags_lock:
1268-
old_flags_copy = self.feature_flags_by_key or {}
1268+
old_flags_copy: dict[str, dict] = (
1269+
self.feature_flags_by_key or {}
1270+
)
12691271
self._update_flag_state(
12701272
cached_data, old_flags_by_key=old_flags_copy
12711273
)
@@ -1943,25 +1945,24 @@ def get_remote_config_payload(self, key: str):
19431945
def _compute_payload_locally(
19441946
self, key: str, match_value: FlagValue
19451947
) -> Optional[str]:
1946-
payload = None
1947-
19481948
with self._flags_lock:
19491949
if self.feature_flags_by_key is None:
1950-
return payload
1950+
return None
19511951

19521952
flag_definition = self.feature_flags_by_key.get(key)
1953-
if flag_definition:
1954-
flag_filters = flag_definition.get("filters") or {}
1955-
flag_payloads = flag_filters.get("payloads") or {}
1956-
# For boolean flags, convert True to "true"
1957-
# For multivariate flags, use the variant string as-is
1958-
lookup_value = (
1959-
"true"
1960-
if isinstance(match_value, bool) and match_value
1961-
else str(match_value)
1962-
)
1963-
payload = flag_payloads.get(lookup_value, None)
1964-
return payload
1953+
if not flag_definition:
1954+
return None
1955+
1956+
flag_filters = flag_definition.get("filters") or {}
1957+
flag_payloads = flag_filters.get("payloads") or {}
1958+
# For boolean flags, convert True to "true"
1959+
# For multivariate flags, use the variant string as-is
1960+
lookup_value = (
1961+
"true"
1962+
if isinstance(match_value, bool) and match_value
1963+
else str(match_value)
1964+
)
1965+
return flag_payloads.get(lookup_value, None)
19651966

19661967
def get_all_flags(
19671968
self,
@@ -2265,9 +2266,6 @@ def _setup_sse_connection(self):
22652266
self.sse_connected = True
22662267

22672268
try:
2268-
import threading
2269-
import json
2270-
22712269
# Use requests with stream=True for SSE
22722270
import requests
22732271

@@ -2279,6 +2277,8 @@ def _setup_sse_connection(self):
22792277

22802278
def sse_listener():
22812279
"""Background thread to listen for SSE messages"""
2280+
import json
2281+
22822282
try:
22832283
response = requests.get(
22842284
url, headers=headers, stream=True, timeout=None
@@ -2419,6 +2419,8 @@ def _process_flag_update(self, flag_data):
24192419
self.feature_flags = []
24202420

24212421
# Update the lookup table
2422+
# mypy doesn't track that the setter ensures feature_flags_by_key is a dict
2423+
assert self.feature_flags_by_key is not None
24222424
self.feature_flags_by_key[flag_key] = flag_data
24232425

24242426
# Update or add to the array

0 commit comments

Comments
 (0)