Skip to content

Commit

Permalink
Merge pull request #60 from statsig-io/relax-user-id-requirement
Browse files Browse the repository at this point in the history
relax userid requirement when customIDs is provided
  • Loading branch information
jkw-statsig authored Apr 21, 2022
2 parents 5dd0c0e + 8c7747e commit 7070671
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
9 changes: 5 additions & 4 deletions statsig/statsig_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -61,5 +64,3 @@ def _get_environment(self):
return {'tier': tier.value}

return None


8 changes: 7 additions & 1 deletion tests/test_bad_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7070671

Please sign in to comment.