Repository: hikari-py/hikari
Pull request #674 (2021-06-19 20:31:09)

Default content to None when empty string
### Checklist
<!-- Make sure to tick all the following boxes by putting an `x` in between (like this `[x]`) -->
- [x] I have run `nox` and all the pipelines have passed.
- [x] I have made unittests according to the code I have added/modified/deleted.


Patch:
diff --git a/dev-requirements.txt b/dev-requirements.txt
index 5cddde4d..5772879e 100644
--- a/dev-requirements.txt
+++ b/dev-requirements.txt
@@ -41,4 +41,4 @@ safety~=1.10.3
 #############
 
 black==21.6b0
-isort==5.8.0
+isort==5.9.0
diff --git a/hikari/impl/entity_factory.py b/hikari/impl/entity_factory.py
index 6bb69b5a..8f876965 100644
--- a/hikari/impl/entity_factory.py
+++ b/hikari/impl/entity_factory.py
@@ -1739,6 +1739,10 @@ def deserialize_partial_message(  # noqa CFQ001 - Function too long
         if "stickers" in payload:
             stickers = [self._deserialize_sticker(sticker) for sticker in payload["stickers"]]
 
+        content = payload.get("content", undefined.UNDEFINED)
+        if content is not undefined.UNDEFINED:
+            content = content or None  # Default to None if content is an empty string
+
         message = message_models.PartialMessage(
             app=self._app,
             id=snowflakes.Snowflake(payload["id"]),
@@ -1746,14 +1750,14 @@ def deserialize_partial_message(  # noqa CFQ001 - Function too long
             guild_id=guild_id,
             author=author,
             member=member,
-            content=payload["content"] if "content" in payload else undefined.UNDEFINED,
+            content=content,
             timestamp=timestamp,
             edited_timestamp=edited_timestamp,
-            is_tts=payload["tts"] if "tts" in payload else undefined.UNDEFINED,
+            is_tts=payload.get("tts", undefined.UNDEFINED),
             attachments=attachments,
             embeds=embeds,
             reactions=reactions,
-            is_pinned=payload["pinned"] if "pinned" in payload else undefined.UNDEFINED,
+            is_pinned=payload.get("pinned", undefined.UNDEFINED),
             webhook_id=snowflakes.Snowflake(payload["webhook_id"]) if "webhook_id" in payload else undefined.UNDEFINED,
             type=message_models.MessageType(payload["type"]) if "type" in payload else undefined.UNDEFINED,
             activity=activity,
@@ -1762,7 +1766,7 @@ def deserialize_partial_message(  # noqa CFQ001 - Function too long
             referenced_message=referenced_message,
             flags=message_models.MessageFlag(payload["flags"]) if "flags" in payload else undefined.UNDEFINED,
             stickers=stickers,
-            nonce=payload["nonce"] if "nonce" in payload else undefined.UNDEFINED,
+            nonce=payload.get("nonce", undefined.UNDEFINED),
             # We initialize these next.
             mentions=NotImplemented,
         )
@@ -1850,7 +1854,7 @@ def deserialize_message(  # noqa CFQ001 - Function too long
             guild_id=guild_id,
             author=author,
             member=member,
-            content=payload["content"],
+            content=payload["content"] or None,
             timestamp=time.iso8601_datetime_string_to_datetime(payload["timestamp"]),
             edited_timestamp=edited_timestamp,
             is_tts=payload["tts"],


Test patch:
diff --git a/tests/hikari/impl/test_entity_factory.py b/tests/hikari/impl/test_entity_factory.py
index 402b1569..28f7fd19 100644
--- a/tests/hikari/impl/test_entity_factory.py
+++ b/tests/hikari/impl/test_entity_factory.py
@@ -3048,6 +3048,7 @@ def test_deserialize_partial_message(
         assert partial_message.nonce == "171000788183678976"
 
     def test_deserialize_partial_message_with_partial_fields(self, entity_factory_impl, message_payload):
+        message_payload["content"] = ""
         message_payload["edited_timestamp"] = None
         message_payload["member"] = None
         message_payload["application"]["primary_sku_id"] = None
@@ -3056,7 +3057,10 @@ def test_deserialize_partial_message_with_partial_fields(self, entity_factory_im
         del message_payload["message_reference"]["message_id"]
         del message_payload["message_reference"]["guild_id"]
         del message_payload["application"]["cover_image"]
+
         partial_message = entity_factory_impl.deserialize_partial_message(message_payload)
+
+        assert partial_message.content is None
         assert partial_message.edited_timestamp is None
         assert partial_message._guild_id is not None
         assert partial_message.member is None
@@ -3069,6 +3073,7 @@ def test_deserialize_partial_message_with_partial_fields(self, entity_factory_im
 
     def test_deserialize_partial_message_with_unset_fields(self, entity_factory_impl, mock_app):
         partial_message = entity_factory_impl.deserialize_partial_message({"id": 123, "channel_id": 456})
+
         assert partial_message.app is mock_app
         assert partial_message.id == 123
         assert partial_message.channel_id == 456
@@ -3213,7 +3218,7 @@ def test_deserialize_message_with_null_and_unset_fields(
             "id": "123",
             "channel_id": "456",
             "author": user_payload,
-            "content": "some info",
+            "content": "",
             "timestamp": "2020-03-21T21:20:16.510000+00:00",
             "edited_timestamp": None,
             "tts": True,
@@ -3228,6 +3233,7 @@ def test_deserialize_message_with_null_and_unset_fields(
 
         message = entity_factory_impl.deserialize_message(message_payload)
         assert message.app is mock_app
+        assert message.content is None
         assert message._guild_id is None
         assert message.member is None
         assert message.edited_timestamp is None