Skip to content

Commit

Permalink
refactor: make Reason enum values instances of str
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Bond <federicobond@gmail.com>
  • Loading branch information
federicobond committed Jul 11, 2023
1 parent 695da28 commit bf8ebec
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 15 deletions.
Empty file.
11 changes: 11 additions & 0 deletions open_feature/_backports/strenum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
try:
from enum import StrEnum
except ImportError:
from enum import Enum

class StrEnum(str, Enum):
"""
Backport StrEnum for Python <3.11
"""

pass
8 changes: 0 additions & 8 deletions open_feature/flag_evaluation/flag_evaluation_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,3 @@ class FlagEvaluationDetails(typing.Generic[T]):
reason: typing.Optional[Reason] = None
error_code: typing.Optional[ErrorCode] = None
error_message: typing.Optional[str] = None

@property
def reason(self) -> str:
return self._reason.value

@reason.setter
def reason(self, reason: Reason):
self._reason = reason
4 changes: 2 additions & 2 deletions open_feature/flag_evaluation/reason.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from enum import Enum
from open_feature._backports.strenum import StrEnum


class Reason(Enum):
class Reason(StrEnum):
CACHED = "CACHED"
DEFAULT = "DEFAULT"
DISABLED = "DISABLED"
Expand Down
6 changes: 3 additions & 3 deletions tests/test_open_feature_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_should_raise_exception_when_invalid_flag_type_provided(no_op_provider_c
assert flag.value
assert flag.error_message == "Unknown flag type"
assert flag.error_code == ErrorCode.GENERAL
assert flag.reason == Reason.ERROR.value
assert flag.reason == Reason.ERROR


def test_should_handle_a_generic_exception_thrown_by_a_provider(no_op_provider_client):
Expand All @@ -111,7 +111,7 @@ def test_should_handle_a_generic_exception_thrown_by_a_provider(no_op_provider_c
assert flag_details is not None
assert flag_details.value
assert isinstance(flag_details.value, bool)
assert flag_details.reason == Reason.ERROR.value
assert flag_details.reason == Reason.ERROR
assert flag_details.error_message == "Generic exception raised"


Expand All @@ -133,7 +133,7 @@ def test_should_handle_an_open_feature_exception_thrown_by_a_provider(
assert flag_details is not None
assert flag_details.value
assert isinstance(flag_details.value, bool)
assert flag_details.reason == Reason.ERROR.value
assert flag_details.reason == Reason.ERROR
assert flag_details.error_message == "error_message"


Expand Down
4 changes: 2 additions & 2 deletions tests/test_open_feature_flag_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_evaulation_details_reason_should_be_a_string():
assert variant == flag_details.variant
assert error_code == flag_details.error_code
assert error_message == flag_details.error_message
assert reason.value == flag_details.reason
assert reason == flag_details.reason


def test_evaulation_details_reason_should_be_a_string_when_set():
Expand All @@ -52,4 +52,4 @@ def test_evaulation_details_reason_should_be_a_string_when_set():
flag_details.reason = Reason.STATIC

# Then
assert Reason.STATIC.value == flag_details.reason
assert Reason.STATIC == flag_details.reason

0 comments on commit bf8ebec

Please sign in to comment.