-
Notifications
You must be signed in to change notification settings - Fork 2
Python UFC SDK updates #29
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,12 +74,15 @@ def get_numeric_assignment( | |
| default: float, | ||
| subject_attributes: Optional[SubjectAttributes] = None, | ||
| ) -> float: | ||
| return self.get_assignment_variation( | ||
| subject_key, | ||
| flag_key, | ||
| default, | ||
| subject_attributes, | ||
| VariationType.NUMERIC, | ||
| # convert to float in case we get an int | ||
| return float( | ||
| self.get_assignment_variation( | ||
| subject_key, | ||
| flag_key, | ||
| default, | ||
| subject_attributes, | ||
| VariationType.NUMERIC, | ||
| ) | ||
| ) | ||
|
|
||
| def get_boolean_assignment( | ||
|
|
@@ -182,6 +185,15 @@ def get_assignment_detail( | |
|
|
||
| result = self.__evaluator.evaluate_flag(flag, subject_key, subject_attributes) | ||
|
|
||
| if result.variation and not check_value_type_match( | ||
| expected_variation_type, result.variation.value | ||
| ): | ||
| logger.error( | ||
| "[Eppo SDK] Variation value does not have the correct type for the flag: " | ||
| f"{flag_key} and variation key {result.variation.key}" | ||
| ) | ||
| return None | ||
|
|
||
| assignment_event = { | ||
| **(result.extra_logging if result else {}), | ||
| "allocation": result.allocation_key if result else None, | ||
|
|
@@ -227,3 +239,20 @@ def check_type_match( | |
| expected_type: Optional[VariationType], actual_type: VariationType | ||
| ): | ||
| return expected_type is None or actual_type == expected_type | ||
|
|
||
|
|
||
| def check_value_type_match( | ||
| expected_type: Optional[VariationType], value: ValueType | ||
| ) -> bool: | ||
| if expected_type is None: | ||
| return True | ||
| if expected_type in [VariationType.JSON, VariationType.STRING]: | ||
| return isinstance(value, str) | ||
| if expected_type == VariationType.INTEGER: | ||
| return isinstance(value, int) | ||
| if expected_type == VariationType.NUMERIC: | ||
| # we can convert int to float | ||
| return isinstance(value, float) or isinstance(value, int) | ||
|
Comment on lines
+254
to
+255
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get it now. The cast to |
||
| if expected_type == VariationType.BOOLEAN: | ||
| return isinstance(value, bool) | ||
| return False | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ class OperatorType(Enum): | |
| LT = "LT" | ||
| ONE_OF = "ONE_OF" | ||
| NOT_ONE_OF = "NOT_ONE_OF" | ||
| IS_NULL = "IS_NULL" | ||
|
|
||
|
|
||
| class Condition(SdkBaseModel): | ||
|
|
@@ -40,6 +41,11 @@ def evaluate_condition( | |
| condition: Condition, subject_attributes: SubjectAttributes | ||
| ) -> bool: | ||
| subject_value = subject_attributes.get(condition.attribute, None) | ||
| if condition.operator == OperatorType.IS_NULL: | ||
| if condition.value: | ||
| return subject_value is None | ||
| return subject_value is not None | ||
|
Comment on lines
+45
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. supernit, optional: could write as |
||
|
|
||
| if subject_value is not None: | ||
| if condition.operator == OperatorType.MATCHES: | ||
| return isinstance(condition.value, str) and bool( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Took me a moment to understand – I first thought you were going to rescue a mismatch (
getNumericfor anIntegerflag) which I didn't like. But what you're doing is ensuring that3.0(a valid variation for a numeric flag) is indeed returned as a float and not as the integer3.