in entities/python/src/onedrive_client/entities/base.py [0:0]
def __setitem__(self, key, value):
"""Set item and apply type-checking rules.
- Enforce type of the field. Raise ``TypeError``
if the type doesn't match.
- Don't allow multiple fields within oneof group to be set.
"""
field_descriptor = self.__get_field_descriptor(key)
oneof = field_descriptor.containing_oneof
if oneof is not None:
try:
next(f.name for f in oneof.fields
if f.name != key and f.name in self)
except StopIteration:
pass
else:
raise TypeError(
'Can\'t set one of the OneOf fields '
'while other is present.'
)
if self.__is_repeated_field(field_descriptor):
collection_cls = self.__make_collection(field_descriptor)
items = collection_cls(value)
self.__data[key] = items
return
elif self.__is_composite_field(field_descriptor):
sub_entity_cls = self.__get_or_make_entity(
field_descriptor.message_type
)
if not isinstance(value, sub_entity_cls):
value = sub_entity_cls(value)
self.__data[key] = value
return
elif self.__is_enum_field(field_descriptor):
enum_cls = self.__get_or_make_enum(field_descriptor.enum_type)
if not isinstance(value, enum_cls):
try:
value = enum_cls(value)
except ValueError as exc:
raise TypeError(exc)
self.__data[key] = value
return
else:
type_ = self.__get_regular_field_types(field_descriptor)
if not isinstance(value, type_):
raise TypeError(self.__FIELD_TYPE_ERROR_MESSAGE)
self.__data[key] = value