Skip to content

Commit

Permalink
relax userid requirement when customIDs is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
jkw-statsig committed Apr 21, 2022
1 parent 5dd0c0e commit 0ba0d08
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 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 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


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

0 comments on commit 0ba0d08

Please sign in to comment.