diff --git a/statsig/statsig_user.py b/statsig/statsig_user.py index eee46f3..175b415 100644 --- a/statsig/statsig_user.py +++ b/statsig/statsig_user.py @@ -2,9 +2,11 @@ from statsig import statsig_environment_tier + def _str_or_none(field): return str(field) if field is not None else None + @dataclass class StatsigUser: """An object of properties relating to the current user @@ -26,9 +28,10 @@ class StatsigUser: _statsig_environment: dict = None def __post_init__(self): - if self.user_id is None or self.user_id == "": + # ensure there is a user id or at least a custom ID, empty dict evaluates to false in python so we can use "not" operator to check + if (self.user_id is None or self.user_id == "") and (self.custom_ids is None or not self.custom_ids): raise ValueError( - 'user_id is required: learn more https://docs.statsig.com/messages/serverRequiredUserID') + 'user_id or at least a custom ID is required: learn more https://docs.statsig.com/messages/serverRequiredUserID') def to_dict(self, forEvaluation=False): user_nullable = { @@ -61,5 +64,3 @@ def _get_environment(self): return {'tier': tier.value} return None - - \ No newline at end of file diff --git a/tests/test_bad_input.py b/tests/test_bad_input.py index cf1308e..e7ab1fc 100644 --- a/tests/test_bad_input.py +++ b/tests/test_bad_input.py @@ -20,14 +20,20 @@ def test_bad_key(self): def test_no_user_id(self): with self.assertRaises(ValueError) as context: StatsigUser("") - self.assertTrue('user_id' in str(context.exception)) with self.assertRaises(ValueError) as context: StatsigUser(None) + self.assertTrue('user_id' in str(context.exception)) + with self.assertRaises(ValueError) as context: + StatsigUser(None, custom_ids=dict()) self.assertTrue('user_id' in str(context.exception)) + user = StatsigUser(None, custom_ids=dict(stableID='123')) + self.assertFalse(user.user_id) + self.assertTrue(user.custom_ids) + def test_invalid_tier(self): with self.assertRaises(ValueError) as context: StatsigOptions(tier=123)