Repository: hikari-py/hikari
Pull request #2197 (2025-03-06 16:39:39)

Fix application command deserialization when context is None
### Summary
<!-- Small summary of the merge request -->

### Checklist
<!-- Make sure to tick all the following boxes by putting an `x` in between (like this `[x]`) -->
- [ ] I have run `nox` and all the pipelines have passed.
- [ ] I have made unittests according to the code I have added/modified/deleted.

### Related issues
<!--
To mention an issue use `#issue-id` and to mention a merge request use `!merge-request-id`
To close/fix an issue use `Close #issue-id` or `Fix #issue-id` (depending on the merge request)
-->



Patch:
diff --git a/hikari/impl/entity_factory.py b/hikari/impl/entity_factory.py
index 29428cf1..881bf3e4 100644
--- a/hikari/impl/entity_factory.py
+++ b/hikari/impl/entity_factory.py
@@ -2324,7 +2324,7 @@ def deserialize_slash_command(
         ]
 
         context_types = [
-            application_models.ApplicationContextType(int(context)) for context in payload.get("contexts", ())
+            application_models.ApplicationContextType(int(context)) for context in payload.get("contexts") or ()
         ]
 
         return commands.SlashCommand(
@@ -2375,7 +2375,7 @@ def deserialize_context_menu_command(
         ]
 
         context_types = [
-            application_models.ApplicationContextType(int(context)) for context in payload.get("contexts", ())
+            application_models.ApplicationContextType(int(context)) for context in payload.get("contexts") or ()
         ]
 
         return commands.ContextMenuCommand(


Test patch:
diff --git a/tests/hikari/impl/test_entity_factory.py b/tests/hikari/impl/test_entity_factory.py
index 71b89d61..a304691d 100644
--- a/tests/hikari/impl/test_entity_factory.py
+++ b/tests/hikari/impl/test_entity_factory.py
@@ -4114,20 +4114,13 @@ def test_deserialize_slash_command_with_passed_through_guild_id(self, entity_fac
 
         assert command.guild_id == 123123
 
-    def test_deserialize_slash_command_with_null_and_unset_values(self, entity_factory_impl):
-        payload = {
-            "id": "1231231231",
-            "application_id": "12354123",
-            "guild_id": "49949494",
-            "type": 1,
-            "name": "good name",
-            "description": "very good description",
-            "options": [],
-            "default_member_permissions": 0,
-            "version": "43123",
-        }
+    def test_deserialize_slash_command_with_unset_values(self, entity_factory_impl, slash_command_payload):
+        del slash_command_payload["options"]
+        del slash_command_payload["nsfw"]
+        del slash_command_payload["integration_types"]
+        del slash_command_payload["contexts"]
 
-        command = entity_factory_impl.deserialize_slash_command(payload)
+        command = entity_factory_impl.deserialize_slash_command(slash_command_payload)
 
         assert command.options is None
         assert command.is_nsfw is False
@@ -4135,6 +4128,13 @@ def test_deserialize_slash_command_with_null_and_unset_values(self, entity_facto
         assert command.context_types == []
         assert isinstance(command, commands.SlashCommand)
 
+    def test_deserialize_slash_command_with_null_values(self, entity_factory_impl, slash_command_payload):
+        slash_command_payload["contexts"] = None
+
+        command = entity_factory_impl.deserialize_slash_command(payload=slash_command_payload)
+
+        assert command.context_types == []
+
     def test_deserialize_slash_command_standardizes_default_member_permissions(
         self, entity_factory_impl, slash_command_payload
     ):
@@ -4838,7 +4838,14 @@ def test_deserialize_context_menu_command_with_guild_id(self, entity_factory_imp
         assert command.integration_types == [application_models.ApplicationIntegrationType.GUILD_INSTALL]
         assert command.context_types == [application_models.ApplicationContextType.GUILD]
 
-    def test_deserialize_context_menu_command_with_with_null_and_unset_values(
+    def test_deserialize_context_menu_command_with_null_values(self, entity_factory_impl, slash_command_payload):
+        slash_command_payload["contexts"] = None
+
+        command = entity_factory_impl.deserialize_slash_command(payload=slash_command_payload)
+
+        assert command.context_types == []
+
+    def test_deserialize_context_menu_command_with_with__unset_values(
         self, entity_factory_impl, context_menu_command_payload
     ):
         del context_menu_command_payload["dm_permission"]