|
23 | 23 | from enum import Enum, EnumMeta
|
24 | 24 |
|
25 | 25 |
|
| 26 | +def _string_representation(x): |
| 27 | + if hasattr(x, "__name__"): |
| 28 | + return x.__name__ |
| 29 | + else: |
| 30 | + return str(x) |
| 31 | + |
| 32 | + |
26 | 33 | def _check_record_or_field(x):
|
27 | 34 | if (type(x) is type and not issubclass(x, Record)) \
|
28 | 35 | 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') |
30 | 37 |
|
31 | 38 |
|
32 | 39 | class RecordMeta(type):
|
@@ -188,7 +195,7 @@ def validate_type(self, name, val):
|
188 | 195 |
|
189 | 196 | if not isinstance(val, self.__class__):
|
190 | 197 | 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__))) |
192 | 199 | return val
|
193 | 200 |
|
194 | 201 | def default(self):
|
@@ -222,7 +229,7 @@ def validate_type(self, name, val):
|
222 | 229 | return self.default()
|
223 | 230 |
|
224 | 231 | 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()))) |
226 | 233 | return val
|
227 | 234 |
|
228 | 235 | def schema(self):
|
@@ -368,7 +375,7 @@ def default(self):
|
368 | 375 | class CustomEnum(Field):
|
369 | 376 | def __init__(self, enum_type, default=None, required=False, required_default=False):
|
370 | 377 | 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") |
372 | 379 | self.enum_type = enum_type
|
373 | 380 | self.values = {}
|
374 | 381 | for x in enum_type.__members__.values():
|
@@ -400,7 +407,7 @@ def validate_type(self, name, val):
|
400 | 407 | raise TypeError(
|
401 | 408 | "Invalid enum value '%s' for field '%s'. Expected: %s" % (val, name, self.values.keys()))
|
402 | 409 | 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()))) |
404 | 411 | else:
|
405 | 412 | return val
|
406 | 413 |
|
@@ -445,7 +452,7 @@ def validate_type(self, name, val):
|
445 | 452 | for x in val:
|
446 | 453 | if type(x) != self.array_type.python_type():
|
447 | 454 | raise TypeError('Array field ' + name + ' items should all be of type ' +
|
448 |
| - self.array_type.type()) |
| 455 | + _string_representation(self.array_type.type())) |
449 | 456 | return val
|
450 | 457 |
|
451 | 458 | def schema(self):
|
@@ -488,7 +495,7 @@ def validate_type(self, name, val):
|
488 | 495 | raise TypeError('Map keys for field ' + name + ' should all be strings')
|
489 | 496 | if type(v) != self.value_type.python_type():
|
490 | 497 | 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())) |
492 | 499 |
|
493 | 500 | return val
|
494 | 501 |
|
|
0 commit comments