Skip to content

Commit e3eed2d

Browse files
authored
Issue #31 - Access name attribute of any type object (#92)
Addresses Issue #31 - we should access a string representation of the given object rather than assuming that the object itself can be concatenated with a string.
1 parent cbd31e7 commit e3eed2d

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

pulsar/schema/definition.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@
2323
from enum import Enum, EnumMeta
2424

2525

26+
def _string_representation(x):
27+
if hasattr(x, "__name__"):
28+
return x.__name__
29+
else:
30+
return str(x)
31+
32+
2633
def _check_record_or_field(x):
2734
if (type(x) is type and not issubclass(x, Record)) \
2835
and not isinstance(x, Field):
29-
raise Exception('Argument ' + x + ' is not a Record or a Field')
36+
raise Exception('Argument ' + _string_representation(x) + ' is not a Record or a Field')
3037

3138

3239
class RecordMeta(type):
@@ -188,7 +195,7 @@ def validate_type(self, name, val):
188195

189196
if not isinstance(val, self.__class__):
190197
raise TypeError("Invalid type '%s' for sub-record field '%s'. Expected: %s" % (
191-
type(val), name, self.__class__))
198+
type(val), name, _string_representation(self.__class__)))
192199
return val
193200

194201
def default(self):
@@ -222,7 +229,7 @@ def validate_type(self, name, val):
222229
return self.default()
223230

224231
if type(val) != self.python_type():
225-
raise TypeError("Invalid type '%s' for field '%s'. Expected: %s" % (type(val), name, self.python_type()))
232+
raise TypeError("Invalid type '%s' for field '%s'. Expected: %s" % (type(val), name, _string_representation(self.python_type())))
226233
return val
227234

228235
def schema(self):
@@ -368,7 +375,7 @@ def default(self):
368375
class CustomEnum(Field):
369376
def __init__(self, enum_type, default=None, required=False, required_default=False):
370377
if not issubclass(enum_type, Enum):
371-
raise Exception(enum_type + " is not a valid Enum type")
378+
raise Exception(_string_representation(enum_type) + " is not a valid Enum type")
372379
self.enum_type = enum_type
373380
self.values = {}
374381
for x in enum_type.__members__.values():
@@ -400,7 +407,7 @@ def validate_type(self, name, val):
400407
raise TypeError(
401408
"Invalid enum value '%s' for field '%s'. Expected: %s" % (val, name, self.values.keys()))
402409
elif type(val) != self.python_type():
403-
raise TypeError("Invalid type '%s' for field '%s'. Expected: %s" % (type(val), name, self.python_type()))
410+
raise TypeError("Invalid type '%s' for field '%s'. Expected: %s" % (type(val), name, _string_representation(self.python_type())))
404411
else:
405412
return val
406413

@@ -445,7 +452,7 @@ def validate_type(self, name, val):
445452
for x in val:
446453
if type(x) != self.array_type.python_type():
447454
raise TypeError('Array field ' + name + ' items should all be of type ' +
448-
self.array_type.type())
455+
_string_representation(self.array_type.type()))
449456
return val
450457

451458
def schema(self):
@@ -488,7 +495,7 @@ def validate_type(self, name, val):
488495
raise TypeError('Map keys for field ' + name + ' should all be strings')
489496
if type(v) != self.value_type.python_type():
490497
raise TypeError('Map values for field ' + name + ' should all be of type '
491-
+ self.value_type.python_type())
498+
+ _string_representation(self.value_type.python_type()))
492499

493500
return val
494501

0 commit comments

Comments
 (0)