fb-python-sdk 1.1.10__tar.gz → 1.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. {fb_python_sdk-1.1.10/fb_python_sdk.egg-info → fb_python_sdk-1.2.0}/PKG-INFO +1 -1
  2. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0/fb_python_sdk.egg-info}/PKG-INFO +1 -1
  3. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fb_python_sdk.egg-info/SOURCES.txt +1 -0
  4. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/client.py +4 -2
  5. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/common_types.py +2 -2
  6. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/evaluator.py +3 -1
  7. fb_python_sdk-1.2.0/release/package.json +3 -0
  8. fb_python_sdk-1.2.0/tests/test_insights_enabled.py +161 -0
  9. fb_python_sdk-1.1.10/release/package.json +0 -3
  10. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/LICENSE +0 -0
  11. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/MANIFEST.in +0 -0
  12. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/README.md +0 -0
  13. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/dev-requirements.txt +0 -0
  14. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fb_python_sdk.egg-info/dependency_links.txt +0 -0
  15. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fb_python_sdk.egg-info/requires.txt +0 -0
  16. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fb_python_sdk.egg-info/top_level.txt +0 -0
  17. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/__init__.py +0 -0
  18. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/category.py +0 -0
  19. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/config.py +0 -0
  20. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/data_storage.py +0 -0
  21. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/event_processor.py +0 -0
  22. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/event_types.py +0 -0
  23. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/flag_change_notification.py +0 -0
  24. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/interfaces.py +0 -0
  25. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/notice_broadcaster.py +0 -0
  26. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/status.py +0 -0
  27. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/status_types.py +0 -0
  28. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/streaming.py +0 -0
  29. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/update_processor.py +0 -0
  30. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/utils/__init__.py +0 -0
  31. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/utils/exponential_backoff_jitter_strategy.py +0 -0
  32. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/utils/http_client.py +0 -0
  33. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/utils/repeatable_task.py +0 -0
  34. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/utils/rwlock.py +0 -0
  35. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/fbclient/utils/variation_splitting_algorithm.py +0 -0
  36. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/pyproject.toml +0 -0
  37. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/requirements.txt +0 -0
  38. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/setup.cfg +0 -0
  39. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/setup.py +0 -0
  40. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/tests/test_data_storage.py +0 -0
  41. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/tests/test_data_update_status_provider.py +0 -0
  42. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/tests/test_evaluator.py +0 -0
  43. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/tests/test_event_processor.py +0 -0
  44. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/tests/test_fbclient.py +0 -0
  45. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/tests/test_flag_change_notification.py +0 -0
  46. {fb_python_sdk-1.1.10 → fb_python_sdk-1.2.0}/tests/test_runtime_safety.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fb-python-sdk
3
- Version: 1.1.10
3
+ Version: 1.2.0
4
4
  Summary: A Python SDK for FeatBit plateform
5
5
  Home-page: https://github.com/featbit/featbit-python-sdk
6
6
  Author: Dian SUN
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fb-python-sdk
3
- Version: 1.1.10
3
+ Version: 1.2.0
4
4
  Summary: A Python SDK for FeatBit plateform
5
5
  Home-page: https://github.com/featbit/featbit-python-sdk
6
6
  Author: Dian SUN
@@ -39,4 +39,5 @@ tests/test_evaluator.py
39
39
  tests/test_event_processor.py
40
40
  tests/test_fbclient.py
41
41
  tests/test_flag_change_notification.py
42
+ tests/test_insights_enabled.py
42
43
  tests/test_runtime_safety.py
@@ -133,6 +133,8 @@ class FBClient:
133
133
 
134
134
  def _send_event_safely(self, event):
135
135
  try:
136
+ if isinstance(event, FlagEvent) and not event.is_send_event:
137
+ return
136
138
  if not self._closed:
137
139
  self._event_processor.send_event(event)
138
140
  except Exception:
@@ -273,7 +275,7 @@ class FBClient:
273
275
  def variation(self, key: str, user: dict, default: Any = None) -> Any:
274
276
  """Return the variation of a feature flag for a given user.
275
277
 
276
- This method will send an event back to feature flag center immediately if no error occurs.
278
+ This method collects an evaluation event if evaluation succeeds and insights are enabled for the flag.
277
279
 
278
280
  The default value should be a string, boolean, numeric, or json type.
279
281
 
@@ -297,7 +299,7 @@ class FBClient:
297
299
  """"Return the variation of a feature flag for a given user, but also provides additional information
298
300
  about how this value was calculated, in the property `data` of the :class:`fbclient.common_types.EvalDetail`.
299
301
 
300
- This method will send an event back to feature flag center immediately if no error occurs.
302
+ This method collects an evaluation event if evaluation succeeds and insights are enabled for the flag.
301
303
 
302
304
  The default value should be a string, boolean, numeric, or json type.
303
305
 
@@ -232,7 +232,7 @@ class AllFlagStates(BasicFlagState, Jsonfy):
232
232
  def get_variation(self, key_name: str, default: Any = None) -> Any:
