Skip to content

Commit d038a73

Browse files
committed
improve typing/logic
1 parent b0aec1a commit d038a73

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

flagsmith/models.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,18 @@ class Flag(BaseFlag):
2929
def from_evaluation_result(
3030
cls,
3131
flag_result: SDKFlagResult,
32-
) -> typing.Optional[Flag]:
32+
) -> Flag:
3333
if metadata := flag_result.get("metadata"):
3434
return Flag(
3535
enabled=flag_result["enabled"],
3636
value=flag_result["value"],
3737
feature_name=flag_result["name"],
3838
feature_id=metadata["flagsmith_id"],
3939
)
40-
return None
40+
raise ValueError(
41+
"FlagResult metadata is missing. Cannot create Flag instance. "
42+
"This means a bug in the SDK, please report it."
43+
)
4144

4245
@classmethod
4346
def from_api_flag(cls, flag_data: typing.Mapping[str, typing.Any]) -> Flag:

tests/test_models.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,6 @@ def test_flag_from_evaluation_result() -> None:
8585
"value": 42,
8686
"metadata": {"flagsmith_id": 3},
8787
},
88-
"feature4": {
89-
"enabled": True,
90-
"feature_key": "4",
91-
"name": "feature4",
92-
"reason": "DEFAULT",
93-
"value": 42,
94-
},
9588
},
9689
["feature1", "feature2", "feature3"],
9790
),
@@ -151,3 +144,18 @@ def test_flag_from_evaluation_result_value_types(
151144
# Then
152145
assert flag
153146
assert flag.value == expected
147+
148+
149+
def test_flag_from_evaluation_result_missing_metadata__raises_expected() -> None:
150+
# Given
151+
flag_result: SDKFlagResult = {
152+
"enabled": True,
153+
"feature_key": "123",
154+
"name": "test_feature",
155+
"reason": "DEFAULT",
156+
"value": "test-value",
157+
}
158+
159+
# When & Then
160+
with pytest.raises(ValueError):
161+
Flag.from_evaluation_result(flag_result)

0 commit comments

Comments
 (0)