From fca15995798bb377c7e6df9f2e1d93a818b189d8 Mon Sep 17 00:00:00 2001 From: Andrei Date: Mon, 28 Nov 2016 05:50:53 -0600 Subject: [PATCH] bugfix - fields without a default but set to None would become UNSET etc stuff as well --- disco/gateway/events.py | 4 ++-- disco/types/base.py | 17 ++++++++++------- disco/types/guild.py | 3 +++ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/disco/gateway/events.py b/disco/gateway/events.py index 5fc28cf..1b4b0b6 100644 --- a/disco/gateway/events.py +++ b/disco/gateway/events.py @@ -7,7 +7,7 @@ from disco.types.user import User, Presence from disco.types.channel import Channel from disco.types.message import Message, MessageReactionEmoji from disco.types.voice import VoiceState -from disco.types.guild import Guild, GuildMember, Role, Emoji +from disco.types.guild import Guild, GuildMember, Role, GuildEmoji from disco.types.base import Model, ModelMeta, Field, ListField, snowflake, lazy_datetime @@ -295,7 +295,7 @@ class GuildEmojisUpdate(GatewayEvent): The new set of emojis for the guild """ guild_id = Field(snowflake) - emojis = ListField(Emoji) + emojis = ListField(GuildEmoji) class GuildIntegrationsUpdate(GatewayEvent): diff --git a/disco/types/base.py b/disco/types/base.py index 0c81e70..28e6da6 100644 --- a/disco/types/base.py +++ b/disco/types/base.py @@ -290,18 +290,21 @@ class Model(six.with_metaclass(ModelMeta, AsyncChainable)): should_skip = skip and name in skip if consume and not should_skip: - raw = obj.pop(field.src_name, None) + raw = obj.pop(field.src_name, UNSET) else: - raw = obj.get(field.src_name, None) + raw = obj.get(field.src_name, UNSET) - if raw is None or should_skip: - if field.has_default(): - default = field.default() if callable(field.default) else field.default - else: - default = UNSET + # If the field is unset/none, and we have a default we need to set it + if (raw in (None, UNSET) or should_skip) and field.has_default(): + default = field.default() if callable(field.default) else field.default setattr(self, field.dst_name, default) continue + # Otherwise if the field is UNSET and has no default, skip conversion + if raw is UNSET or should_skip: + setattr(self, field.dst_name, raw) + continue + value = field.try_convert(raw, self.client) setattr(self, field.dst_name, value) diff --git a/disco/types/guild.py b/disco/types/guild.py index 5048535..0b652fa 100644 --- a/disco/types/guild.py +++ b/disco/types/guild.py @@ -49,6 +49,9 @@ class GuildEmoji(Emoji): managed = Field(bool) roles = ListField(snowflake) + def __str__(self): + return u'<:{}:{}>'.format(self.name, self.id) + @property def url(self): return 'https://discordapp.com/api/emojis/{}.png'.format(self.id)