233
233
  """Return the variation of a given feature flag key
234
234
 
235
- This method will send event to back to feature flag center immediately
235
+ This method submits the evaluation event only if insights were enabled when the flag was evaluated.
236
236
 
237
237
  The default value should be a string, boolean, numeric, or json type.
238
238
 
@@ -244,7 +244,7 @@ class AllFlagStates(BasicFlagState, Jsonfy):
244
244
  def get(self, key_name: str, default: Any = None) -> EvalDetail:
245
245
  """Return the flag evaluation details of a given feature flag key
246
246
 
247
- This method will send event to back to feature flag center immediately
247
+ This method submits the evaluation event only if insights were enabled when the flag was evaluated.
248
248
 
249
249
  The default value should be a string, boolean, numeric, or json type.
250
250
 
@@ -95,7 +95,9 @@ class Evaluator:
95
95
  finally:
96
96
  if er is not None:
97
97
  log.info('FB Python SDK: User %s, Feature Flag %s, Flag Value %s' % (user.get('KeyId'), er.key_name, er.value))
98
- if fb_event is not None:
98
+ # Match the server default: only an explicit boolean false
99
+ # disables insights; evaluation and experiment eligibility stay unchanged.
100
+ if fb_event is not None and flag.get('insightsEnabled') is not False:
99
101
  fb_event.add(FlagEventVariation(er))
100
102
 
101
103
  # return a value when flag is off
