Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/nipanel/_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import logging
from collections.abc import Collection
from typing import Any
from typing import Any, Iterable

from google.protobuf import any_pb2

Expand Down Expand Up @@ -104,7 +104,7 @@ def _get_best_matching_type(python_value: object) -> str:
container_types.append(Collection.__name__)

best_matching_type = None
candidates = [parent.__name__ for parent in underlying_parents]
candidates = _get_candidate_strings(underlying_parents)
for candidate in candidates:
containers_str = ".".join(container_types)
python_typename = f"{containers_str}.{candidate}" if containers_str else candidate
Expand Down Expand Up @@ -142,3 +142,11 @@ def is_supported_type(value: object) -> bool:
return True
except TypeError:
return False


def _get_candidate_strings(candidates: Iterable[type]) -> list[str]:
candidate_names = []
for candidate in candidates:
candidate_names.append(f"{candidate.__module__}.{candidate.__name__}")

return candidate_names
24 changes: 12 additions & 12 deletions src/nipanel/converters/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BoolConverter(Converter[bool, wrappers_pb2.BoolValue]):
@property
def python_typename(self) -> str:
"""The Python type that this converter handles."""
return bool.__name__
return f"{bool.__module__}.{bool.__name__}"

@property
def protobuf_message(self) -> Type[wrappers_pb2.BoolValue]:
Expand All @@ -38,7 +38,7 @@ class BytesConverter(Converter[bytes, wrappers_pb2.BytesValue]):
@property
def python_typename(self) -> str:
"""The Python type that this converter handles."""
return bytes.__name__
return f"{bytes.__module__}.{bytes.__name__}"

@property
def protobuf_message(self) -> Type[wrappers_pb2.BytesValue]:
Expand All @@ -60,7 +60,7 @@ class FloatConverter(Converter[float, wrappers_pb2.DoubleValue]):
@property
def python_typename(self) -> str:
"""The Python type that this converter handles."""
return float.__name__
return f"{float.__module__}.{float.__name__}"

@property
def protobuf_message(self) -> Type[wrappers_pb2.DoubleValue]:
Expand All @@ -82,7 +82,7 @@ class IntConverter(Converter[int, wrappers_pb2.Int64Value]):
@property
def python_typename(self) -> str:
"""The Python type that this converter handles."""
return int.__name__
return f"{int.__module__}.{int.__name__}"

@property
def protobuf_message(self) -> Type[wrappers_pb2.Int64Value]:
Expand All @@ -104,7 +104,7 @@ class StrConverter(Converter[str, wrappers_pb2.StringValue]):
@property
def python_typename(self) -> str:
"""The Python type that this converter handles."""
return str.__name__
return f"{str.__module__}.{str.__name__}"

@property
def protobuf_message(self) -> Type[wrappers_pb2.StringValue]:
Expand All @@ -126,7 +126,7 @@ class DTDateTimeConverter(Converter[dt.datetime, timestamp_pb2.Timestamp]):
@property
def python_typename(self) -> str:
"""The Python type that this converter handles."""
return dt.datetime.__name__
return f"{dt.datetime.__module__}.{dt.datetime.__name__}"

@property
def protobuf_message(self) -> Type[timestamp_pb2.Timestamp]:
Expand All @@ -150,7 +150,7 @@ class DTTimeDeltaConverter(Converter[dt.timedelta, duration_pb2.Duration]):
@property
def python_typename(self) -> str:
"""The Python type that this converter handles."""
return dt.timedelta.__name__
return f"{dt.timedelta.__module__}.{dt.timedelta.__name__}"

