Skip to content

Commit 597eab5

Browse files
author
Michael Johansen
committed
Merge from main.
Signed-off-by: Michael Johansen <[email protected]>
2 parents 3cbb00b + f11d190 commit 597eab5

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

src/nipanel/_convert.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858

5959
def to_any(python_value: object) -> any_pb2.Any:
6060
"""Convert a Python object to a protobuf Any."""
61+
best_matching_type = _get_best_matching_type(python_value)
62+
converter = _CONVERTER_FOR_PYTHON_TYPE[best_matching_type]
63+
return converter.to_protobuf_any(python_value)
64+
65+
66+
def _get_best_matching_type(python_value: object) -> str:
6167
underlying_parents = type(python_value).mro() # This covers enum.IntEnum and similar
6268

6369
container_type = None
@@ -90,9 +96,7 @@ def to_any(python_value: object) -> any_pb2.Any:
9096
f"Unsupported type: ({container_type}, {payload_type}) with parents {underlying_parents}. Supported types are: {_SUPPORTED_PYTHON_TYPES}"
9197
)
9298
_logger.debug(f"Best matching type for '{repr(python_value)}' resolved to {best_matching_type}")
93-
94-
converter = _CONVERTER_FOR_PYTHON_TYPE[best_matching_type]
95-
return converter.to_protobuf_any(python_value)
99+
return best_matching_type
96100

97101

98102
def from_any(protobuf_any: any_pb2.Any) -> object:

tests/unit/test_convert.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,51 @@
3333
]
3434

3535

36+
# ========================================================
37+
# _get_best_matching_type() tests
38+
# ========================================================
39+
@pytest.mark.parametrize(
40+
"python_object, expected_type_string",
41+
[
42+
(False, "bool"),
43+
(b"mystr", "bytes"),
44+
(456.2, "float"),
45+
(123, "int"),
46+
("mystr", "str"),
47+
([False, False], "Collection.bool"),
48+
([b"mystr", b"mystr"], "Collection.bytes"),
49+
([456.2, 1.0], "Collection.float"),
50+
([123, 456], "Collection.int"),
51+
(["mystr", "mystr"], "Collection.str"),
52+
((False, False), "Collection.bool"),
53+
((b"mystr", b"mystr"), "Collection.bytes"),
54+
((456.2, 1.0), "Collection.float"),
55+
((123, 456), "Collection.int"),
56+
(("mystr", "mystr"), "Collection.str"),
57+
((False, False), "Collection.bool"),
58+
((b"mystr", b"mystr"), "Collection.bytes"),
59+
((456.2, 1.0), "Collection.float"),
60+
((123, 456), "Collection.int"),
61+
(("mystr", "mystr"), "Collection.str"),
62+
(set([False, True]), "Collection.bool"),
63+
(set([b"mystr", b"mystr2"]), "Collection.bytes"),
64+
(set([456.2, 1.0]), "Collection.float"),
65+
(set([123, 456]), "Collection.int"),
66+
(set(["mystr", "mystr2"]), "Collection.str"),
67+
(frozenset([False, True]), "Collection.bool"),
68+
(frozenset([b"mystr", b"mystr2"]), "Collection.bytes"),
69+
(frozenset([456.2, 1.0]), "Collection.float"),
70+
(frozenset([123, 456]), "Collection.int"),
71+
(frozenset(["mystr", "mystr2"]), "Collection.str"),
72+
],
73+
)
74+
def test___various_python_objects___get_best_matching_type___returns_correct_type_string(
75+
python_object: object, expected_type_string: str
76+
) -> None:
77+
type_string = nipanel._convert._get_best_matching_type(python_object)
78+
assert type_string == expected_type_string
79+
80+
3681
# ========================================================
3782
# Built-in Types: Python to Protobuf
3883
# ========================================================

0 commit comments

Comments
 (0)