@@ -0,0 +1,3 @@
1
+ {
2
+ "version":"1.2.0"
3
+ }
@@ -0,0 +1,161 @@
1
+ import base64
2
+ import json
3
+ from pathlib import Path
4
+ from threading import Event
5
+ from unittest.mock import Mock
6
+
7
+ import pytest
8
+
9
+ from fbclient.client import FBClient
10
+ from fbclient.common_types import FBUser
11
+ from fbclient.config import Config
12
+ from fbclient.event_processor import DefaultEventProcessor, NullEventProcessor
13
+ from fbclient.event_types import FlagEvent, MetricEvent, UserEvent
14
+ from fbclient.streaming import Streaming
15
+ from fbclient.update_processor import NullUpdateProcessor
16
+
17
+
18
+ MISSING = object()
19
+ USER = {'key': 'test-user-1', 'name': 'Test User', 'country': 'us'}
20
+ FLAG_KEY = 'ff-test-bool'
21
+ SETTINGS = [MISSING, True, False, None, 0, 'false']
22
+
23
+
24
+ def payload(setting, event_type='full', revision=1):
25
+ message = json.loads(Path('tests/fbclient_test_data.json').read_text())
26
+ message['data']['eventType'] = event_type
27
+ for flag in message['data']['featureFlags']:
28
+ flag.pop('insightsEnabled', None)
29
+ if setting is not MISSING:
30
+ flag['insightsEnabled'] = setting
31
+ flag['updatedAt'] = '2030-01-01T00:00:%02dZ' % revision
32
+ return json.dumps(message)
33
+
34
+
35
+ @pytest.fixture
36
+ def client():
37
+ processor = Mock()
38
+ config = Config(base64.b64encode(b'insights-test').decode(),
39
+ event_url='http://fake', streaming_url='http://fake',
40
+ update_processor_imp=NullUpdateProcessor,
41
+ event_processor_imp=lambda *_: processor)
42
+ with FBClient(config) as instance:
43
+ yield instance
44
+
45
+
46
+ def sync(client, message):
47
+ # Exercise the real WebSocket message parser and storage update without
48
+ # starting a network connection or a ping thread.
49
+ streaming = Streaming(Config('unused', 'http://fake', 'http://fake', offline=True), client._broadcaster,
50
+ client._update_status_provider, Event())
51
+ streaming._on_message(None, message)
52
+
53
+
54
+ @pytest.mark.parametrize('setting', SETTINGS)
55
+ @pytest.mark.parametrize('entry', ['variation', 'variation_detail', 'get', 'get_variation'])
56
+ def test_synced_insights_setting_controls_all_evaluation_entries(client, setting, entry):
57
+ sync(client, payload(setting))
58
+ flag = client._get_flag_internal(FLAG_KEY)
59
+ if setting is MISSING:
60
+ assert 'insightsEnabled' not in flag
61
+ else:
62
+ assert flag['insightsEnabled'] == setting
63
+ assert type(flag['insightsEnabled']) is type(setting)
64
+
65
+ if entry in ('get', 'get_variation'):
66
+ states = client.get_all_latest_flag_variations(USER)
67
+ client._event_processor.send_event.assert_not_called()
68
+ result = getattr(states, entry)(FLAG_KEY, False)
69
+ else:
70
+ result = getattr(client, entry)(FLAG_KEY, USER, False)
71
+ assert (result.variation if entry in ('get', 'variation_detail') else result) is True
72
+
73
+ calls = client._event_processor.send_event.call_args_list
74
+ assert len(calls) == (0 if setting is False else 1)
75
+ if calls:
76
+ variation = calls[0][0][0].to_json_dict()['variations'][0]['variation']
77
+ assert variation['id'] == flag['fallthrough']['variations'][0]['id']
78
+ assert variation['value'] == 'true'
79
+
80
+
81
+ @pytest.mark.parametrize('setting', SETTINGS)
82
+ def test_external_json_preserves_insights_setting(setting):
83
+ with FBClient(Config('unused', 'http://fake', 'http://fake', offline=True)) as client:
84
+ assert client.initialize_from_external_json(payload(setting))
85
+ flag = client._get_flag_internal(FLAG_KEY)
86
+ assert flag.get('insightsEnabled', MISSING) == setting
87
+ event = FlagEvent(FBUser.from_dict(USER))
88
+ client._evaluator.evaluate(flag, FBUser.from_dict(USER), event)
89
+ assert bool(event.is_send_event) == (setting is not False)
90
+ # Enabling insights does not replace offline event suppression.
91
+ client.variation(FLAG_KEY, USER, False)
92
+ assert isinstance(client._event_processor, NullEventProcessor)
93
+
94
+
95
+ @pytest.mark.parametrize('key,user', [
96
+ ('ff-test-off', USER),
97
+ ('ff-evaluation-test', {'key': 'test-target-user'}),
98
+ ('ff-evaluation-test', {'key': 'rule-user', 'graduated': 'true'}),
99
+ ('ff-evaluation-test', {'key': 'fallthrough-user'}),
100
+ ])
101
+ def test_insights_does_not_change_evaluation_or_experiment_eligibility(client, key, user):
102
+ sync(client, payload(True))
103
+ flag = client._get_flag_internal(key)
104
+ user = FBUser.from_dict(dict(user, name='Test User'))
105
+ enabled_event = FlagEvent(user)
106
+ before = client._evaluator.evaluate(flag, user, enabled_event)
107
+ flag = dict(flag, insightsEnabled=False)
108
+ disabled_event = FlagEvent(user)
109
+ after = client._evaluator.evaluate(flag, user, disabled_event)
110
+ assert (after.id, after.value, after.reason, after.is_send_to_expt) == (
111
+ before.id, before.value, before.reason, before.is_send_to_expt)
112
+ assert enabled_event.is_send_event
113
+ assert not disabled_event.is_send_event
114
+
115
+
116
+ def test_patch_updates_future_evaluations_but_preserves_snapshot_eligibility(client):
117
+ sync(client, payload(True))
118
+ enabled_snapshot = client.get_all_latest_flag_variations(USER)
119
+ sync(client, payload(False, 'patch', 2))
120
+ assert client._get_flag_internal(FLAG_KEY)['insightsEnabled'] is False
121
+ disabled_snapshot = client.get_all_latest_flag_variations(USER)
122
+ assert client.variation(FLAG_KEY, USER) is True
123
+ client._event_processor.send_event.assert_not_called()
124
+ enabled_snapshot.get(FLAG_KEY)
125
+ assert client._event_processor.send_event.call_count == 1
126
+
127
+ sync(client, payload(True, 'patch', 3))
128
+ assert client._get_flag_internal(FLAG_KEY)['insightsEnabled'] is True
129
+ disabled_snapshot.get_variation(FLAG_KEY)
130
+ assert client._event_processor.send_event.call_count == 1
131
+ assert client.variation(FLAG_KEY, USER) is True
132
+ assert client._event_processor.send_event.call_count == 2
133
+
134
+
135
+ def test_metrics_and_identify_are_unaffected_and_closed_client_stays_silent(client):
136
+ sync(client, payload(False))
137
+ client.track_metric(USER, 'purchase')
138
+ client.identify(USER)
139
+ events = [call[0][0] for call in client._event_processor.send_event.call_args_list]
140
+ assert [type(event) for event in events] == [MetricEvent, UserEvent]
141
+ sync(client, payload(True, 'patch', 2))
142
+ client.stop()
143
+ client.variation(FLAG_KEY, USER)
144
+ assert client._event_processor.send_event.call_count == 2
145
+
146
+
147
+ def test_default_processor_only_delivers_enabled_evaluations(client):
148
+ sender = Mock()
149
+ processor = DefaultEventProcessor(client._config, sender)
150
+ client._event_processor = processor
151
+ sync(client, payload(True))
152
+ client.variation(FLAG_KEY, USER)
153
+ sync(client, payload(False, 'patch', 2))
154
+ client.variation(FLAG_KEY, USER)
155
+ client.get_all_latest_flag_variations(USER).get(FLAG_KEY)
156
+ # Shutdown drains accepted events, including the one collected before disable.
157
+ client.stop()
158
+ delivered = [event for call in sender.postJson.call_args_list
159
+ for event in json.loads(call[0][1])]
160
+ assert len(delivered) == 1
161
+ assert delivered[0]['variations'][0]['featureFlagKey'] == FLAG_KEY
@@ -1,3 +0,0 @@
1
- {
2
- "version":"1.1.10"
3
- }
File without changes
File without changes
File without changes
File without changes