@property
def protobuf_message(self) -> Type[duration_pb2.Duration]:
Expand All @@ -174,7 +174,7 @@ class BoolCollectionConverter(Converter[Collection[bool], panel_types_pb2.BoolCo
@property
def python_typename(self) -> str:
"""The Python type that this converter handles."""
return f"{Collection.__name__}.{bool.__name__}"
return f"{Collection.__name__}.{bool.__module__}.{bool.__name__}"

@property
def protobuf_message(self) -> Type[panel_types_pb2.BoolCollection]:
Expand All @@ -196,7 +196,7 @@ class BytesCollectionConverter(Converter[Collection[bytes], panel_types_pb2.Byte
@property
def python_typename(self) -> str:
"""The Python type that this converter handles."""
return f"{Collection.__name__}.{bytes.__name__}"
return f"{Collection.__name__}.{bytes.__module__}.{bytes.__name__}"

@property
def protobuf_message(self) -> Type[panel_types_pb2.ByteStringCollection]:
Expand All @@ -222,7 +222,7 @@ class FloatCollectionConverter(Converter[Collection[float], panel_types_pb2.Floa
@property
def python_typename(self) -> str:
"""The Python type that this converter handles."""
return f"{Collection.__name__}.{float.__name__}"
return f"{Collection.__name__}.{float.__module__}.{float.__name__}"

@property
def protobuf_message(self) -> Type[panel_types_pb2.FloatCollection]:
Expand All @@ -248,7 +248,7 @@ class IntCollectionConverter(Converter[Collection[int], panel_types_pb2.IntColle
@property
def python_typename(self) -> str:
"""The Python type that this converter handles."""
return f"{Collection.__name__}.{int.__name__}"
return f"{Collection.__name__}.{int.__module__}.{int.__name__}"

@property
def protobuf_message(self) -> Type[panel_types_pb2.IntCollection]:
Expand All @@ -270,7 +270,7 @@ class StrCollectionConverter(Converter[Collection[str], panel_types_pb2.StringCo
@property
def python_typename(self) -> str:
"""The Python type that this converter handles."""
return f"{Collection.__name__}.{str.__name__}"
return f"{Collection.__name__}.{str.__module__}.{str.__name__}"

@property
def protobuf_message(self) -> Type[panel_types_pb2.StringCollection]:
Expand Down
8 changes: 4 additions & 4 deletions src/nipanel/converters/protobuf_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Double2DArrayConverter(Converter[Collection[Collection[float]], Double2DAr
@property
def python_typename(self) -> str:
"""The Python type that this converter handles."""
return f"{Collection.__name__}.{Collection.__name__}.{float.__name__}"
return f"{Collection.__name__}.{Collection.__name__}.{float.__module__}.{float.__name__}"

@property
def protobuf_message(self) -> Type[Double2DArray]:
Expand Down Expand Up @@ -97,7 +97,7 @@ def __init__(self) -> None:
@property
def python_typename(self) -> str:
"""The Python type that this converter handles."""
return AnalogWaveform.__name__
return f"{AnalogWaveform.__module__}.{AnalogWaveform.__name__}"

@property
def protobuf_message(self) -> Type[DoubleAnalogWaveform]:
Expand Down Expand Up @@ -194,7 +194,7 @@ class PrecisionTimestampConverter(Converter[bt.DateTime, PrecisionTimestamp]):
@property
def python_typename(self) -> str:
"""The Python type that this converter handles."""
return bt.DateTime.__name__
return f"{bt.DateTime.__module__}.{bt.DateTime.__name__}"

@property
def protobuf_message(self) -> Type[PrecisionTimestamp]:
Expand All @@ -220,7 +220,7 @@ class ScalarConverter(Converter[Scalar[_AnyScalarType], scalar_pb2.ScalarData]):
@property
def python_typename(self) -> str:
"""The Python type that this converter handles."""
return Scalar.__name__
return f"{Scalar.__module__}.{Scalar.__name__}"

@property
def protobuf_message(self) -> Type[scalar_pb2.ScalarData]:
Expand Down
104 changes: 55 additions & 49 deletions tests/unit/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,55 +44,61 @@
@pytest.mark.parametrize(
"python_object, expected_type_string",
[
(False, "bool"),
(b"mystr", "bytes"),
(456.2, "float"),
(123, "int"),
("mystr", "str"),
(tests.types.MyIntFlags.VALUE1, "int"),
(tests.types.MyIntEnum.VALUE10, "int"),
(tests.types.MixinIntEnum.VALUE11, "int"),
(tests.types.MyStrEnum.VALUE1, "str"),
(tests.types.MixinStrEnum.VALUE11, "str"),
(dt.datetime.now(), "datetime"),
(dt.timedelta(days=1), "timedelta"),
([False, False], "Collection.bool"),
([b"mystr", b"mystr"], "Collection.bytes"),
([456.2, 1.0], "Collection.float"),
([123, 456], "Collection.int"),
(["mystr", "mystr"], "Collection.str"),
((False, False), "Collection.bool"),
((b"mystr", b"mystr"), "Collection.bytes"),
((456.2, 1.0), "Collection.float"),
((123, 456), "Collection.int"),
(("mystr", "mystr"), "Collection.str"),
((False, False), "Collection.bool"),
((b"mystr", b"mystr"), "Collection.bytes"),
((456.2, 1.0), "Collection.float"),
((123, 456), "Collection.int"),
(("mystr", "mystr"), "Collection.str"),
(set([False, True]), "Collection.bool"),
(set([b"mystr", b"mystr2"]), "Collection.bytes"),
(set([456.2, 1.0]), "Collection.float"),
(set([123, 456]), "Collection.int"),
(set(["mystr", "mystr2"]), "Collection.str"),
(frozenset([False, True]), "Collection.bool"),
(frozenset([b"mystr", b"mystr2"]), "Collection.bytes"),
(frozenset([456.2, 1.0]), "Collection.float"),
(frozenset([123, 456]), "Collection.int"),
(frozenset(["mystr", "mystr2"]), "Collection.str"),
([[1.0, 2.0], [1.0, 2.0]], "Collection.Collection.float"),
([(1.0, 2.0), (3.0, 4.0)], "Collection.Collection.float"),
([set([1.0, 2.0]), set([3.0, 4.0])], "Collection.Collection.float"),
([frozenset([1.0, 2.0]), frozenset([3.0, 4.0])], "Collection.Collection.float"),
(([1.0, 2.0], [3.0, 4.0]), "Collection.Collection.float"),
(((1.0, 2.0), (3.0, 4.0)), "Collection.Collection.float"),
((set([1.0, 2.0]), set([3.0, 4.0])), "Collection.Collection.float"),
((frozenset([1.0, 2.0]), frozenset([3.0, 4.0])), "Collection.Collection.float"),
(set([(1.0, 2.0), (3.0, 4.0)]), "Collection.Collection.float"),
(set([frozenset([1.0, 2.0]), frozenset([3.0, 4.0])]), "Collection.Collection.float"),
(frozenset([(1.0, 2.0), (3.0, 4.0)]), "Collection.Collection.float"),
(frozenset([frozenset([1.0, 2.0]), frozenset([3.0, 4.0])]), "Collection.Collection.float"),
(False, "builtins.bool"),
(b"mystr", "builtins.bytes"),
(456.2, "builtins.float"),
(123, "builtins.int"),
("mystr", "builtins.str"),
(tests.types.MyIntFlags.VALUE1, "builtins.int"),
(tests.types.MyIntEnum.VALUE10, "builtins.int"),
(tests.types.MixinIntEnum.VALUE11, "builtins.int"),
(tests.types.MyStrEnum.VALUE1, "builtins.str"),
(tests.types.MixinStrEnum.VALUE11, "builtins.str"),
(dt.datetime.now(), "datetime.datetime"),
(dt.timedelta(days=1), "datetime.timedelta"),
([False, False], "Collection.builtins.bool"),
([b"mystr", b"mystr"], "Collection.builtins.bytes"),
([456.2, 1.0], "Collection.builtins.float"),
([123, 456], "Collection.builtins.int"),
(["mystr", "mystr"], "Collection.builtins.str"),
((False, False), "Collection.builtins.bool"),
((b"mystr", b"mystr"), "Collection.builtins.bytes"),
((456.2, 1.0), "Collection.builtins.float"),
((123, 456), "Collection.builtins.int"),
(("mystr", "mystr"), "Collection.builtins.str"),
((False, False), "Collection.builtins.bool"),
((b"mystr", b"mystr"), "Collection.builtins.bytes"),
((456.2, 1.0), "Collection.builtins.float"),
((123, 456), "Collection.builtins.int"),
(("mystr", "mystr"), "Collection.builtins.str"),
(set([False, True]), "Collection.builtins.bool"),
(set([b"mystr", b"mystr2"]), "Collection.builtins.bytes"),
(set([456.2, 1.0]), "Collection.builtins.float"),
(set([123, 456]), "Collection.builtins.int"),
(set(["mystr", "mystr2"]), "Collection.builtins.str"),
(frozenset([False, True]), "Collection.builtins.bool"),
(frozenset([b"mystr", b"mystr2"]), "Collection.builtins.bytes"),
(frozenset([456.2, 1.0]), "Collection.builtins.float"),
(frozenset([123, 456]), "Collection.builtins.int"),
(frozenset(["mystr", "mystr2"]), "Collection.builtins.str"),
([[1.0, 2.0], [1.0, 2.0]], "Collection.Collection.builtins.float"),
([(1.0, 2.0), (3.0, 4.0)], "Collection.Collection.builtins.float"),
([set([1.0, 2.0]), set([3.0, 4.0])], "Collection.Collection.builtins.float"),
([frozenset([1.0, 2.0]), frozenset([3.0, 4.0])], "Collection.Collection.builtins.float"),
(([1.0, 2.0], [3.0, 4.0]), "Collection.Collection.builtins.float"),
(((1.0, 2.0), (3.0, 4.0)), "Collection.Collection.builtins.float"),
((set([1.0, 2.0]), set([3.0, 4.0])), "Collection.Collection.builtins.float"),
((frozenset([1.0, 2.0]), frozenset([3.0, 4.0])), "Collection.Collection.builtins.float"),
(set([(1.0, 2.0), (3.0, 4.0)]), "Collection.Collection.builtins.float"),
(
set([frozenset([1.0, 2.0]), frozenset([3.0, 4.0])]),
"Collection.Collection.builtins.float",
),
(frozenset([(1.0, 2.0), (3.0, 4.0)]), "Collection.Collection.builtins.float"),
(
frozenset([frozenset([1.0, 2.0]), frozenset([3.0, 4.0])]),
"Collection.Collection.builtins.float",
),
],
)
def test___various_python_objects___get_best_matching_type___returns_correct_type_string(
Expand Down
Loading