Skip to content

Commit 28a5e3a

Browse files
author
Michael Johansen
committed
Refactor python type inspection into a separate method.
Signed-off-by: Michael Johansen <[email protected]>
1 parent 58bd0c3 commit 28a5e3a

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
@@ -54,6 +54,12 @@
5454

5555
def to_any(python_value: object) -> any_pb2.Any:
5656
"""Convert a Python object to a protobuf Any."""
57+
best_matching_type = _get_best_matching_type(python_value)
58+
converter = _CONVERTER_FOR_PYTHON_TYPE[best_matching_type]
59+
return converter.to_protobuf_any(python_value)
60+
61+
62+
def _get_best_matching_type(python_value: object) -> str:
5763
underlying_parents = type(python_value).mro() # This covers enum.IntEnum and similar
5864

5965
container_type = None
@@ -86,9 +92,7 @@ def to_any(python_value: object) -> any_pb2.Any:
8692
f"Unsupported type: ({container_type}, {payload_type}) with parents {underlying_parents}. Supported types are: {_SUPPORTED_PYTHON_TYPES}"
8793
)
8894
_logger.debug(f"Best matching type for '{repr(python_value)}' resolved to {best_matching_type}")
89-
90-
converter = _CONVERTER_FOR_PYTHON_TYPE[best_matching_type]
91-
return converter.to_protobuf_any(python_value)
95+
return best_matching_type
9296

9397

9498
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
@@ -70,6 +70,51 @@ def test___python_panel_collection___to_any___valid_paneltype_value(
7070
assert unpack_dest.values == expected_value
7171

7272

73+
# ========================================================
74+
# _get_best_matching_type() tests
75+
# ========================================================
76+
@pytest.mark.parametrize(
77+
"python_object, expected_type_string",
78+
[
79+
(False, "bool"),
80+
(b"mystr", "bytes"),
81+
(456.2, "float"),
82+
(123, "int"),
83+
("mystr", "str"),
84+
([False, False], "Collection.bool"),
85+
([b"mystr", b"mystr"], "Collection.bytes"),
86+
([456.2, 1.0], "Collection.float"),
87+
([123, 456], "Collection.int"),
88+
(["mystr", "mystr"], "Collection.str"),
89+
((False, False), "Collection.bool"),
90+
((b"mystr", b"mystr"), "Collection.bytes"),
91+
((456.2, 1.0), "Collection.float"),
92+
((123, 456), "Collection.int"),
93+
(("mystr", "mystr"), "Collection.str"),
94+
((False, False), "Collection.bool"),
95+
((b"mystr", b"mystr"), "Collection.bytes"),
96+
((456.2, 1.0), "Collection.float"),
97+
((123, 456), "Collection.int"),
98+
(("mystr", "mystr"), "Collection.str"),
99+
(set([False, True]), "Collection.bool"),
100+
(set([b"mystr", b"mystr2"]), "Collection.bytes"),
101+
(set([456.2, 1.0]), "Collection.float"),
102+
(set([123, 456]), "Collection.int"),
103+
(set(["mystr", "mystr2"]), "Collection.str"),
104+
(frozenset([False, True]), "Collection.bool"),
105+
(frozenset([b"mystr", b"mystr2"]), "Collection.bytes"),
106+
(frozenset([456.2, 1.0]), "Collection.float"),
107+
(frozenset([123, 456]), "Collection.int"),
108+
(frozenset(["mystr", "mystr2"]), "Collection.str"),
109+
],
110+
)
111+
def test___various_python_objects___get_best_matching_type___returns_correct_type_string(
112+
python_object: object, expected_type_string: str
113+
) -> None:
114+
type_string = nipanel._convert._get_best_matching_type(python_object)
115+
assert type_string == expected_type_string
116+
117+
73118
# ========================================================
74119
# Protobuf to Python
75120
# ========================================================

0 commit comments

Comments
 (0)