From 0ba0d08acb1f02f57e656bac86d7d64d8c5c82ad Mon Sep 17 00:00:00 2001 From: Jiakan Wang Date: Wed, 20 Apr 2022 17:04:52 -0700 Subject: [PATCH 1/3] relax userid requirement when customIDs is provided --- statsig/statsig_user.py | 9 +++++---- tests/test_bad_input.py | 9 +++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/statsig/statsig_user.py b/statsig/statsig_user.py index eee46f3..9244253 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 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..cbbebf7 100644 --- a/tests/test_bad_input.py +++ b/tests/test_bad_input.py @@ -28,6 +28,15 @@ def test_no_user_id(self): 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) From 62ced42a519a53628fc16fbd26d9f52bdf4e925a Mon Sep 17 00:00:00 2001 From: Jiakan Wang Date: Wed, 20 Apr 2022 17:49:03 -0700 Subject: [PATCH 2/3] test --- statsig/statsig_user.py | 2 +- tests/test_bad_input.py | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/statsig/statsig_user.py b/statsig/statsig_user.py index 9244253..175b415 100644 --- a/statsig/statsig_user.py +++ b/statsig/statsig_user.py @@ -29,7 +29,7 @@ class StatsigUser: def __post_init__(self): # 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 not self.custom_ids: + 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 or at least a custom ID is required: learn more https://docs.statsig.com/messages/serverRequiredUserID') diff --git a/tests/test_bad_input.py b/tests/test_bad_input.py index cbbebf7..3cf9693 100644 --- a/tests/test_bad_input.py +++ b/tests/test_bad_input.py @@ -20,19 +20,17 @@ 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)) + u = StatsigUser(None, custom_ids=dict()) user = StatsigUser(None, custom_ids=dict(stableID='123')) self.assertFalse(user.user_id) self.assertTrue(user.custom_ids) From 8c7747e2ddd4058b280bd8e34ce2253893db8914 Mon Sep 17 00:00:00 2001 From: Jiakan Wang Date: Wed, 20 Apr 2022 17:50:31 -0700 Subject: [PATCH 3/3] fix test --- tests/test_bad_input.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_bad_input.py b/tests/test_bad_input.py index 3cf9693..e7ab1fc 100644 --- a/tests/test_bad_input.py +++ b/tests/test_bad_input.py @@ -30,7 +30,6 @@ def test_no_user_id(self): StatsigUser(None, custom_ids=dict()) self.assertTrue('user_id' in str(context.exception)) - u = StatsigUser(None, custom_ids=dict()) user = StatsigUser(None, custom_ids=dict(stableID='123')) self.assertFalse(user.user_id) self.assertTrue(user.custom_ids)