Skip to content

Commit 7a8c835

Browse files
authored
fix: Log warning if targeting key is not a string (#16)
1 parent dd17a4c commit 7a8c835

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

ld_openfeature/impl/context_converter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __get_targeting_key(self, targeting_key: Optional[str], key: Any) -> str:
4949
if key is not None and isinstance(key, str):
5050
targeting_key = targeting_key if targeting_key else key
5151

52-
if targeting_key is None or targeting_key == "":
52+
if targeting_key is None or targeting_key == "" or not isinstance(targeting_key, str):
5353
logger.error("The EvaluationContext must contain either a 'targetingKey' or a 'key' and the type must be a string.")
5454

5555
return targeting_key if targeting_key else ""

tests/impl/test_context_converter.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ def test_create_context_with_invalid_key(context_converter: EvaluationContextCon
2828
assert caplog.records[0].message == "A non-string 'key' attribute was provided."
2929

3030

31+
def test_create_context_with_invalid_targeting_key(context_converter: EvaluationContextConverter, caplog):
32+
context = EvaluationContext(False) # type: ignore[arg-type]
33+
ld_context = context_converter.to_ld_context(context)
34+
35+
assert ld_context.valid is False
36+
assert ld_context.key == ''
37+
38+
assert caplog.records[0].message == "The EvaluationContext must contain either a 'targetingKey' or a 'key' and the type must be a string."
39+
40+
3141
def test_invalid_private_attribute_types_are_ignored(context_converter: EvaluationContextConverter, caplog):
3242
context = EvaluationContext("user-key", {"privateAttributes": [True]})
3343
ld_context = context_converter.to_ld_context(context)

0 commit comments

Comments
 (0)