Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 20 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 @@ -102,7 +102,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 @@ -140,3 +140,21 @@ def is_supported_type(value: object) -> bool:
return True
except TypeError:
return False


def _get_candidate_strings(candidates: Iterable[type]) -> list[str]:
names_to_disambiguate = [
"datetime",
"DateTime",
"timedelta",
"TimeDelta",
]

candidate_names = []
for candidate in candidates:
if candidate.__name__ in names_to_disambiguate:
candidate_names.append(f"{candidate.__module__}.{candidate.__name__}")
else:
candidate_names.append(candidate.__name__)

return candidate_names
2 changes: 1 addition & 1 deletion src/nipanel/converters/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 Down
2 changes: 1 addition & 1 deletion tests/unit/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
(tests.types.MixinIntEnum.VALUE11, "int"),
(tests.types.MyStrEnum.VALUE1, "str"),
(tests.types.MixinStrEnum.VALUE11, "str"),
(dt.datetime.now(), "datetime"),
(dt.datetime.now(), "datetime.datetime"),
([False, False], "Collection.bool"),
([b"mystr", b"mystr"], "Collection.bytes"),
([456.2, 1.0], "Collection.float"),
Expand Down
Loading