Skip to content

Issue #31 - Access name attribute of any type object #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 13, 2023
Merged
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
21 changes: 14 additions & 7 deletions pulsar/schema/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@
from enum import Enum, EnumMeta


def _string_representation(x):
if hasattr(x, "__name__"):
return x.__name__
else:
return str(x)


def _check_record_or_field(x):
if (type(x) is type and not issubclass(x, Record)) \
and not isinstance(x, Field):
raise Exception('Argument ' + x + ' is not a Record or a Field')
raise Exception('Argument ' + _string_representation(x) + ' is not a Record or a Field')


class RecordMeta(type):
Expand Down Expand Up @@ -188,7 +195,7 @@ def validate_type(self, name, val):

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

def default(self):
Expand Down Expand Up @@ -222,7 +229,7 @@ def validate_type(self, name, val):
return self.default()

if type(val) != self.python_type():
raise TypeError("Invalid type '%s' for field '%s'. Expected: %s" % (type(val), name, self.python_type()))
raise TypeError("Invalid type '%s' for field '%s'. Expected: %s" % (type(val), name, _string_representation(self.python_type())))
return val

def schema(self):
Expand Down Expand Up @@ -368,7 +375,7 @@ def default(self):
class CustomEnum(Field):
def __init__(self, enum_type, default=None, required=False, required_default=False):
if not issubclass(enum_type, Enum):
raise Exception(enum_type + " is not a valid Enum type")
raise Exception(_string_representation(enum_type) + " is not a valid Enum type")
self.enum_type = enum_type
self.values = {}
for x in enum_type.__members__.values():
Expand Down Expand Up @@ -400,7 +407,7 @@ def validate_type(self, name, val):
raise TypeError(
"Invalid enum value '%s' for field '%s'. Expected: %s" % (val, name, self.values.keys()))
elif type(val) != self.python_type():
raise TypeError("Invalid type '%s' for field '%s'. Expected: %s" % (type(val), name, self.python_type()))
raise TypeError("Invalid type '%s' for field '%s'. Expected: %s" % (type(val), name, _string_representation(self.python_type())))
else:
return val

Expand Down Expand Up @@ -445,7 +452,7 @@ def validate_type(self, name, val):
for x in val:
if type(x) != self.array_type.python_type():
raise TypeError('Array field ' + name + ' items should all be of type ' +
self.array_type.type())
_string_representation(self.array_type.type()))
return val

def schema(self):
Expand Down Expand Up @@ -488,7 +495,7 @@ def validate_type(self, name, val):
raise TypeError('Map keys for field ' + name + ' should all be strings')
if type(v) != self.value_type.python_type():
raise TypeError('Map values for field ' + name + ' should all be of type '
+ self.value_type.python_type())
+ _string_representation(self.value_type.python_type()))

return val

Expand Down