Skip to content

Commit

Permalink
fix: need to check ".get()" because attribute may not be a dict (#3267)
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerHYang committed May 22, 2024
1 parent d3d6ecf commit 3917fcc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/phoenix/trace/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ def get_attribute_value(
will return `1`. If the key is `"a.b"`, then the function will return
`{"c": 1}`.
"""
if not attributes:
if not (attributes and isinstance(attributes, dict)):
return None
sub_keys = key.split(separator)
for sub_key in sub_keys[:-1]:
attributes = attributes.get(sub_key)
if not attributes:
if not (attributes and isinstance(attributes, dict)):
return None
return attributes.get(sub_keys[-1])

Expand Down
34 changes: 33 additions & 1 deletion tests/trace/test_attributes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
from typing import Any, Mapping

import pytest
from phoenix.trace.attributes import unflatten
from phoenix.trace.attributes import get_attribute_value, unflatten


@pytest.mark.parametrize(
"mapping,key,expected",
[
({}, "a.b.c", None),
({"a": "b"}, "a", "b"),
({"a": "b"}, "a.b", None),
({"a": "b"}, "a.b.c", None),
({"a": {"b": "c", "d": "e"}}, "a", {"b": "c", "d": "e"}),
({"a": {"b": "c", "d": "e"}}, "a.b", "c"),
({"a": {"b": "c", "d": "e"}}, "a.b.c", None),
({"a": {"b": {"c": "d"}}}, "a", {"b": {"c": "d"}}),
({"a": {"b": {"c": "d"}}}, "a.b", {"c": "d"}),
({"a": {"b": {"c": "d"}}}, "a.b.c", "d"),
({"a": {"bb": {"c": "d"}}}, "a.b.c", None),
("{}", "a.b.c", None),
({"a": {"b": "c"}}, "", None),
({"a": {"b": "c"}}, ".", None),
({"a": {"b": "c"}}, "a.", None),
({"a": {"b": "c"}}, "..", None),
({"a": {"b": "c"}}, "a..", None),
],
)
def test_get_attribute_value(
mapping: Mapping[str, Any],
key: str,
expected: Any,
) -> None:
assert get_attribute_value(mapping, key) == expected


@pytest.mark.parametrize(
Expand Down

0 comments on commit 3917fcc

Please sign in to comment.