Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion openhtf/util/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def convert_to_base_types(obj,
# Because it's *really* annoying to pass a single string accidentally.
assert not isinstance(ignore_keys, str), 'Pass a real iterable!'

if hasattr(obj, 'as_base_types'):
if hasattr(obj, 'as_base_types') and not inspect.isclass(obj):
return obj.as_base_types()
if hasattr(obj, '_asdict') and not inspect.isclass(obj):
obj = obj._asdict()
Expand Down
14 changes: 13 additions & 1 deletion test/util/data_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ def __init__(self):
def as_base_types(self):
return self.value

class ClassWithAsBaseTypes(object):

def as_base_types(self):
return 'called on instance'

@attr.s(slots=True, frozen=True)
class FrozenAttr(object):
value = attr.ib(type=int)
Expand All @@ -72,7 +77,8 @@ class EnumClass(enum.Enum):
'special': SpecialBaseTypes('must_not_be_present'),
'not_copied': not_copied,
'enum': EnumClass.A,

'class_with_as_base_types_instance': ClassWithAsBaseTypes(),
'class_with_as_base_types_class': ClassWithAsBaseTypes,
# Some plugs such as UserInputPlug will return None as a response to
# AsDict().
'none_dict': AsDict(),
Expand All @@ -99,6 +105,12 @@ class StrEnumClass(enum.StrEnum):
self.assertIsInstance(converted['special'], dict)
self.assertEqual(converted['special'], {'safe_value': True})
self.assertIs(converted['not_copied'], not_copied.value)
self.assertEqual(
converted['class_with_as_base_types_instance'], 'called on instance'
)
self.assertEqual(
converted['class_with_as_base_types_class'], str(ClassWithAsBaseTypes)
)

self.assertIsNone(converted['none_dict'])

Expand Down
Loading