Skip to content

Commit 47c6730

Browse files
committed
fix: user.with_attr accepts any as value and auto convert to str
1 parent ae898f0 commit 47c6730

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

featureprobe/user.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class User:
2222
Usually corresponding to a user of your application.
2323
"""
2424

25-
def __init__(self, attrs: Dict[str, str] = None,
25+
def __init__(self, attrs: Dict[str, any] = None,
2626
stable_rollout_key: str = None):
2727
"""Creates a new FeatureProbe User.
2828
@@ -33,9 +33,9 @@ def __init__(self, attrs: Dict[str, str] = None,
3333
self._key = stable_rollout_key
3434
else:
3535
self._key = str(int(time.time() * 10**6))
36-
self._attrs = attrs or {}
36+
self._attrs = {k: str(v) for k, v in (attrs or {}).items()}
3737

38-
def __setitem__(self, key: str, value: str):
38+
def __setitem__(self, key: str, value: any):
3939
"""Alias for :func:`~featureprobe.User.with_attr`.
4040
4141
Usage::
@@ -45,7 +45,7 @@ def __setitem__(self, key: str, value: str):
4545
>>> user.with_attr('key1', 'value1')
4646
>>> user['key2'] = 'value2'
4747
"""
48-
self._attrs[key] = value
48+
self._attrs[key] = str(value)
4949

5050
def __getitem__(self, attr: str):
5151
"""Gets the value of specified attribute.
@@ -92,7 +92,7 @@ def attrs(self, attrs: Dict[str, str]):
9292
"""Sets (replace the original attributes) multiple attributes to a FeatureProbe User"""
9393
self._attrs = attrs
9494

95-
def with_attr(self, key: str, value: str) -> "User":
95+
def with_attr(self, key: str, value: any) -> "User":
9696
"""Adds an attribute to the user.
9797
9898
:param key: Attribute key / name.
@@ -104,7 +104,7 @@ def with_attr(self, key: str, value: str) -> "User":
104104
>>> import featureprobe as fp
105105
>>> user = fp.User('unique id').with_attr('key1', 'value1').with_attr('key2', 'value2')
106106
"""
107-
self._attrs[key] = value
107+
self._attrs[key] = str(value)
108108
return self
109109

110110
def has_attr(self, attr: str) -> bool:

tests/condition_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_string_is_one_of():
4242
condition.objects = ['12345', '987654', '665544', '13797347245']
4343
condition.predicate = fp.model.StringPredicate.IS_ONE_OF
4444

45-
user['userId'] = '12345'
45+
user['userId'] = 12345 # should auto convert to string
4646
assert condition.match_objects(user, segments)
4747

4848
user['userId'] = '999999'

0 commit comments

Comments
 (0)