Skip to content
Merged
Changes from 4 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
23 changes: 12 additions & 11 deletions src/nipanel/_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

from __future__ import annotations

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

from google.protobuf import any_pb2
from nitypes.vector import Vector
from nitypes.waveform import AnalogWaveform, ComplexWaveform

from nipanel.converters import Converter
Expand Down Expand Up @@ -73,13 +75,6 @@
VectorConverter(),
]

_CONVERTIBLE_COLLECTION_TYPES = {
frozenset,
list,
set,
tuple,
}

_CONVERTER_FOR_PYTHON_TYPE = {entry.python_typename: entry for entry in _CONVERTIBLE_TYPES}
_CONVERTER_FOR_GRPC_TYPE = {entry.protobuf_typename: entry for entry in _CONVERTIBLE_TYPES}
_SUPPORTED_PYTHON_TYPES = _CONVERTER_FOR_PYTHON_TYPE.keys()
Expand All @@ -97,7 +92,7 @@ def _get_best_matching_type(python_value: object) -> str:
additional_info_string = _get_additional_type_info_string(python_value)

container_types = []
value_is_collection = any(_CONVERTIBLE_COLLECTION_TYPES.intersection(underlying_parents))
value_is_collection = _is_collection_for_convert(python_value)
# Variable to use when traversing down through collection types.
working_python_value = python_value
while value_is_collection:
Expand All @@ -119,9 +114,7 @@ def _get_best_matching_type(python_value: object) -> str:
# If this element is a collection, we want to continue traversing. Once we find a
# non-collection, underlying_parents will refer to the candidates for the non-
# collection type.
value_is_collection = any(
_CONVERTIBLE_COLLECTION_TYPES.intersection(underlying_parents)
)
value_is_collection = _is_collection_for_convert(working_python_value)
container_types.append(Collection)

best_matching_type = None
Expand Down Expand Up @@ -192,3 +185,11 @@ def _get_additional_type_info_string(python_value: object) -> str:
return str(python_value.dtype)
else:
return ""


def _is_collection_for_convert(python_value: object) -> bool:
# str, bytes, dict, Enum, and Vector are instances of Collection
# but they are either invalid types or have custom converters.
return isinstance(python_value, Collection) and not isinstance(
python_value, (str, bytes, dict, enum.Enum, Vector)
)
Loading