Skip to content

Commit c01fd8f

Browse files
committed
fix: Adhere to strict typing
The defined types don't leave room for missing or optional keys. We'll use the types as they're defined.
1 parent c429241 commit c01fd8f

File tree

2 files changed

+3
-52
lines changed

2 files changed

+3
-52
lines changed

posthog/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,9 +1213,9 @@ def _update_flag_state(
12131213
self, data: FlagDefinitionCacheData, old_flags_by_key: Optional[dict] = None
12141214
) -> None:
12151215
"""Update internal flag state from cache data and invalidate evaluation cache if changed."""
1216-
self.feature_flags = data.get("flags") or []
1217-
self.group_type_mapping = data.get("group_type_mapping") or {}
1218-
self.cohorts = data.get("cohorts") or {}
1216+
self.feature_flags = data["flags"]
1217+
self.group_type_mapping = data["group_type_mapping"]
1218+
self.cohorts = data["cohorts"]
12191219

12201220
# Invalidate evaluation cache if flag definitions changed
12211221
if (

posthog/test/test_flag_definition_cache.py

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -368,55 +368,6 @@ def test_shutdown_error_is_logged_but_continues(self, mock_get):
368368
# Shutdown was called
369369
self.assertEqual(self.cache_provider.shutdown_call_count, 1)
370370

371-
@mock.patch("posthog.client.get")
372-
def test_handles_cache_data_missing_keys_gracefully(self, mock_get):
373-
"""When cache returns data missing optional keys, should handle gracefully."""
374-
self.cache_provider.should_fetch_return_value = False
375-
# Missing 'cohorts' key - should use .get() default
376-
self.cache_provider.stored_data = {
377-
"flags": [{"key": "test-flag", "active": True, "filters": {}}],
378-
"group_type_mapping": {"0": "company"},
379-
}
380-
381-
client = self._create_client_with_cache()
382-
client._load_feature_flags()
383-
384-
# Should not call API
385-
mock_get.assert_not_called()
386-
387-
# Flags should be loaded
388-
self.assertEqual(len(client.feature_flags), 1)
389-
self.assertEqual(client.feature_flags[0]["key"], "test-flag")
390-
391-
# Missing cohorts should default to empty dict
392-
self.assertEqual(client.cohorts, {})
393-
394-
client.join()
395-
396-
@mock.patch("posthog.client.get")
397-
def test_handles_cache_data_with_none_values(self, mock_get):
398-
"""When cache returns data with None values, should use fallback defaults."""
399-
self.cache_provider.should_fetch_return_value = False
400-
# All values are None - should use 'or []' / 'or {}' fallbacks
401-
self.cache_provider.stored_data = {
402-
"flags": None,
403-
"group_type_mapping": None,
404-
"cohorts": None,
405-
}
406-
407-
client = self._create_client_with_cache()
408-
client._load_feature_flags()
409-
410-
# Should not call API
411-
mock_get.assert_not_called()
412-
413-
# All fields should have safe defaults
414-
self.assertEqual(client.feature_flags, [])
415-
self.assertEqual(client.group_type_mapping, {})
416-
self.assertEqual(client.cohorts, {})
417-
418-
client.join()
419-
420371

421372
class TestShutdownLifecycle(TestFlagDefinitionCacheProvider):
422373
"""Tests for shutdown lifecycle."""

0 commit comments

Comments
 (0)